-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8055598
commit 65e1656
Showing
3 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters