Skip to content

Commit d14f3bf

Browse files
committed
More consistently handle errors in forcefulShutdown
1 parent 7a8f872 commit d14f3bf

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

src/main.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ export function _runTaskList(
856856
"Errors occurred whilst shutting down worker",
857857
);
858858
}
859+
859860
events.emit("pool:gracefulShutdown:complete", {
860861
pool: workerPool,
861862
workerPool,
@@ -870,9 +871,7 @@ export function _runTaskList(
870871
const message = coerceError(e).message;
871872
logger.error(
872873
`Error occurred during graceful shutdown: ${message}`,
873-
{
874-
error: e,
875-
},
874+
{ error: e },
876875
);
877876
// NOTE: we now rely on forcefulShutdown to handle terminate()
878877
if (this._forcefulShuttingDown) {
@@ -922,6 +921,8 @@ export function _runTaskList(
922921
// Stop new jobs being added
923922
const deactivatePromise = deactivate();
924923

924+
const errors: Error[] = [];
925+
925926
// Release all our workers' jobs
926927
const workers = [...workerPool._workers];
927928
const jobsInProgress: Array<Job> = workers
@@ -941,6 +942,7 @@ export function _runTaskList(
941942
logger.error(`Deactivation failed: ${deactivateResult.reason}`, {
942943
error: deactivateResult.reason,
943944
});
945+
errors.push(coerceError(deactivateResult.reason));
944946
}
945947

946948
if (jobsInProgress.length > 0) {
@@ -954,19 +956,33 @@ export function _runTaskList(
954956
workerIds,
955957
},
956958
);
957-
const cancelledJobs = await failJobs(
958-
compiledSharedOptions,
959-
withPgClient,
960-
workerPool.id,
961-
jobsInProgress,
962-
message,
963-
);
964-
logger.debug(`Cancelled ${cancelledJobs.length} jobs`, {
965-
cancelledJobs,
966-
});
959+
try {
960+
const cancelledJobs = await failJobs(
961+
compiledSharedOptions,
962+
withPgClient,
963+
workerPool.id,
964+
jobsInProgress,
965+
message,
966+
);
967+
logger.debug(`Cancelled ${cancelledJobs.length} jobs`, {
968+
cancelledJobs,
969+
});
970+
} catch (e) {
971+
errors.push(coerceError(e));
972+
}
967973
} else {
968974
logger.debug("No active jobs to release");
969975
}
976+
977+
if (errors.length === 1) {
978+
throw errors[0];
979+
} else if (errors.length > 1) {
980+
throw new AggregateError(
981+
errors,
982+
"Errors occurred whilst forcefully shutting down worker",
983+
);
984+
}
985+
970986
events.emit("pool:forcefulShutdown:complete", {
971987
pool: workerPool,
972988
workerPool,

0 commit comments

Comments
 (0)