This repository has been archived by the owner on Sep 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
scales.js
111 lines (99 loc) · 2.75 KB
/
scales.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
#!/usr/bin/env node
/**
* Scales.js — the flexible game management daemon built for PufferPanel.
* Licensed under a GPL-v3 license.
*/
require('colors');
var Proc = require('child_process');
var Fs = require('fs-extra');
var getRunningPid = function (callback) {
Fs.readFile(__dirname + '/scales.pid', {
encoding: 'utf-8'
}, function (err, text) {
if (err) {
return callback(err);
}
try {
var pid = parseInt(text);
process.kill(parseInt(pid), 0);
return callback(undefined, pid);
} catch (e) {
return callback(e);
}
});
};
switch (process.argv[2]) {
case 'start':
{
console.log('Starting Scales');
getRunningPid(function (err, pid) {
if (pid) {
console.log('Process already running: ' + pid);
process.exit(1);
}
Proc.fork(__dirname + '', {
env: process.env
});
});
break;
}
case 'stop':
{
console.log('Stopping Scales');
getRunningPid(function (err, pid) {
if (err) {
console.error('Error reading PID, was Scales even running?', err.message);
}
process.kill(pid, 'SIGINT');
var waiting = function () {
try {
process.kill(pid, 0);
process.stdout.write('.');
setTimeout(waiting, 50);
} catch (e) {
process.stdout.write('\n');
}
};
waiting();
});
break;
}
case 'start':
{
console.log('Starting Scales');
Proc.fork(__dirname, {
env: process.env
});
break;
}
case 'debug':
{
console.log('Starting Scales with debug view');
Proc.fork(__dirname, ['--consoleLevel=verbose', '--showMeta', '--nodaemon'], {
env: process.env
});
break;
}
case 'status':
{
getRunningPid(function (err, pid) {
if (err) {
console.log('Scales is not running');
} else {
console.log('Scales is running (pid: ' + pid + ')');
}
});
break;
}
default:
{
console.log('Scales - PufferPanel Daemon');
console.log('Usage:');
console.log('./scales {start|stop|status|debug}');
console.log(' start - Starts the Scales service');
console.log(' stop - Stops the Scales service');
console.log(' status - Shows the status of the Scales service');
console.log(' debug - Run the Scales service in a debug view');
break;
}
}