-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from vivid-planet/refactor/extract-commands
Refactor: extract commands
- Loading branch information
Showing
10 changed files
with
206 additions
and
198 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface AppDefinition { | ||
name: string; | ||
script: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
] | ||
}; |
Oops, something went wrong.