Skip to content

Commit 6b63ebd

Browse files
committed
Massively improve logging around worker shutdown
1 parent bc42fc8 commit 6b63ebd

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/interfaces.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,9 @@ export interface WorkerPool {
568568

569569
export interface Runner {
570570
/** Attempts to cleanly shut down the runner */
571-
stop: () => Promise<void>;
571+
stop: (reason?: string) => Promise<void>;
572572
/** Use .stop() instead, unless you know what you're doing */
573-
kill: () => Promise<void>;
573+
kill: (reason?: string) => Promise<void>;
574574
addJob: AddJobFunction;
575575
promise: Promise<void>;
576576
events: WorkerEvents;

src/runner.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,10 @@ function buildRunner(input: {
164164
});
165165

166166
let running = true;
167-
const stop = async () => {
168-
compiledOptions.logger.debug("Runner stopping");
167+
const stop = async (reason: string | null, itsFine = reason === null) => {
168+
compiledOptions.logger[itsFine ? "debug" : "warn"](
169+
`Runner stopping${reason ? ` (reason: ${reason})` : ""}`,
170+
);
169171
if (running) {
170172
running = false;
171173
events.emit("stop", { ctx });
@@ -190,25 +192,19 @@ function buildRunner(input: {
190192
throw new Error("Runner is already stopped");
191193
}
192194
};
193-
const kill = async () => {
194-
if (running) {
195-
stop().catch(() => {});
196-
}
197-
if (workerPool._active) {
198-
await workerPool.forcefulShutdown(`Terminated through .kill() command`);
199-
}
200-
};
201195

202-
const wp = workerPool.promise.finally(() => {
203-
if (running) {
204-
stop();
205-
}
206-
});
207-
const cp = cron.promise.finally(() => {
208-
if (running) {
209-
stop();
210-
}
211-
});
196+
const wp = workerPool.promise
197+
.then(
198+
() => (running ? stop("worker pool exited cleanly", true) : void 0),
199+
(e) => (running ? stop(`worker pool exited with error: ${e}`) : void 0),
200+
)
201+
.catch(noop);
202+
const cp = cron.promise
203+
.then(
204+
() => (running ? stop("cron exited cleanly", true) : void 0),
205+
(e) => (running ? stop(`cron exited with error: ${e}`) : void 0),
206+
)
207+
.catch(noop);
212208

213209
const promise = Promise.all([cp, wp]).then(
214210
() => {
@@ -217,7 +213,7 @@ function buildRunner(input: {
217213
async (error) => {
218214
if (running) {
219215
logger.error(`Stopping worker due to an error: ${error}`, { error });
220-
await stop();
216+
await stop(String(error));
221217
} else {
222218
logger.error(
223219
`Error occurred, but worker is already stopping: ${error}`,
@@ -229,10 +225,22 @@ function buildRunner(input: {
229225
);
230226

231227
return {
232-
stop,
233-
kill,
228+
// It's fine - the user told us to exit
229+
stop: (reason) => stop(reason ?? null, true),
230+
async kill(reason?: string) {
231+
if (running) {
232+
stop(`runner.kill() called${reason ? `: ${reason}` : ""}`).catch(noop);
233+
}
234+
if (workerPool._active) {
235+
await workerPool.forcefulShutdown(`Terminated through .kill() command`);
236+
}
237+
},
234238
addJob,
235239
promise,
236240
events,
237241
};
238242
}
243+
244+
function noop() {
245+
/* NOOP */
246+
}

0 commit comments

Comments
 (0)