Skip to content

Commit

Permalink
Merge pull request #2 from vivid-planet/refactor/extract-commands
Browse files Browse the repository at this point in the history
Refactor: extract commands
  • Loading branch information
Fabian-Fynn authored Feb 21, 2022
2 parents 81a336f + d3f1213 commit d819855
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 198 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/app-definition.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface AppDefinition {
name: string;
script: string;
}
5 changes: 5 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export { start } from "./start.command";
export { shutdown } from "./shutdown.command";
export { status } from "./status.command";
export { restart } from "./restart.command";
export { logs } from "./logs.command";
18 changes: 18 additions & 0 deletions src/commands/logs.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createConnection } from "net";

export const logs = async (name?: string) => {
const client = createConnection(".pm.sock")
client.on('connect', () => {
if (name) {
client.write("logs " + name);
} else {
//all logs
client.write("logs");
}

});
client.on("data", (data) => {
//TODO handle stderr/stdin and also write on stderr
process.stdout.write(data);
})
}
13 changes: 13 additions & 0 deletions src/commands/restart.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { createConnection } from "net";

export const restart = async (name: string) => {
const client = createConnection(".pm.sock")
client.on('connect', () => {
client.write("restart " + name);
});
client.on("data", (data) => {
//TODO handle stderr/stdin and also write on stderr
process.stdout.write(data);
})
}

8 changes: 8 additions & 0 deletions src/commands/shutdown.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createConnection } from "net";

export const shutdown = async () => {
const client = createConnection(".pm.sock")
client.on('connect', () => {
client.write("shutdown");
});
}
129 changes: 129 additions & 0 deletions src/commands/start.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { spawn, ChildProcess } from "child_process";
import { Socket, createServer, createConnection } from "net";
import { AppDefinition } from "../app-definition.type";

export const start = async (apps: AppDefinition[]) => {
const processes: { [key: string]: ChildProcess } = {};
const logSockets: { socket: Socket, name: string | null; }[] = [];
let shuttingDown = false;

function startProcess(app: AppDefinition) {
console.log("starting " + app.script);
const p = spawn("bash", ["-c", app.script]);
p.stdout.on('data', data => {
process.stdout.write(data);
logSockets.forEach(s => {
if (!s.name || s.name == app.name) {
s.socket.write(data);
}
});
});
p.stderr.on('data', data => {
process.stderr.write(data);
logSockets.forEach(s => {
if (!s.name || s.name == app.name) {
s.socket.write(data);
}
});
});
p.on('close', code => {
if (!shuttingDown) {
console.error("process stopped", app.name, ", restarting");
startProcess(app);
}
})
p.on('error', (err) => {
// TODO handle
console.error(err);
console.error("Failed starting process", app.name);
});
processes[app.name] = p;
}

const server = createServer();
server.listen(".pm.sock");
server.on('connection', (s) => {
s.on('data', async (command) => {
const cmd = command.toString();
if (cmd == "logs" || cmd.startsWith("logs ")) {
const name = cmd != "logs" ? cmd.substring(5) : null; // null means all
if (name && !apps.find((app) => app.name === name)) {
console.error("Unknown name");
} else {
logSockets.push({ socket: s, name });
s.on('close', () => {
var index = logSockets.findIndex(i => i.socket == s);
if (index !== -1) {
logSockets.splice(index, 1);
}
});
}
} else if (cmd.startsWith("restart ")) {
const name = cmd.substring(8);
const p = processes[name];
if (!p) {
console.error("Unknown name");
s.end();
return;
}

if (!p.killed) {
console.log("killing " + name);
p.kill("SIGINT");
while (!p.killed) {
console.log("waiting for killed");
await new Promise(r => setTimeout(r, 100));
}
}

const app = apps.find(i => i.name == name);
if (!app) {
console.error("Unknown name");
s.end();
return;
}
startProcess(app);
s.end();

} else if (cmd == "status") {
const response = Object.keys(processes).map(name => {
const p = processes[name];
return {
name,
running: !p.killed
}
});
console.log("sending status reponse", response);
s.write(JSON.stringify(response));
s.end();
} else if (cmd == "shutdown") {
console.log("shutting down");
shuttingDown = true;
Object.values(processes).forEach(p => {
if (!p.killed) p.kill("SIGINT");
})
server.close();
process.exit();
} else {
console.error("Unknown command", cmd);
}
});
});

apps.forEach(app => {
startProcess(app);
});

process.on("SIGINT", function () {
console.log("shutting down")
server.close();
shuttingDown = true;
for (const name in processes) {
const p = processes[name];
if (!p.killed) {
console.log("killing " + name);
p.kill("SIGINT");
}
}
});
}
11 changes: 11 additions & 0 deletions src/commands/status.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createConnection } from "net";

export const status = async () => {
const client = createConnection(".pm.sock")
client.on('connect', () => {
client.write("status");
});
client.on("data", (data) => {
console.log(data.toString());
})
}
12 changes: 12 additions & 0 deletions src/ecosystem.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default {
apps: [
{
name: "sleep60",
script: "echo sleep60 && sleep 60",
},
{
name: "sleep3",
script: "while true; do echo sleep3 && sleep 3; done",
},
]
};
Loading

0 comments on commit d819855

Please sign in to comment.