Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/flow-control/restart-process.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ it('restart processes forever, if tries is negative', () => {
expect(controller.tries).toBe(Infinity);
});

it('stops restarting processes when abort signal is triggered', () => {
const abortController = new AbortController();
controller = new RestartProcess({
logger,
scheduler,
delay: 100,
tries: 5,
abortSignal: abortController.signal,
});
controller.handle(commands);

// First failure should trigger a restart
commands[0].close.next(createFakeCloseEvent({ exitCode: 1 }));
scheduler.flush();
expect(commands[0].start).toHaveBeenCalledTimes(1);

// Abort signal is triggered (e.g., by killOthers)
abortController.abort();

// Subsequent failures should NOT trigger restarts
commands[0].close.next(createFakeCloseEvent({ exitCode: 1 }));
scheduler.flush();
expect(commands[0].start).toHaveBeenCalledTimes(1); // Still 1, no new restart
});

it('restarts processes until they succeed', () => {
controller.handle(commands);

Expand Down
27 changes: 26 additions & 1 deletion lib/flow-control/restart-process.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import Rx from 'rxjs';
import { defaultIfEmpty, delayWhen, filter, map, skip, take, takeWhile } from 'rxjs/operators';
import {
defaultIfEmpty,
delayWhen,
filter,
map,
share,
skip,
take,
takeUntil,
takeWhile,
} from 'rxjs/operators';

import { Command } from '../command.js';
import * as defaults from '../defaults.js';
Expand All @@ -15,31 +25,45 @@ export class RestartProcess implements FlowController {
private readonly logger: Logger;
private readonly scheduler?: Rx.SchedulerLike;
private readonly delay: RestartDelay;
private readonly abortSignal?: AbortSignal;
readonly tries: number;

constructor({
delay,
tries,
logger,
scheduler,
abortSignal,
}: {
delay?: RestartDelay;
tries?: number;
logger: Logger;
scheduler?: Rx.SchedulerLike;
/**
* When this signal is aborted, no more restarts will be attempted.
* This is useful to prevent restarts after `killOthers` has been triggered.
*/
abortSignal?: AbortSignal;
}) {
this.logger = logger;
this.delay = delay ?? 0;
this.tries = tries != null ? +tries : defaults.restartTries;
this.tries = this.tries < 0 ? Infinity : this.tries;
this.scheduler = scheduler;
this.abortSignal = abortSignal;
}

handle(commands: Command[]) {
if (this.tries === 0) {
return { commands };
}

// When abort signal is triggered, stop all restart attempts.
// This prevents restarts after `killOthers` has been triggered (#496).
const abort$ = this.abortSignal
? Rx.fromEvent(this.abortSignal, 'abort', { once: true }).pipe(share())
: Rx.NEVER;

const delayOperator = delayWhen((_, index) => {
const { delay } = this;
const value = delay === 'exponential' ? 2 ** index * 1000 : delay;
Expand All @@ -51,6 +75,7 @@ export class RestartProcess implements FlowController {
command.close.pipe(
take(this.tries),
takeWhile(({ exitCode }) => exitCode !== 0),
takeUntil(abort$),
),
)
.forEach((failure, index) =>
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export function concurrently(
logger,
delay: options.restartDelay,
tries: options.restartTries,
abortSignal: abortController.signal,
}),
new KillOthers({
logger,
Expand Down
Loading