-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathworker.ts
executable file
·51 lines (47 loc) · 1.84 KB
/
worker.ts
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
import "./config/env"; // Important to be before other dependencies
import throng from "throng";
import { isNodeProd } from "utils/is-node-env";
import { listenToMicrosLifecycle } from "./services/micros/lifecycle";
import { ClusterCommand, sendCommandToCluster } from "./services/cluster/send-command";
import { createWorkerServerApp } from "./worker/app";
import { initializeSentry } from "./config/sentry";
import { listenForClusterCommand } from "./services/cluster/listen-command";
import { start, stop } from "./worker/startup";
import { getLogger } from "config/logger";
import { initStatsdLogger } from "config/statsd";
const initialize = () => {
initStatsdLogger(getLogger("config.statsd"));
initializeSentry();
// starts healthcheck/deepcheck or else deploy will fail
const port = Number(process.env.WORKER_PORT) || Number(process.env.PORT) || 8081;
const workerWebApp = createWorkerServerApp();
workerWebApp.listen(port, ()=>{
getLogger("worker").info(`Worker started at port ${port}`);
});
};
if (isNodeProd()) {
// Production clustering (one process per core)
// Read more about Node clustering: https://nodejs.org/api/cluster.html
throng({
worker: () => {
listenForClusterCommand(ClusterCommand.start, start);
listenForClusterCommand(ClusterCommand.stop, stop);
},
master: () => {
initialize();
// Listen to micros lifecycle event to know when to start/stop
listenToMicrosLifecycle(
// When 'active' event is triggered, start queue processing
() => sendCommandToCluster(ClusterCommand.start),
// When 'inactive' event is triggered, stop queue processing
() => sendCommandToCluster(ClusterCommand.stop)
);
},
lifetime: Infinity
});
} else {
initialize();
// Dev/test single process, no need for clustering or lifecycle events
// eslint-disable-next-line @typescript-eslint/no-floating-promises
start();
}