Job Timeout
When you add a job to the queue, you can set a timeout for it. If the job doesn't finish before the timeout, it will be marked as failed and may be retried. See Failed Jobs for more information.
When the timeout is reached, DataQueue does not actually stop the handler from running. You need to handle this in your handler by checking the AbortSignal at one or more points in your code. For example:
const handler = async (payload, signal) => {
// Simulate work
// Do something that may take a long time
// Check if the job is aborted
if (signal.aborted) {
return;
}
// Do something else
// Check again if the job is aborted
if (signal.aborted) {
return;
}
// ...rest of your logic
};If the job times out, the signal will be aborted and your handler should exit early. If your handler does not check for signal.aborted, it will keep running in the background even after the job is marked as failed due to timeout. For best results, always make your handlers abortable if they might run for a long time.
Force Kill on Timeout
If you need to forcefully terminate jobs that don't respond to the abort signal, you can use forceKillOnTimeout: true. This will run the handler in a Worker Thread and forcefully terminate it when the timeout is reached.
⚠️ Runtime Requirements: forceKillOnTimeout requires Node.js and will not work in Bun or other runtimes without worker thread support. See Force Kill on Timeout for details.
Important: When using forceKillOnTimeout, your handler must be serializable. See Force Kill on Timeout for details.
await queue.addJob({
jobType: 'longRunningTask',
payload: { data: '...' },
timeoutMs: 5000,
forceKillOnTimeout: true, // Forcefully terminate if timeout is reached
});