Skip to content

Commit c68e717

Browse files
authored
Merge pull request #60 from fluentci-io/fix/clean-services
fix: always stop services at the end of pipeline execution
2 parents 42615af + 3818fd8 commit c68e717

File tree

5 files changed

+63
-10
lines changed

5 files changed

+63
-10
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ Requirements:
8686

8787
**Latest (CLI):**
8888

89-
- `Mac`: arm64: [fluentci_v0.15.4_aarch64-apple-darwin.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.4/fluentci_v0.15.4_aarch64-apple-darwin.tar.gz) intel: [fluentci_v0.15.4_x86_64-apple-darwin.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.4/fluentci_v0.15.4_x86_64-apple-darwin.tar.gz)
90-
- `Linux`: intel: [fluentci_v0.15.4_x86_64-unknown-linux-gnu.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.4/fluentci_v0.15.4_x86_64-unknown-linux-gnu.tar.gz) arm64: [fluentci_v0.15.4_aarch64-unknown-linux-gnu.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.4/fluentci_v0.15.4_aarch64-unknown-linux-gnu.tar.gz)
89+
- `Mac`: arm64: [fluentci_v0.15.5_aarch64-apple-darwin.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.5/fluentci_v0.15.5_aarch64-apple-darwin.tar.gz) intel: [fluentci_v0.15.5_x86_64-apple-darwin.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.5/fluentci_v0.15.5_x86_64-apple-darwin.tar.gz)
90+
- `Linux`: intel: [fluentci_v0.15.5_x86_64-unknown-linux-gnu.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.5/fluentci_v0.15.5_x86_64-unknown-linux-gnu.tar.gz) arm64: [fluentci_v0.15.5_aarch64-unknown-linux-gnu.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.5/fluentci_v0.15.5_aarch64-unknown-linux-gnu.tar.gz)
9191

9292
## ✨ Quick Start
9393

@@ -110,7 +110,7 @@ fluentci studio
110110
fluentci --help
111111

112112
Usage: fluentci [pipeline] [jobs...]
113-
Version: 0.15.4
113+
Version: 0.15.5
114114

115115
Description:
116116

src/cmd/agent.ts

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
getAccessToken,
2323
getDaggerVersion,
2424
isLogged,
25+
stopServices,
2526
} from "../utils.ts";
2627
import { hostname, release, cpus, arch, totalmem, platform } from "node:os";
2728
import { Action, Agent, Log, Run } from "../types.ts";
@@ -399,6 +400,11 @@ async function executeActions(
399400
body: `fluentci_exit=1`,
400401
headers,
401402
}).catch((e) => logger.error(e.message));
403+
404+
if (actions.some((x) => x.use_wasm)) {
405+
await stopServices(cwd);
406+
}
407+
402408
return;
403409
}
404410
}
@@ -447,6 +453,10 @@ async function executeActions(
447453
(e) => logger.error(e.message)
448454
);
449455

456+
if (actions.some((x) => x.use_wasm)) {
457+
await stopServices(cwd);
458+
}
459+
450460
fetch(`${FLUENTCI_EVENTS_URL}?client_id=${clientId}`, {
451461
method: "POST",
452462
body: `fluentci_exit=0`,

src/consts.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { dir } from "../deps.ts";
2-
export const VERSION = "0.15.4";
2+
export const VERSION = "0.15.5";
33

44
export const BASE_URL = "https://api.fluentci.io/v1";
55

src/server/executor.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Action } from "./graphql/objects/action.ts";
33
import { Run } from "./graphql/objects/run.ts";
44
import { createId, dayjs } from "../../deps.ts";
55
import { Log } from "./graphql/objects/log.ts";
6-
import { sendSocketMessage } from "../utils.ts";
6+
import { sendSocketMessage, stopServices } from "../utils.ts";
77

88
export default async function run(ctx: Context, actions: Action[], data: Run) {
99
let currentActionIndex = 0;
@@ -114,6 +114,11 @@ export default async function run(ctx: Context, actions: Action[], data: Run) {
114114
duration,
115115
});
116116
await ctx.kv.projects.updateStats(data.projectId);
117+
118+
if (actions.some((x) => x.useWasm)) {
119+
await stopServices(project?.path!);
120+
}
121+
117122
return;
118123
}
119124
}
@@ -159,6 +164,10 @@ export default async function run(ctx: Context, actions: Action[], data: Run) {
159164
})
160165
)
161166
);
167+
168+
if (actions.some((x) => x.useWasm)) {
169+
await stopServices(project?.path!);
170+
}
162171
}
163172

164173
async function spawn(

src/utils.ts

+39-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { dir, brightGreen, wait } from "../deps.ts";
1+
import { dir, brightGreen, wait, green, procfile } from "../deps.ts";
22

33
export async function isLogged(): Promise<boolean> {
44
if (Deno.env.get("FLUENTCI_ACCESS_TOKEN")) {
@@ -404,23 +404,28 @@ export async function bash(
404404
}
405405
}
406406

407-
export async function getProcfiles() {
407+
export async function getProcfiles(cwd = ".", exit = true): Promise<string[]> {
408408
const command = new Deno.Command("bash", {
409-
args: ["-c", "ls .fluentci/*/Procfile"],
409+
args: ["-c", "ls .fluentci/*/Procfile 2> /dev/null"],
410410
stdout: "piped",
411+
cwd,
411412
});
412413
const process = await command.spawn();
413414
const { stdout, success } = await process.output();
414415
if (!success) {
415416
console.log("No services running");
416-
Deno.exit(0);
417+
if (exit) Deno.exit(0);
418+
return [];
417419
}
418420

419421
const decoder = new TextDecoder();
420422
return decoder.decode(stdout).trim().split("\n");
421423
}
422424

423-
export async function getServicePid(name: string, socket: string) {
425+
export async function getServicePid(
426+
name: string,
427+
socket: string
428+
): Promise<string | undefined> {
424429
try {
425430
const response = await writeToSocket(socket, "status\n");
426431
const lines = response.replaceAll("\x00", "").trim().split("\n");
@@ -467,3 +472,32 @@ export async function writeToSocket(
467472
conn.close();
468473
return data;
469474
}
475+
476+
export async function stopServices(cwd: string) {
477+
const files = await getProcfiles(cwd, false);
478+
const services = [];
479+
// deno-lint-ignore no-explicit-any
480+
let infos: Record<string, any> = {};
481+
482+
for (const file of files) {
483+
const manifest = procfile.parse(Deno.readTextFileSync(file));
484+
services.push(...Object.keys(manifest));
485+
infos = {
486+
...infos,
487+
...manifest,
488+
};
489+
490+
for (const service of Object.keys(manifest)) {
491+
const socket = file.replace("Procfile", ".overmind.sock");
492+
493+
try {
494+
await writeToSocket(cwd + "/" + socket, "stop\n");
495+
} catch (_e) {
496+
console.log(`Failed to stop ${green(service)}`);
497+
continue;
498+
}
499+
500+
console.log(`Successfully stopped ${green(service)}`);
501+
}
502+
}
503+
}

0 commit comments

Comments
 (0)