From 1719e0ad2013a81ebf6e68bdd6cb099918788ce3 Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Sun, 8 Dec 2024 10:44:59 +0800 Subject: [PATCH] feat(experimental): configure mine worker timeout (#797) * feat(experimental): configure mine worker timeout * reduce log length --- src/utils/env.ts | 10 ++++++++++ src/worker/queues/mineTransactionQueue.ts | 6 +++++- src/worker/tasks/mineTransactionWorker.ts | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/utils/env.ts b/src/utils/env.ts index 5a8d13f00..e93a03204 100644 --- a/src/utils/env.ts +++ b/src/utils/env.ts @@ -98,6 +98,14 @@ export const env = createEnv({ QUEUE_FAIL_HISTORY_COUNT: z.coerce.number().default(10_000), // Sets the number of recent nonces to map to queue IDs. NONCE_MAP_COUNT: z.coerce.number().default(10_000), + + /** + * Experimental env vars. These may be renamed or removed in future non-major releases. + */ + // Sets how long the mine worker waits for a transaction receipt before considering the transaction dropped (default: 30 minutes). + EXPERIMENTAL__MINE_WORKER_TIMEOUT_SECONDS: z.coerce + .number() + .default(30 * 60), }, clientPrefix: "NEVER_USED", client: {}, @@ -134,6 +142,8 @@ export const env = createEnv({ QUEUE_COMPLETE_HISTORY_COUNT: process.env.QUEUE_COMPLETE_HISTORY_COUNT, QUEUE_FAIL_HISTORY_COUNT: process.env.QUEUE_FAIL_HISTORY_COUNT, NONCE_MAP_COUNT: process.env.NONCE_MAP_COUNT, + EXPERIMENTAL__MINE_WORKER_TIMEOUT_SECONDS: + process.env.EXPERIMENTAL__MINE_WORKER_TIMEOUT_SECONDS, METRICS_PORT: process.env.METRICS_PORT, METRICS_ENABLED: process.env.METRICS_ENABLED, }, diff --git a/src/worker/queues/mineTransactionQueue.ts b/src/worker/queues/mineTransactionQueue.ts index e35627272..977a727b5 100644 --- a/src/worker/queues/mineTransactionQueue.ts +++ b/src/worker/queues/mineTransactionQueue.ts @@ -1,5 +1,6 @@ import { Queue } from "bullmq"; import superjson from "superjson"; +import { env } from "../../utils/env"; import { redis } from "../../utils/redis/redis"; import { defaultJobOptions } from "./queues"; @@ -7,6 +8,9 @@ export type MineTransactionData = { queueId: string; }; +// Attempts are made every ~20 seconds. See backoffStrategy in initMineTransactionWorker(). +const NUM_ATTEMPTS = env.EXPERIMENTAL__MINE_WORKER_TIMEOUT_SECONDS / 20; + export class MineTransactionQueue { static q = new Queue("transactions-2-mine", { connection: redis, @@ -23,7 +27,7 @@ export class MineTransactionQueue { const jobId = this.jobId(data); await this.q.add(jobId, serialized, { jobId, - attempts: 100, // > 30 minutes with the backoffStrategy defined on the worker + attempts: NUM_ATTEMPTS, backoff: { type: "custom" }, }); }; diff --git a/src/worker/tasks/mineTransactionWorker.ts b/src/worker/tasks/mineTransactionWorker.ts index 1d3ce45f9..f054b2915 100644 --- a/src/worker/tasks/mineTransactionWorker.ts +++ b/src/worker/tasks/mineTransactionWorker.ts @@ -162,8 +162,9 @@ const _mineTransaction = async ( } // Else the transaction is not mined yet. + const ellapsedMs = Date.now() - sentTransaction.queuedAt.getTime(); job.log( - `Transaction is not mined yet. Check again later. sentTransactionHashes=${sentTransaction.sentTransactionHashes}`, + `Transaction is not mined yet. Check again later. elapsed=${ellapsedMs / 1000}s`, ); // Resend the transaction (after some initial delay).