-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdata_worker.ts
More file actions
78 lines (66 loc) · 2.2 KB
/
data_worker.ts
File metadata and controls
78 lines (66 loc) · 2.2 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { Worker } from "bullmq";
import { summarizeDataSettlementJob, summarizeError } from "./logging.js";
import { incrementMetric } from "./metrics.js";
import {
createDataWorkerContext,
createBullMqConnection,
processDataSettlementJob,
} from "./all_networks_shared.js";
import {
DATA_WORKER_EVM_PRIVATE_KEY_ENV,
DATA_SETTLEMENT_QUEUE_NAME,
SHUTDOWN_TIMEOUT_MS,
type DataSettlementJobData,
} from "./all_networks_types_helpers.js";
const dataWorkerContext = createDataWorkerContext();
const worker = new Worker<DataSettlementJobData>(
DATA_SETTLEMENT_QUEUE_NAME,
async (job: { id?: string; data: DataSettlementJobData }) => {
console.log("[data-worker] Processing data settlement job", {
jobId: job.id,
...summarizeDataSettlementJob(job.data),
});
return processDataSettlementJob(job.data, dataWorkerContext);
},
{
connection: createBullMqConnection(),
concurrency: 1,
},
);
worker.on("completed", (job: { id?: string }) => {
incrementMetric("worker.job.completed.count", ["worker:data"]);
console.log(`[data-worker] Completed job ${job.id}`);
});
worker.on("failed", (job: { id?: string } | undefined, err: unknown) => {
incrementMetric("worker.job.failed.count", ["worker:data"]);
console.error("[data-worker] Failed job", {
jobId: job?.id ?? "unknown",
...summarizeError(err),
});
});
let isShuttingDown = false;
async function shutdown(signal: string): Promise<void> {
if (isShuttingDown) {
return;
}
isShuttingDown = true;
console.log(`\\nReceived ${signal}. Shutting down data worker...`);
const forcedExitTimer = setTimeout(() => {
console.error(`Forced shutdown after ${SHUTDOWN_TIMEOUT_MS}ms`);
process.exit(1);
}, SHUTDOWN_TIMEOUT_MS);
forcedExitTimer.unref();
await worker.close();
clearTimeout(forcedExitTimer);
process.exit(0);
}
process.on("SIGINT", () => {
void shutdown("SIGINT");
});
process.on("SIGTERM", () => {
void shutdown("SIGTERM");
});
console.log(`[data-worker] Listening on queue: ${DATA_SETTLEMENT_QUEUE_NAME}`);
console.log(
`[data-worker] Using ${DATA_WORKER_EVM_PRIVATE_KEY_ENV} for signer ${dataWorkerContext.signerAddress} on ${dataWorkerContext.chainName} (${dataWorkerContext.chainId})`,
);