forked from metarhia/impress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
impress.js
222 lines (195 loc) · 6.95 KB
/
impress.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
'use strict';
process.title = 'impress';
const fsp = require('fs').promises;
const { Worker } = require('worker_threads');
const path = require('path');
const { Config } = require('metaconfiguration');
const metavm = require('metavm');
const metautil = require('metautil');
const { loadSchema } = require('metaschema');
const { Logger } = require('metalog');
const { Planner } = require('./lib/planner.js');
const CONFIG_SECTIONS = ['log', 'scale', 'server', 'sessions'];
const PATH = process.cwd();
const WORKER_PATH = path.join(__dirname, 'lib/worker.js');
const CFG_PATH = path.join(PATH, 'application/config');
const LOG_PATH = path.join(PATH, 'log');
const CTRL_C = 3;
const LOG_OPTIONS = { path: LOG_PATH, home: PATH, workerId: 0 };
const CONTEXT = metavm.createContext({ process });
const CFG_OPTIONS = { mode: process.env.MODE, context: CONTEXT };
const impress = {
logger: null,
config: null,
planner: null,
finalized: () => {},
finalization: false,
initialization: true,
console,
applications: new Map(),
lastWorkerId: 0,
startTimer: null,
};
const exit = async (message) => {
impress.console.info(message);
if (impress.logger && impress.logger.active) await impress.logger.close();
process.exit(1);
};
const logError = (type) => (err) => {
const msg = err?.stack || err?.message || 'exit';
impress.console.error(`${type}: ${msg}`);
if (impress.finalization) return;
if (impress.initialization) exit('Can not start Application server');
};
process.on('uncaughtException', logError('Uncaught exception'));
process.on('warning', logError('Warning'));
process.on('unhandledRejection', logError('Unhandled rejection'));
const startWorker = async (app, kind, port, id = ++impress.lastWorkerId) => {
const workerData = { id, kind, path: app.path, port };
const options = { trackUnmanagedFds: true, workerData };
const worker = new Worker(WORKER_PATH, options);
if (kind === 'worker') {
app.pool.add(worker);
await app.pool.capture();
}
app.threads.set(id, worker);
worker.on('exit', (code) => {
if (code !== 0) startWorker(app, kind, port, id);
else app.threads.delete(id);
if (impress.initialization) exit('Can not start Application server');
if (app.threads.size === 0) {
impress.applications.delete(app.path);
if (impress.applications.size === 0) impress.finalized();
}
});
const handlers = {
started: ({ kind }) => {
app.ready++;
if (kind === 'worker') app.pool.release(worker);
if (app.threads.size === app.ready) {
clearTimeout(impress.startTimer);
impress.initialization = false;
impress.console.info(`App started: ${app.path}`);
}
},
task: async ({ action, port, task }) => {
const { planner } = impress;
task.app = app.path;
if (action === 'add') port.postMessage({ id: await planner.add(task) });
else if (action === 'remove') planner.remove(task.id);
else if (action === 'stop') planner.stop(task.name);
},
invoke: async (msg) => {
const { status, port, exclusive } = msg;
if (status === 'done') {
app.pool.release(worker);
return;
}
const promisedThread = exclusive ? app.pool.capture() : app.pool.next();
const next = await promisedThread.catch(() => {
const error = new Error('No thread available');
port.postMessage({ name: 'error', error });
return null;
});
if (!next) return;
next.postMessage(msg, [port]);
},
};
worker.on('message', (msg) => {
const handler = handlers[msg.name];
if (handler) handler(msg);
});
};
const validateConfig = async (config) => {
let valid = true;
const schemaPath = path.join(__dirname, 'schemas/config');
for (const section of CONFIG_SECTIONS) {
const fileName = path.join(schemaPath, section + '.js');
const schema = await loadSchema(fileName);
const checkResult = schema.check(config[section]);
if (!checkResult.valid) {
for (const err of checkResult.errors) {
impress.console.error(`${err} in application/config/${section}.js`);
}
valid = false;
}
}
if (!valid) exit('Application server configuration is invalid');
};
const loadApplication = async (root) => {
impress.console.info(`Start: ${root}`);
const configPath = path.join(root, 'application/config');
const config = await new Config(configPath, CFG_OPTIONS).catch((err) => {
exit(`Can not read configuration: ${CFG_PATH}\n${err.stack}`);
});
await validateConfig(config);
const { balancer, ports = [], workers = {} } = config.server;
const threads = new Map();
const pool = new metautil.Pool({ timeout: workers.wait });
const app = { path: root, config, threads, pool, ready: 0 };
if (balancer) await startWorker(app, 'balancer', balancer);
for (const port of ports) await startWorker(app, 'server', port);
const poolSize = workers.pool || 0;
for (let i = 0; i < poolSize; i++) await startWorker(app, 'worker');
impress.applications.set(root, app);
};
const loadApplications = async () => {
const list = await fsp
.readFile('.applications', 'utf8')
.then((data) => data.split('\n').filter((s) => s.length !== 0))
.catch(() => [PATH]);
for (const path of list) {
await loadApplication(path);
}
};
const stopApplication = (root) => {
const app = impress.applications.get(root);
for (const thread of app.threads.values()) {
thread.postMessage({ name: 'stop' });
}
};
const stop = async () => {
impress.finalization = true;
const logClosed = impress.logger.close();
const portsClosed = new Promise((resolve) => {
impress.finalized = resolve;
setTimeout(() => {
impress.console.error('Exit with graceful shutdown timeout');
resolve();
}, impress.config.server.timeouts.stop);
});
for (const app of impress.applications.values()) {
stopApplication(app.path);
}
await Promise.allSettled([logClosed, portsClosed]);
exit('Application server stopped');
};
(async () => {
const configPath = path.join(PATH, 'application/config');
const config = await new Config(configPath, CFG_OPTIONS).catch((err) => {
exit(`Can not read configuration: ${CFG_PATH}\n${err.stack}`);
});
await validateConfig(config);
impress.config = config;
const logger = await new Logger({ ...LOG_OPTIONS, ...config.log });
logger.on('error', logError('Logger'));
if (logger.active) impress.console = logger.console;
impress.logger = logger;
const tasksPath = path.join(PATH, 'application/tasks');
const tasksConfig = config.server.scheduler;
impress.planner = await new Planner(tasksPath, tasksConfig, impress);
process.on('SIGINT', stop);
process.on('SIGTERM', stop);
impress.startTimer = setTimeout(
logError('Initialization timeout'),
config.server.timeouts.start,
);
await loadApplications();
if (process.stdin.isTTY) {
process.stdin.setRawMode(true);
process.stdin.on('data', (data) => {
const key = data[0];
if (key === CTRL_C) stop();
});
}
})().catch(logError('Initialization'));