|
| 1 | +import { brightGreen, gray, bold, procfile, Table, Cell } from "../../deps.ts"; |
| 2 | +import { getServicePid } from "../utils.ts"; |
| 3 | + |
| 4 | +export default async function status(name: string) { |
| 5 | + const command = new Deno.Command("bash", { |
| 6 | + args: ["-c", "ls .fluentci/*/Procfile"], |
| 7 | + stdout: "piped", |
| 8 | + }); |
| 9 | + const process = await command.spawn(); |
| 10 | + const { stdout, success } = await process.output(); |
| 11 | + if (!success) { |
| 12 | + console.log("No services running"); |
| 13 | + Deno.exit(0); |
| 14 | + } |
| 15 | + const decoder = new TextDecoder(); |
| 16 | + const files = decoder.decode(stdout).trim().split("\n"); |
| 17 | + const services = []; |
| 18 | + // deno-lint-ignore no-explicit-any |
| 19 | + let infos: Record<string, any> = {}; |
| 20 | + |
| 21 | + for (const file of files) { |
| 22 | + const manifest = procfile.parse(Deno.readTextFileSync(file)); |
| 23 | + services.push(...Object.keys(manifest)); |
| 24 | + infos = { |
| 25 | + ...infos, |
| 26 | + ...manifest, |
| 27 | + }; |
| 28 | + |
| 29 | + for (const service of Object.keys(manifest)) { |
| 30 | + infos[service].procfile = file; |
| 31 | + const socket = file.replace("Procfile", ".overmind.sock"); |
| 32 | + infos[service].socket = socket; |
| 33 | + const command = new Deno.Command("sh", { |
| 34 | + args: ["-c", `echo status | nc -U -w 1 ${socket}`], |
| 35 | + stdout: "piped", |
| 36 | + }); |
| 37 | + const process = await command.spawn(); |
| 38 | + const { stdout, success } = await process.output(); |
| 39 | + if (!success) { |
| 40 | + infos[service].status = "Stopped"; |
| 41 | + continue; |
| 42 | + } |
| 43 | + const decoder = new TextDecoder(); |
| 44 | + infos[service].status = decoder.decode(stdout).includes("running") |
| 45 | + ? "Up" |
| 46 | + : "Stopped"; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (!infos[name]) { |
| 51 | + console.log("Service not found in Procfile"); |
| 52 | + Deno.exit(1); |
| 53 | + } |
| 54 | + |
| 55 | + const pid = await getServicePid(name, infos[name].socket); |
| 56 | + |
| 57 | + console.log( |
| 58 | + `${infos[name].status === "Up" ? brightGreen("●") : "○"} ${name}` |
| 59 | + ); |
| 60 | + |
| 61 | + const table = new Table().body([ |
| 62 | + [ |
| 63 | + new Cell("Procfile:").align("right"), |
| 64 | + `${infos[name].procfile}\n└─ ${gray( |
| 65 | + infos[name].command + " " + infos[name].options.join(" ") |
| 66 | + )}`, |
| 67 | + ], |
| 68 | + [ |
| 69 | + new Cell("Active:").align("right"), |
| 70 | + infos[name].status === "Up" |
| 71 | + ? bold(brightGreen("active (running)")) |
| 72 | + : "inactive (dead)", |
| 73 | + ], |
| 74 | + [new Cell("Socket:").align("right"), infos[name].socket], |
| 75 | + [new Cell("Main PID:").align("right"), pid], |
| 76 | + [ |
| 77 | + new Cell("WorkDir:").align("right"), |
| 78 | + infos[name].socket.replace("/.overmind.sock", ""), |
| 79 | + ], |
| 80 | + ]); |
| 81 | + table.render(); |
| 82 | + console.log(""); |
| 83 | +} |
0 commit comments