-
Notifications
You must be signed in to change notification settings - Fork 10
/
auto-restart-controller.js
52 lines (45 loc) · 1.39 KB
/
auto-restart-controller.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
const {fork} = require('child_process');
const path = require('path');
const fs = require('fs');
let shouldAutoRestart;
const configFilePath = path.join(__dirname, './build/nodeConfig.json');
// Function to update shouldAutoRestart from the config file
const updateAutoRestartStatus = () => {
try {
const configFileContent = fs.readFileSync(configFilePath).toString();
const config = JSON.parse(configFileContent);
shouldAutoRestart = config.autoRestart;
} catch (error) {
console.error(`Error reading or parsing the config file: ${error}`);
}
};
// Initialize shouldAutoRestart
updateAutoRestartStatus();
// Watch the config file for changes
fs.watch(configFilePath, (eventType, filename) => {
if (eventType === 'change') {
console.log(
`${filename} has been updated. Updating auto-restart status...`
);
updateAutoRestartStatus();
}
});
const startApp = () => {
const app = fork(path.join(__dirname, '../validator/dist/src/index.js'), {
cwd: path.join(__dirname, './build'),
});
app.on('exit', code => {
console.log(
`Child process exited with code ${code}. shouldAutoRestart: `,
shouldAutoRestart
);
if (shouldAutoRestart && code === 0) {
console.log('Restarting validator');
startApp();
} else {
// Propagate the child process exit code to the main process
process.exit(code);
}
});
};
startApp();