Skip to content

Commit

Permalink
Merge pull request #60 from fluentci-io/fix/clean-services
Browse files Browse the repository at this point in the history
fix: always stop services at the end of pipeline execution
  • Loading branch information
tsirysndr authored Jul 27, 2024
2 parents 42615af + 3818fd8 commit c68e717
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ Requirements:

**Latest (CLI):**

- `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)
- `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)
- `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)
- `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)

## ✨ Quick Start

Expand All @@ -110,7 +110,7 @@ fluentci studio
fluentci --help

Usage: fluentci [pipeline] [jobs...]
Version: 0.15.4
Version: 0.15.5

Description:

Expand Down
10 changes: 10 additions & 0 deletions src/cmd/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
getAccessToken,
getDaggerVersion,
isLogged,
stopServices,
} from "../utils.ts";
import { hostname, release, cpus, arch, totalmem, platform } from "node:os";
import { Action, Agent, Log, Run } from "../types.ts";
Expand Down Expand Up @@ -399,6 +400,11 @@ async function executeActions(
body: `fluentci_exit=1`,
headers,
}).catch((e) => logger.error(e.message));

if (actions.some((x) => x.use_wasm)) {
await stopServices(cwd);
}

return;
}
}
Expand Down Expand Up @@ -447,6 +453,10 @@ async function executeActions(
(e) => logger.error(e.message)
);

if (actions.some((x) => x.use_wasm)) {
await stopServices(cwd);
}

fetch(`${FLUENTCI_EVENTS_URL}?client_id=${clientId}`, {
method: "POST",
body: `fluentci_exit=0`,
Expand Down
2 changes: 1 addition & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { dir } from "../deps.ts";
export const VERSION = "0.15.4";
export const VERSION = "0.15.5";

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

Expand Down
11 changes: 10 additions & 1 deletion src/server/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Action } from "./graphql/objects/action.ts";
import { Run } from "./graphql/objects/run.ts";
import { createId, dayjs } from "../../deps.ts";
import { Log } from "./graphql/objects/log.ts";
import { sendSocketMessage } from "../utils.ts";
import { sendSocketMessage, stopServices } from "../utils.ts";

export default async function run(ctx: Context, actions: Action[], data: Run) {
let currentActionIndex = 0;
Expand Down Expand Up @@ -114,6 +114,11 @@ export default async function run(ctx: Context, actions: Action[], data: Run) {
duration,
});
await ctx.kv.projects.updateStats(data.projectId);

if (actions.some((x) => x.useWasm)) {
await stopServices(project?.path!);
}

return;
}
}
Expand Down Expand Up @@ -159,6 +164,10 @@ export default async function run(ctx: Context, actions: Action[], data: Run) {
})
)
);

if (actions.some((x) => x.useWasm)) {
await stopServices(project?.path!);
}
}

async function spawn(
Expand Down
44 changes: 39 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { dir, brightGreen, wait } from "../deps.ts";
import { dir, brightGreen, wait, green, procfile } from "../deps.ts";

export async function isLogged(): Promise<boolean> {
if (Deno.env.get("FLUENTCI_ACCESS_TOKEN")) {
Expand Down Expand Up @@ -404,23 +404,28 @@ export async function bash(
}
}

export async function getProcfiles() {
export async function getProcfiles(cwd = ".", exit = true): Promise<string[]> {
const command = new Deno.Command("bash", {
args: ["-c", "ls .fluentci/*/Procfile"],
args: ["-c", "ls .fluentci/*/Procfile 2> /dev/null"],
stdout: "piped",
cwd,
});
const process = await command.spawn();
const { stdout, success } = await process.output();
if (!success) {
console.log("No services running");
Deno.exit(0);
if (exit) Deno.exit(0);
return [];
}

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

export async function getServicePid(name: string, socket: string) {
export async function getServicePid(
name: string,
socket: string
): Promise<string | undefined> {
try {
const response = await writeToSocket(socket, "status\n");
const lines = response.replaceAll("\x00", "").trim().split("\n");
Expand Down Expand Up @@ -467,3 +472,32 @@ export async function writeToSocket(
conn.close();
return data;
}

export async function stopServices(cwd: string) {
const files = await getProcfiles(cwd, false);
const services = [];
// deno-lint-ignore no-explicit-any
let infos: Record<string, any> = {};

for (const file of files) {
const manifest = procfile.parse(Deno.readTextFileSync(file));
services.push(...Object.keys(manifest));
infos = {
...infos,
...manifest,
};

for (const service of Object.keys(manifest)) {
const socket = file.replace("Procfile", ".overmind.sock");

try {
await writeToSocket(cwd + "/" + socket, "stop\n");
} catch (_e) {
console.log(`Failed to stop ${green(service)}`);
continue;
}

console.log(`Successfully stopped ${green(service)}`);
}
}
}

0 comments on commit c68e717

Please sign in to comment.