diff --git a/docs/gitbook/SUMMARY.md b/docs/gitbook/SUMMARY.md index d8fcbf3d49..1b3f1407b0 100644 --- a/docs/gitbook/SUMMARY.md +++ b/docs/gitbook/SUMMARY.md @@ -58,6 +58,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({