backend-manager 3.2.178 → 3.2.180
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,36 +1,49 @@
|
|
|
1
1
|
const powertools = require('node-powertools');
|
|
2
2
|
const yargs = require('yargs/yargs');
|
|
3
3
|
const { hideBin } = require('yargs/helpers');
|
|
4
|
+
const chalk = require('chalk');
|
|
4
5
|
|
|
5
|
-
function ServerManager(command) {
|
|
6
|
+
function ServerManager(command, options) {
|
|
7
|
+
const self = this;
|
|
8
|
+
|
|
9
|
+
// Set the command
|
|
10
|
+
self.command = command;
|
|
11
|
+
self.options = options;
|
|
6
12
|
}
|
|
7
13
|
|
|
8
14
|
ServerManager.prototype.monitor = function (command, options) {
|
|
9
15
|
const self = this;
|
|
10
16
|
|
|
11
17
|
return new Promise(async function(resolve, reject) {
|
|
18
|
+
// Use instance properties if arguments are not provided
|
|
19
|
+
command = command || self.command;
|
|
20
|
+
options = options || self.options;
|
|
21
|
+
|
|
22
|
+
// Set the command
|
|
23
|
+
command = command || yargs(hideBin(process.argv)).argv.command || 'npm start';
|
|
24
|
+
|
|
12
25
|
// Set the options
|
|
13
26
|
options = options || {};
|
|
14
27
|
options.log = options.log === undefined ? true : options.log;
|
|
15
28
|
|
|
16
|
-
// Set the command
|
|
17
|
-
self.command = command || yargs(hideBin(process.argv)).argv.command || 'npm start';
|
|
18
|
-
|
|
19
29
|
// Log
|
|
20
|
-
|
|
30
|
+
log(`Starting server (command=${command})`);
|
|
21
31
|
|
|
22
32
|
// Start the server
|
|
23
|
-
await powertools.execute(
|
|
33
|
+
await powertools.execute(command, options, (serverProcess) => {
|
|
24
34
|
serverProcess.on('exit', (code) => {
|
|
35
|
+
const string = `(code=${code}, timestamp=${new Date().toISOString()})`;
|
|
25
36
|
if (code !== 0) {
|
|
26
|
-
|
|
37
|
+
error(`Server crashed (${string})`);
|
|
38
|
+
error(`Restarting in 1 second...`);
|
|
27
39
|
|
|
28
40
|
// Restart the server
|
|
29
41
|
setTimeout(function () {
|
|
42
|
+
error(`Restarting server...`);
|
|
30
43
|
self.monitor();
|
|
31
44
|
}, 1000);
|
|
32
45
|
} else {
|
|
33
|
-
|
|
46
|
+
log(`Server stopped safely (${string})`);
|
|
34
47
|
}
|
|
35
48
|
});
|
|
36
49
|
})
|
|
@@ -39,6 +52,14 @@ ServerManager.prototype.monitor = function (command, options) {
|
|
|
39
52
|
});
|
|
40
53
|
};
|
|
41
54
|
|
|
55
|
+
function log() {
|
|
56
|
+
console.log(chalk.blue('[ServerManager]:'), ...arguments);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function error() {
|
|
60
|
+
console.error(chalk.red('[ServerManager]:'), ...arguments);
|
|
61
|
+
}
|
|
62
|
+
|
|
42
63
|
// Check if the script is run directly from the command line
|
|
43
64
|
if (require.main === module) {
|
|
44
65
|
const manager = new ServerManager();
|