Skip to content

Commit

Permalink
Merge pull request #57 from fluentci-io/fix/overmind-socket
Browse files Browse the repository at this point in the history
fix(services): connect to overmind unix domain socket using Deno
  • Loading branch information
tsirysndr authored Jul 24, 2024
2 parents 6c0a418 + 0f4bcb7 commit b6559c1
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 97 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.3_aarch64-apple-darwin.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.3/fluentci_v0.15.3_aarch64-apple-darwin.tar.gz) intel: [fluentci_v0.15.3_x86_64-apple-darwin.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.3/fluentci_v0.15.3_x86_64-apple-darwin.tar.gz)
- `Linux`: intel: [fluentci_v0.15.3_x86_64-unknown-linux-gnu.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.3/fluentci_v0.15.3_x86_64-unknown-linux-gnu.tar.gz) arm64: [fluentci_v0.15.3_aarch64-unknown-linux-gnu.tar.gz](https://github.com/fluentci-io/fluentci/releases/download/v0.15.3/fluentci_v0.15.3_aarch64-unknown-linux-gnu.tar.gz)
- `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)

## ✨ Quick Start

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

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

Description:

Expand Down
14 changes: 6 additions & 8 deletions src/cmd/down.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { green, procfile } from "../../deps.ts";
import { getProcfiles } from "../utils.ts";
import { getProcfiles, writeToSocket } from "../utils.ts";

export default async function down() {
const files = await getProcfiles();
Expand All @@ -18,16 +18,14 @@ export default async function down() {
for (const service of Object.keys(manifest)) {
const socket = file.replace("Procfile", ".overmind.sock");
infos[service].socket = socket;
const command = new Deno.Command("sh", {
args: ["-c", `echo stop | nc -U -w 1 ${socket}`],
stdout: "piped",
});
const process = await command.spawn();
const { success } = await process.output();
if (!success) {

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

console.log(`Successfully stopped ${green(service)}`);
}
}
Expand Down
13 changes: 4 additions & 9 deletions src/cmd/echo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { green, procfile } from "../../deps.ts";
import { getProcfiles } from "../utils.ts";
import { getProcfiles, writeToSocket } from "../utils.ts";

export default async function echo(name: string) {
const files = await getProcfiles();
Expand Down Expand Up @@ -27,14 +27,9 @@ export default async function echo(name: string) {
}

const socket = infos[name].socket;
const command = new Deno.Command("sh", {
args: ["-c", `echo echo | nc -U ${socket}`],
stdout: "inherit",
stderr: "inherit",
});
const process = await command.spawn();
const { success } = await process.output();
if (!success) {
try {
await writeToSocket(socket, "echo\n", true);
} catch (_e) {
console.log(`Failed to stream logs for ${green(name)}`);
Deno.exit(1);
}
Expand Down
17 changes: 5 additions & 12 deletions src/cmd/ps.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { procfile, Table } from "../../deps.ts";
import { writeToSocket } from "../utils.ts";
import { getProcfiles, getServicePid } from "../utils.ts";

export default async function listServices() {
Expand All @@ -18,20 +19,12 @@ export default async function listServices() {
for (const service of Object.keys(manifest)) {
const socket = file.replace("Procfile", ".overmind.sock");
infos[service].socket = socket;
const command = new Deno.Command("sh", {
args: ["-c", `echo status | nc -U -w 1 ${socket}`],
stdout: "piped",
});
const process = await command.spawn();
const { stdout, success } = await process.output();
if (!success) {
try {
const response = await writeToSocket(socket, "status\n");
infos[service].status = response.includes("running") ? "Up" : "Stopped";
} catch (_e) {
infos[service].status = "Stopped";
continue;
}
const decoder = new TextDecoder();
infos[service].status = decoder.decode(stdout).includes("running")
? "Up"
: "Stopped";
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/cmd/restart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { green, procfile } from "../../deps.ts";
import { getProcfiles } from "../utils.ts";
import { getProcfiles, writeToSocket } from "../utils.ts";

export default async function restart(name: string) {
const files = await getProcfiles();
Expand Down Expand Up @@ -27,13 +27,9 @@ export default async function restart(name: string) {
}

const socket = infos[name].socket;
const command = new Deno.Command("sh", {
args: ["-c", `echo restart | nc -U -w 1 ${socket}`],
stdout: "piped",
});
const process = await command.spawn();
const { success } = await process.output();
if (!success) {
try {
await writeToSocket(socket, "restart\n");
} catch (_e) {
console.log(`Failed to restart ${green(name)}`);
return;
}
Expand Down
22 changes: 9 additions & 13 deletions src/cmd/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { brightGreen, gray, bold, procfile, Table, Cell } from "../../deps.ts";
import { getServicePid } from "../utils.ts";
import { getServicePid, writeToSocket } from "../utils.ts";

export default async function status(name: string) {
const command = new Deno.Command("bash", {
Expand Down Expand Up @@ -30,20 +30,16 @@ export default async function status(name: string) {
infos[service].procfile = file;
const socket = file.replace("Procfile", ".overmind.sock");
infos[service].socket = socket;
const command = new Deno.Command("sh", {
args: ["-c", `echo status | nc -U -w 1 ${socket}`],
stdout: "piped",
});
const process = await command.spawn();
const { stdout, success } = await process.output();
if (!success) {
try {
const response = await writeToSocket(socket, "status\n");
if (!response.includes("running")) {
infos[service].status = "Stopped";
continue;
}
} catch (_e) {
infos[service].status = "Stopped";
continue;
}
const decoder = new TextDecoder();
infos[service].status = decoder.decode(stdout).includes("running")
? "Up"
: "Stopped";
infos[service].status = "Up";
}
}

Expand Down
12 changes: 4 additions & 8 deletions src/cmd/stop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { green, procfile } from "../../deps.ts";
import { getProcfiles } from "../utils.ts";
import { getProcfiles, writeToSocket } from "../utils.ts";

export default async function stop(name: string) {
const files = await getProcfiles();
Expand Down Expand Up @@ -27,13 +27,9 @@ export default async function stop(name: string) {
}

const socket = infos[name].socket;
const command = new Deno.Command("sh", {
args: ["-c", `echo stop | nc -U -w 1 ${socket}`],
stdout: "piped",
});
const process = await command.spawn();
const { success } = await process.output();
if (!success) {
try {
await writeToSocket(socket, "stop\n");
} catch (_e) {
console.log(`Failed to stop ${green(name)}`);
return;
}
Expand Down
12 changes: 4 additions & 8 deletions src/cmd/up.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { green, procfile } from "../../deps.ts";
import { getProcfiles } from "../utils.ts";
import { getProcfiles, writeToSocket } from "../utils.ts";

export default async function up() {
const files = await getProcfiles();
Expand All @@ -19,13 +19,9 @@ export default async function up() {
const socket = file.replace("Procfile", ".overmind.sock");

infos[service].socket = socket;
const command = new Deno.Command("sh", {
args: ["-c", `echo restart | nc -U -w 1 ${socket}`],
stdout: "piped",
});
const process = await command.spawn();
const { success } = await process.output();
if (!success) {
try {
await writeToSocket(socket, "restart\n");
} catch (_e) {
console.log(`Failed to start ${green(service)}`);
continue;
}
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.3";
export const VERSION = "0.15.4";

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

Expand Down
67 changes: 40 additions & 27 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,36 +421,49 @@ export async function getProcfiles() {
}

export async function getServicePid(name: string, socket: string) {
const command = new Deno.Command("sh", {
args: ["-c", `echo status | nc -U -w 1 ${socket}`],
stdout: "piped",
});
const process = await command.spawn();
const { stdout } = await process.output();
const decoder = new TextDecoder();
const lines = decoder.decode(stdout).trim().split("\n");
return lines
.find((line) => line.startsWith(name + " "))
?.split(" ")
?.filter((x) => x)[1];
try {
const response = await writeToSocket(socket, "status\n");
const lines = response.replaceAll("\x00", "").trim().split("\n");
return lines
.find((line) => line.startsWith(name + " "))
?.split(" ")
?.filter((x) => x)[1];
} catch (_e) {
return "";
}
}

export async function startOvermind(cwd: string) {
const command = new Deno.Command("sh", {
args: [
"-c",
`[ -S .overmind.sock ] || overmind start -f Procfile --daemonize`,
],
stdout: "inherit",
stderr: "inherit",
cwd,
});
export async function writeToSocket(
socket: string,
message: string,
stream = false
): Promise<string> {
const conn = await Deno.connect({ transport: "unix", path: socket });
await conn.write(new TextEncoder().encode(message));

if (["stop", "restart"].includes(message.trim())) {
conn.close();
return "";
}

const process = await command.spawn();
const { code } = await process.status;
if (stream) {
while (true) {
const buf = new Uint8Array(1024);
const bytesRead = await conn.read(buf);
if (bytesRead === null) break;
console.log(new TextDecoder().decode(buf));
}
conn.close();
return "";
}

if (code !== 0) {
console.log("Failed to start Overmind.");
Deno.exit(1);
let data = "";
while (true) {
const buf = new Uint8Array(1024);
const bytesRead = await conn.read(buf);
if (bytesRead === null) break;
data += new TextDecoder().decode(buf);
}
conn.close();
return data;
}

0 comments on commit b6559c1

Please sign in to comment.