-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathrestart.js
84 lines (72 loc) · 2.17 KB
/
restart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import fs from "fs";
import { spawn } from "child_process";
const pidFile = ".pid";
/**
* Function to terminate an existing child process based on PID file
*/
const terminateExistingProcess = () => {
if (fs.existsSync(pidFile)) {
const pid = parseInt(fs.readFileSync(pidFile, "utf-8").trim(), 10);
if (!isNaN(pid)) {
try {
process.kill(pid, "SIGTERM");
console.log(`Terminated previous process with PID: ${pid}`);
} catch (e) {
console.log(
`No previous process found or unable to terminate PID ${pid}: ${e.message}`
);
}
}
}
};
/**
* Function to start the new child process
* @returns {ChildProcess} - The spawned child process
*/
const startChildProcess = () => {
const child = spawn("node", ["dist/index.js"], {
stdio: "inherit"
});
child.on("spawn", () => {
console.log(`Started new process with PID: ${child.pid}`);
fs.writeFileSync(pidFile, String(child.pid));
});
child.on("error", (err) => {
console.error("Failed to start child process:", err);
});
child.on("exit", (code, signal) => {
console.log(`Child process exited with code ${code} and signal ${signal}`);
});
return child;
};
// Main execution flow
const main = async () => {
// Terminate any existing process from PID file
terminateExistingProcess();
// Start the new child process
const childProcess = startChildProcess();
// Handle termination signals
const handleExit = () => {
if (childProcess && childProcess.pid) {
console.log(`Terminating child process with PID: ${childProcess.pid}`);
try {
process.kill(childProcess.pid, "SIGTERM");
console.log("Child process terminated successfully.");
} catch (err) {
console.error("Error terminating child process:", err.message);
}
}
process.exit();
};
// Handle termination signals
process.on("SIGTERM", handleExit);
process.on("SIGINT", handleExit);
process.on("SIGHUP", handleExit);
process.on("SIGQUIT", handleExit);
process.on("SIGBREAK", handleExit);
};
// Execute the main function
main().catch((err) => {
console.error("Error in restart script:", err);
process.exit(1);
});