Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(pattern): add timeout section #2281

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/gitbook/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
64 changes: 64 additions & 0 deletions docs/gitbook/patterns/timeout.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 0 additions & 2 deletions tests/test_worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading