From 2646b852e2b561074e99059646a4548250032842 Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Wed, 18 Oct 2023 11:07:07 -0700 Subject: [PATCH] Time out export jobs after 30 seconds. --- src/scripts/export/workers/export.ts | 65 ++++++++++++++++++---------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/src/scripts/export/workers/export.ts b/src/scripts/export/workers/export.ts index 6965b8f0..01e253f6 100644 --- a/src/scripts/export/workers/export.ts +++ b/src/scripts/export/workers/export.ts @@ -24,30 +24,47 @@ export const makeExportWorker: ExportWorkerMaker<{ return { queueName: QueueName.Export, - processor: async ({ data: { data } }) => { - // Group data by handler. - const groupedData = data.reduce( - (acc, { handler, data }) => ({ - ...acc, - [handler]: (acc[handler] || []).concat(data), - }), - {} as Record - ) - - // Process data. - for (const { name, handler } of handlers) { - const events = groupedData[name] - if (!events?.length) { - continue - } + processor: ({ data: { data } }) => + new Promise(async (resolve, reject) => { + // Time out if takes more than 30 seconds. + let timeout: NodeJS.Timeout | null = setTimeout(() => { + timeout = null + reject(new Error('Export timed out after 30 seconds.')) + }, 30000) + + try { + // Group data by handler. + const groupedData = data.reduce( + (acc, { handler, data }) => ({ + ...acc, + [handler]: (acc[handler] || []).concat(data), + }), + {} as Record + ) + + // Process data. + for (const { name, handler } of handlers) { + const events = groupedData[name] + if (!events?.length) { + continue + } - // Retry 3 times with exponential backoff starting at 100ms delay. - await retry(handler.process, [events], { - retriesMax: 3, - exponential: true, - interval: 100, - }) - } - }, + // Retry 3 times with exponential backoff starting at 100ms delay. + await retry(handler.process, [events], { + retriesMax: 3, + exponential: true, + interval: 100, + }) + } + + resolve() + } catch (err) { + reject(err) + } finally { + if (timeout !== null) { + clearTimeout(timeout) + } + } + }), } }