-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
127 lines (107 loc) · 3.52 KB
/
index.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const { spawn } = require("child_process");
const apps = require("./apps.json");
const processList = [];
const startChildProcess = async (executable, name) => {
return new Promise((resolve, reject) => {
const childProcess = spawn(executable, [], {
detached: true,
stdio: ["ignore", "pipe", "pipe"], // Capture stdout and stderr
});
childProcess.stdout.on("data", (data) => {
// Display stdout of child process in the main process
console.log(`[${name}] ${data.toString().trim()}`);
});
childProcess.stderr.on("data", (data) => {
// Handle stderr if needed
if (
// this warning doesnt mean anything to us and we want to hide it
!data.toString().trim().includes("(sequalize) Warning: Model attributes")
) {
console.error(`[${name}] Error: ${data.toString().trim()}`);
}
});
childProcess.on("error", (err) => {
console.error(`Error starting ${name}:`, err);
reject(err);
});
childProcess.on("exit", (code, signal) => {
console.log(`${name} exited with code ${code} and signal ${signal}`);
if (code !== 0) {
// Restart the process because it stopped unexpectedly
console.log(`UNEXPECTED EXIT: Restarting ${name}...`);
startChildProcess(executable, name);
}
resolve();
});
childProcess.unref(); // Unreference the child process to allow the main process to exit independently
processList.push(childProcess); // Store child process in the list
});
};
const startChildProcesses = async () => {
// Process "cadt" first
if (apps.hasOwnProperty("cadt")) {
try {
startChildProcess(`apps/${apps["cadt"]}`, "cadt");
await new Promise((resolve) => setTimeout(resolve, 5000));
} catch (error) {
console.error(error.message);
}
}
// Process "climate_explorer" second
if (apps.hasOwnProperty("climate_explorer")) {
try {
startChildProcess(`apps/${apps["climate_explorer"]}`, "climate_explorer");
await new Promise((resolve) => setTimeout(resolve, 5000));
} catch (error) {
console.error(error.message);
}
}
if (apps.hasOwnProperty("climate_token_driver")) {
try {
startChildProcess(
`apps/${apps["climate_token_driver"]}`,
"climate_token_driver"
);
await new Promise((resolve) => setTimeout(resolve, 5000));
} catch (error) {
console.error(error.message);
}
}
// Process the remaining keys
for (const name of Object.keys(apps)) {
if (
name === "cadt" ||
name === "climate_explorer" ||
name === "climate_token_driver"
) {
continue; // Skip if already processed
}
const executable = `apps/${apps[name]}`;
try {
startChildProcess(executable, name);
await new Promise((resolve) => setTimeout(resolve, 5000));
} catch (error) {
console.error(error.message);
}
}
};
process.on("SIGINT", () => {
console.log("Received SIGINT signal. Terminating child processes...");
// Terminate child processes
for (const childProcess of processList) {
childProcess.kill("SIGINT"); // Send SIGINT to child process
}
// Give some time for child processes to exit gracefully
setTimeout(() => {
process.exit(0); // Exit the main process
}, 5000); // Adjust the delay as needed
});
// Listen for the exit event of the main process
process.on("exit", () => {
console.log("Exiting...");
// Terminate all child processes
for (const childProcess of processList) {
childProcess.kill();
}
});
startChildProcesses();