|
1 |
| -import PQueue from "p-queue"; |
2 |
| -import { knex } from "../../db/client"; |
| 1 | +import cron from "node-cron"; |
| 2 | +import { getConfiguration } from "../../db/configuration/getConfiguration"; |
3 | 3 | import { logger } from "../../utils/logger";
|
4 | 4 | import { processTx } from "../tasks/processTx";
|
5 | 5 |
|
6 |
| -const queue = new PQueue({ |
7 |
| - concurrency: 1, |
8 |
| - autoStart: true, |
9 |
| -}); |
10 |
| - |
11 |
| -queue.on("error", (err) => { |
12 |
| - logger({ |
13 |
| - service: "worker", |
14 |
| - level: "error", |
15 |
| - message: `[Queue Error] Size: ${queue.size}, Pending: ${queue.pending}`, |
16 |
| - error: err, |
17 |
| - }); |
18 |
| - |
19 |
| - throw err; |
20 |
| -}); |
21 |
| - |
22 | 6 | export const queuedTxListener = async (): Promise<void> => {
|
23 | 7 | logger({
|
24 | 8 | service: "worker",
|
25 | 9 | level: "info",
|
26 | 10 | message: `Listening for queued transactions`,
|
27 | 11 | });
|
28 | 12 |
|
29 |
| - // TODO: This doesn't even need to be a listener |
30 |
| - const connection = await knex.client.acquireConnection(); |
31 |
| - connection.query(`LISTEN new_transaction_data`); |
32 |
| - |
33 |
| - // Queue transaction processing immediately on startup |
34 |
| - queue.add(processTx); |
35 |
| - |
36 |
| - // Whenever we receive a new transaction, process it |
37 |
| - connection.on( |
38 |
| - "notification", |
39 |
| - async (msg: { channel: string; payload: string }) => { |
40 |
| - queue.add(processTx); |
41 |
| - }, |
42 |
| - ); |
43 |
| - |
44 |
| - connection.on("end", async () => { |
45 |
| - await knex.destroy(); |
46 |
| - knex.client.releaseConnection(connection); |
47 |
| - |
48 |
| - logger({ |
49 |
| - service: "worker", |
50 |
| - level: "info", |
51 |
| - message: `Released database connection on end`, |
52 |
| - }); |
53 |
| - }); |
54 |
| - |
55 |
| - connection.on("error", async (err: any) => { |
56 |
| - logger({ |
57 |
| - service: "worker", |
58 |
| - level: "error", |
59 |
| - message: `Database connection error`, |
60 |
| - error: err, |
61 |
| - }); |
| 13 | + const config = await getConfiguration(); |
62 | 14 |
|
63 |
| - await knex.destroy(); |
64 |
| - knex.client.releaseConnection(connection); |
| 15 | + if (!config.minedTxListenerCronSchedule) { |
| 16 | + return; |
| 17 | + } |
65 | 18 |
|
66 |
| - logger({ |
67 |
| - service: "worker", |
68 |
| - level: "info", |
69 |
| - message: `Released database connection on error`, |
70 |
| - error: err, |
71 |
| - }); |
| 19 | + cron.schedule(config.minedTxListenerCronSchedule, async () => { |
| 20 | + await processTx(); |
72 | 21 | });
|
73 | 22 | };
|
0 commit comments