-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
144 lines (129 loc) · 4.16 KB
/
server.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
const _ = require('lodash')
, low = require('lowdb')
, fs = require('fs')
, child_process = require('child_process')
, debug = require('debug')('acs-main:debug')
, info = require('debug')('acs-main:info');
process.setMaxListeners(0);
var plugins = {};
var config = low('config.json', {storage: require('lowdb/lib/file-sync')});
var db = low('data.json', {storage: require('lowdb/lib/file-sync')});
db.defaults({ drivers: [] }).value();
/* check exists results */
try {
if(!fs.statSync('results').isDirectory()) {
fs.mkdir('results', 0o744);
}
} catch(e) {
if(e.code === 'ENOENT') {
fs.mkdir('results', 0o744);
}
}
/* Run http server */
var http_listen_port = (config.get('http_listen_port').cloneDeep().value() || 3000);
var webServer = (function httpServer(port, _handle_http_server) {
return child_process.fork("httpserver.js", [port])
.on("message", _handle_http_server)
.on("exit", (code, signal) => {
if(Number(code) > 0) {
info("Restarting Http Server (%s, %s)", code, signal);
webServer = httpServer(port, _handle_http_server);
}
});
})((http_listen_port || 3000), function(message) {
if(typeof message !== 'object' || typeof message.command !== 'string') return;
switch(message.command) {
case "start_plugin":
info("Run ACS Plug-in");
start_plugin();
break;
case "stop_plugin":
info("Stop ACS Plug-in");
stop_plugin();
break;
case "config":
info("Update ACSP Config");
_.forEach(message.config, function(conf, key) {
config.set(key, conf).value();
});
break;
default:
debug("Unknown command : %s", message.command);
break;
}
});
var start_plugin = function() {
// plugins are already running.
if(_.keys(plugins).length > 0) {
return;
}
var event_config = {
title : config.get('event_title').cloneDeep().value(),
welcome_message : (config.get('welcome_message').cloneDeep().value() || '').split('\n')
};
var pconfs = config.get('plugins').cloneDeep().value();
for(var i in pconfs) {
var plugin_config = pconfs[i];
plugin_config.monitor_port = http_listen_port;
plugin_config.event_title = event_config.title;
plugin_config.welcome_message = _.cloneDeep(event_config.welcome_message)
.concat(plugin_config.welcome_message.split('\n'))
.filter(function(value) { return value.trim() !== ''; })
.map(function(value) { return value.trim(); });
launch_plugin(plugin_config);
}
};
var launch_plugin = function(options) {
var plugin = child_process.fork('plugin.js')
.on('message', function(message) {
if(typeof message !== 'object' || typeof message.command !== 'string') return;
switch(message.command) {
case "add_driver_info":
var driver_info = message.data;
var find = db.get('drivers').find({ guid: driver_info.guid }).value();
if(typeof find === 'undefined') {
driver_info = _.assign(driver_info, { ballast: 0 });
db.get('drivers').push(driver_info).value();
} else {
driver_info = db.get('drivers').find({ guid: driver_info.guid }).assign(driver_info).value();
}
break;
case "driver_info":
var guid = message.data;
var driver_info = db.get('drivers').find({ guid: guid }).value();
this.send({ command: 'driver_info', data: driver_info });
break;
}
})
.on('exit', function(code, signal) {
info("Plug-in process closed. (PID : %s, Code : %d, Signal : %s)", this.pid, code, signal);
if(Number(code) > 0) {
info("Abnormal plug-in termination. Relaunch an application.");
var plugin = plugins[this.pid];
launch_plugin(_.cloneDeep(plugin.options));
}
delete plugins[this.pid];
});
plugin.send({ command: 'start_plugin', options: options });
plugins[plugin.pid] = { options: options, process: plugin };
info("Plug-in running on PID (%d)", plugin.pid);
return plugin;
}
var stop_plugin = function() {
var pids = Object.keys(plugins);
for(var pid in pids) {
var plugin = plugins[pids[pid]];
plugin.process.kill('SIGTERM');
}
}
// Process SIGNAL Event Listening
process.on('SIGINT', function() {
(function(callback) {
callback();
setInterval(callback, 1000);
}(function() {
if(Object.keys(plugins).length === 0) {
process.exit();
}
}));
});