-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworker.js
More file actions
59 lines (49 loc) · 1.79 KB
/
worker.js
File metadata and controls
59 lines (49 loc) · 1.79 KB
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
'use strict';
const config = require('config');
const { getContext } = require('./context');
const { logger } = require('./logger');
const { startWork } = require('./workers/worker');
const { getWorkers } = require('./workers/worker-client');
const { getDB } = require('./data/db-client');
const killSignals = {
SIGHUP: 1,
SIGINT: 2,
SIGUSR2: 12,
SIGTERM: 15,
};
async function shutdown(workers, context, signal, value) {
context.logger.info(`Trying shutdown, got signal ${signal}`);
await workers.closeAll();
context.logger.info('Workers stopped.');
context.db.$pool.end();
context.logger.info('DB connections stopped.');
process.exit(128 + value);
}
function start(context) {
const workers = startWork(context);
context.logger.info(`Workers started`);
process.on('unhandledRejection', (error, promise) => {
context.logger.error(`unhandledRejection at: ${promise} `, {
stack: error.stack,
error: JSON.stringify(error),
});
});
process.on('uncaughtException', error => {
context.logger.error(`uncaughtException: ${error.message}`, {
stack: error.stack,
error: JSON.stringify(error),
});
});
process.once('SIGUSR2', () => shutdown(workers, context, 'SIGUSR2', killSignals.SIGUSR2));
process.once('SIGHUP', () => shutdown(workers, context, 'SIGHUP', killSignals.SIGHUP));
process.once('SIGINT', () => shutdown(workers, context, 'SIGINT', killSignals.SIGINT));
process.once('SIGTERM', () => shutdown(workers, context, 'SIGTERM', killSignals.SIGTERM));
return workers;
}
// First get context that waits for the DB to be available before starting the workers
getContext(config, logger, getDB(config.PG_CONNECTION), getWorkers(config))
.then(start)
.catch(error => {
logger.info(`Failed to workers: ${error.stack}`);
process.exit(1);
});