From 65e16569a33974bf5071717557f6d5496b6b0c0e Mon Sep 17 00:00:00 2001 From: roggervalf Date: Tue, 14 Nov 2023 21:36:37 -0700 Subject: [PATCH] docs(pattern): add timeout section --- docs/gitbook/SUMMARY.md | 1 + docs/gitbook/patterns/timeout.md | 64 ++++++++++++++++++++++++++++++++ tests/test_worker.ts | 2 - 3 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 docs/gitbook/patterns/timeout.md diff --git a/docs/gitbook/SUMMARY.md b/docs/gitbook/SUMMARY.md index 3b353011fe..6137239660 100644 --- a/docs/gitbook/SUMMARY.md +++ b/docs/gitbook/SUMMARY.md @@ -60,6 +60,7 @@ * [Process Step Jobs](patterns/process-step-jobs.md) * [Failing fast when Redis is down](patterns/failing-fast-when-redis-is-down.md) * [Stop retrying jobs](patterns/stop-retrying-jobs.md) +* [Timeout](patterns/timeout.md) ## BullMQ Pro diff --git a/docs/gitbook/patterns/timeout.md b/docs/gitbook/patterns/timeout.md new file mode 100644 index 0000000000..e351b2d2ea --- /dev/null +++ b/docs/gitbook/patterns/timeout.md @@ -0,0 +1,64 @@ +# Timeout + +Sometimes, it is useful to timeout a processor function but you should be aware that async processes are not going to be cancelled immediately, even if you timeout a process, you need to validate that your process is in a cancelled state: + +```typescript +enum Step { + Initial, + Second, + Finish, +} + +const worker = new Worker( + 'queueName', + async job => { + let { step, timeout } = job.data; + let timeoutReached = false; + + setTimeout(() => { + timeoutReached = true; + }, timeout); + while (step !== Step.Finish) { + switch (step) { + case Step.Initial: { + await doInitialStepStuff(1000); + if (timeoutReached) { + throw new Error('Timeout'); + } + await job.updateData({ + step: Step.Second, + timeout, + }); + step = Step.Second; + break; + } + case Step.Second: { + await doSecondStepStuff(); + if (timeoutReached) { + throw new Error('Timeout'); + } + await job.updateData({ + step: Step.Finish, + timeout, + }); + step = Step.Finish; + return Step.Finish; + } + default: { + throw new Error('invalid step'); + } + } + } + }, + { connection }, +); +``` + +{% hint style="info" %} +It's better to split a long process into little functions/steps to be able to stop an execution by validating if we reach the timeout in each transition. +{% endhint %} + +## Read more: + +- 📋 [Process Step jobs](./process-step-jobs.md) +- 📋 [Cancellation by using Observables](../bullmq-pro/observables/cancelation.md) \ No newline at end of file diff --git a/tests/test_worker.ts b/tests/test_worker.ts index d45a547160..d294a4c26a 100644 --- a/tests/test_worker.ts +++ b/tests/test_worker.ts @@ -2577,13 +2577,11 @@ describe('workers', function () { setTimeout(() => { timeoutReached = true; }, timeout); - console.log(step, timeoutReached, job.data.timeout); while (step !== Step.Finish) { switch (step) { case Step.Initial: { await delay(1000); if (timeoutReached) { - console.log('reaached1'); throw new Error('Timeout'); } await job.updateData({