Skip to content

Commit dee897b

Browse files
committed
On major error, trigger workerPool shutdown
1 parent 1b0b50e commit dee897b

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/localQueue.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ export class LocalQueue {
242242
* for just a moment. (I.e. `runOnce()`)
243243
*/
244244
private readonly continuous: boolean,
245+
private readonly onMajorError: (e: unknown) => void,
245246
) {
246247
this.ttl =
247248
compiledSharedOptions.resolvedPreset.worker.localQueue?.ttl ?? 5 * MINUTE;
@@ -477,10 +478,18 @@ export class LocalQueue {
477478
);
478479
}
479480

480-
// NOTE: considered doing `this.receivedJobs(jobsToReturn)`; but I
481+
// NOTE: considered doing `this.receivedJobs(jobsToReturn)`; but
481482
// simply trying to release them again seems safer and more correct.
482483
default: {
483-
if (attempts < maxAttempts) {
484+
if (attempts >= maxAttempts) {
485+
this.onMajorError(
486+
new Error(
487+
`Error occurred whilst returning jobs from local queue to database queue; raising major error after ${attempts} (>= ${maxAttempts}) attempts (should trigger shutdown). Initial error: ${initialError.message}`,
488+
{ cause: initialError },
489+
),
490+
);
491+
}
492+
{
484493
const code = lastError?.code as string;
485494
const retryable = RETRYABLE_ERROR_CODES[code];
486495
const delay = calculateDelay(attempts - 1, {
@@ -499,15 +508,6 @@ export class LocalQueue {
499508
jobsToReturn,
500509
).then(noop, onError),
501510
);
502-
} else {
503-
// TODO: is this the correct way to handle this? Are we allowed to
504-
// trigger shut down internally?
505-
this.release();
506-
// Now we're in release mode, throwing the error will be tracked
507-
// automatically by `this.background()`
508-
throw new Error(
509-
`Error occurred whilst returning jobs from local queue to database queue; aborting after ${attempts} attempts. Initial error: ${initialError.message}`,
510-
);
511511
}
512512
}
513513
}

src/main.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,16 @@ export function _runTaskList(
12071207
workerPool,
12081208
localQueueSize,
12091209
continuous,
1210+
function onMajorError(e) {
1211+
if (_shuttingDownForcefully) {
1212+
// Already shutting down, ignore
1213+
} else if (_shuttingDownGracefully) {
1214+
// workerPool.forcefulShutdown(String(e));
1215+
// Already shutting down, ignore
1216+
} else {
1217+
workerPool.gracefulShutdown(String(e));
1218+
}
1219+
},
12101220
)
12111221
: null;
12121222
const getJob: GetJobFunction = localQueue

0 commit comments

Comments
 (0)