This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmartiesRunner.js
145 lines (131 loc) · 3.95 KB
/
SmartiesRunner.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var SmartiesCore = require("./src/SmartiesCore");
const events = require("events");
const colors = require("colors");
var core = null;
const SmartiesRunnerConstants = require("./SmartiesRunnerConstants");
const Logger = require("./src/logger/Logger");
const os = require("os");
const fs = require("fs-extra");
const childProcess = require("child_process");
const CHILD_PROCESS_STOP_SIGNAL = "SIGTERM";
const RESTART_DELAY = 5; // In seconds
const STOP_DELAY_S = 3; // In seconds
/**
* The runner class.
* @class
*/
class SmartiesRunner {
/**
* Constructor
*
* @returns {SmartiesRunner} The instance
*/
constructor() {
this.core = null;
this.runnerEventBus = new events.EventEmitter();
this.childPids = [];
const self = this;
this.runnerEventBus.on(SmartiesRunnerConstants.RESTART, () => {
setTimeout((me) => {
me.restart(me);
}, RESTART_DELAY * 1000, self);
});
try {
if (!process.env.TEST) {
try {
os.setPriority(process.pid, -20); // Highest priority
} catch(e) {
Logger.err(e);
}
process.title = "smarties.io";
}
this.start(this);
} catch (e) {
Logger.err(e);
fs.writeFileSync(SmartiesRunnerConstants.CRASH_FILE, JSON.stringify({message:e.message, stack:e.stack}));
self.childPids.forEach((childPid) => {
process.kill(childPid, CHILD_PROCESS_STOP_SIGNAL);
});
this.stop(this);
process.exit(1);
}
}
/**
* Start core
*
* @param {SmartiesRunner} self The instance
*/
start(self) {
if (!self.core) {
this.runnerEventBus.on(SmartiesRunnerConstants.PID_SPAWN, (pid) => {
try {
self.childPids.push(pid);
} catch (e) {
}
});
Logger.info("Starting runner");
self.core = new SmartiesCore.class(self.runnerEventBus, SmartiesRunnerConstants);
self.core.start();
}
}
/**
* Stop core
*
* @param {SmartiesRunner} self The instance
*/
stop(self) {
if (self.core) {
Logger.info("Stopping runner");
this.runnerEventBus.emit(SmartiesRunnerConstants.STOP);
self.core.stop();
self.childPids.forEach((childPid) => {
try {
process.kill(childPid, CHILD_PROCESS_STOP_SIGNAL);
} catch (e) {
}
});
self.runnerEventBus.eventNames().forEach((eventName) => {
if (eventName !== SmartiesRunnerConstants.RESTART && eventName !== SmartiesRunnerConstants.STOP) {
self.runnerEventBus.removeAllListeners(eventName);
}
});
self.core = null;
}
}
/**
* Restart core
*
* @param {SmartiesRunner} self The instance
*/
restart(self) {
self.stop(self);
self.start(self);
}
}
if (process.argv.length > 2) {
if (process.argv[2] == "create-plugin") {
require("./scripts/create-plugin");
} else if (process.argv[2] == "push-plugin") {
require("./scripts/push-plugin");
} else {
console.log(colors.red("Invalid command"));
}
} else {
const runner = new SmartiesRunner();
process.on("SIGINT", () => {
Logger.info("Received SIGINT");
runner.stop(runner);
process.kill(process.pid, "SIGKILL");
setTimeout(() => {
process.exit(0);
}, STOP_DELAY_S * 1000);
});
process.on("SIGTERM", () => {
Logger.info("Received SIGTERM");
runner.stop(runner);
process.kill(process.pid, "SIGKILL");
setTimeout(() => {
process.exit(0);
}, STOP_DELAY_S * 1000);
});
}