Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Services: Don't fail in watch mode when a dependency restarts or fails #526

Merged
merged 5 commits into from
Nov 14, 2022
Merged
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
62 changes: 46 additions & 16 deletions src/execution/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ type ServiceState =
| {
id: 'stopping';
child: ScriptChildProcess;
fingerprint: Fingerprint;
}
| {id: 'stopped'}
| {
id: 'failing';
child: ScriptChildProcess;
failure: Failure;
fingerprint: Fingerprint;
}
| {
id: 'failed';
Expand Down Expand Up @@ -164,7 +166,7 @@ function unexpectedState(state: ServiceState) {
* │ │ └────┬────┬┘ │ │
* │ │ │ │ │ │
* │ │ │ ╰─ depServiceExit ─►─╮ │
* ▼ │ │ │ │
* ▼ │ │ (unless watch mode) │ │
* │ │ │ │ │ │
* │ ▼ │ ▼ ▼ ▼
* │ │ started │ │ │
Expand All @@ -175,6 +177,7 @@ function unexpectedState(state: ServiceState) {
* │ │ └──────┬─┬┘ │ │
* │ │ │ │ │ │
* │ │ │ ╰── depServiceExit ─►─┤ │
* │ │ │ (unless watch mode) │ │
* │ │ │ │ │
* │ │ ╰───── detach ──╮ │ │
* │ │ │ │ │
Expand All @@ -197,6 +200,7 @@ function unexpectedState(state: ServiceState) {
export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScriptConfig> {
private _state: ServiceState;
private readonly _terminated = new Deferred<Result<void, Failure>>();
private readonly _isWatchMode: boolean;

/**
* Resolves as "ok" when this script decides it is no longer needed, and
Expand All @@ -213,9 +217,11 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
executor: Executor,
logger: Logger,
entireExecutionAborted: Promise<void>,
adoptee: ServiceScriptExecution | undefined
adoptee: ServiceScriptExecution | undefined,
isWatchMode: boolean
) {
super(config, executor, logger);
this._isWatchMode = isWatchMode;
this._state = {
id: 'initial',
entireExecutionAborted,
Expand All @@ -233,19 +239,19 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'unstarted':
case 'depsStarting':
case 'starting':
case 'started': {
case 'started':
case 'stopping':
case 'failing': {
return this._state.fingerprint;
}
case 'stopping':
case 'stopped':
case 'failed':
case 'failing':
case 'detached': {
return undefined;
case 'failed': {
return;
}
case 'initial':
case 'executingDeps':
case 'fingerprinting': {
case 'fingerprinting':
case 'detached': {
throw unexpectedState(this._state);
}
default: {
Expand All @@ -260,7 +266,9 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
*/
detach(): ScriptChildProcess | undefined {
switch (this._state.id) {
case 'started': {
case 'started':
case 'stopping':
case 'failing': {
const child = this._state.child;
this._state = {id: 'detached'};
// Note that for some reason, removing all listeners from stdout/stderr
Expand All @@ -271,10 +279,8 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
child.stderr.removeAllListeners('data');
return child;
}
case 'stopping':
case 'stopped':
case 'failed':
case 'failing': {
case 'failed': {
return undefined;
}
case 'unstarted':
Expand Down Expand Up @@ -630,9 +636,24 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
data,
});
});
void this._anyServiceTerminated.then(() => {
this._onDepServiceExit();
});
if (!this._isWatchMode) {
// If we're in watch mode, we don't care about our dependency services
// exiting because:
//
// 1. If we're iteration N-1 which is about to be adopted into
// iteration N, our dependencies will sometimes intentionally
// restart. This should not cause us to fail, since we'll either
// also restart very shortly (when cascade is true), or we'll just
// keep running (when cascade is false).
//
// 2. If we're iteration N and our dependency unexpectedly exits by
// itself, it's not actually helpful if we also exit. In non-watch
// mode it's important because we want wireit itself to exit as
// soon as this happens, but not so in watch mode.
void this._anyServiceTerminated.then(() => {
this._onDepServiceExit();
});
}
return;
}
case 'failed': {
Expand Down Expand Up @@ -695,6 +716,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
this._state = {
id: 'failing',
child: this._state.child,
fingerprint: this._state.fingerprint,
failure: {
type: 'failure',
script: this._config,
Expand All @@ -707,6 +729,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
this._state = {
id: 'failing',
child: this._state.child,
fingerprint: this._state.fingerprint,
failure: {
type: 'failure',
script: this._config,
Expand Down Expand Up @@ -795,6 +818,11 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
return;
}
case 'failing': {
this._logger.log({
script: this._config,
type: 'info',
detail: 'service-stopped',
});
this._enterFailedState(this._state.failure);
return;
}
Expand Down Expand Up @@ -829,13 +857,15 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
this._state = {
id: 'stopping',
child: this._state.child,
fingerprint: this._state.fingerprint,
};
break;
}
case 'starting': {
this._state = {
id: 'stopping',
child: this._state.child,
fingerprint: this._state.fingerprint,
};
break;
}
Expand Down
3 changes: 2 additions & 1 deletion src/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,8 @@ export class Executor {
this,
this._logger,
this._stopServices.promise,
this._previousIterationServices?.get(key)
this._previousIterationServices?.get(key),
this._isWatchMode
);
if (config.isPersistent) {
this._persistentServices.set(key, execution);
Expand Down
78 changes: 78 additions & 0 deletions src/test/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1143,4 +1143,82 @@ test(
})
);

test(
'service in watch mode persists when non-cascading dependency restarts or fails',
// parentService
// |
// v
// childService (restarts and fails)
timeout(async ({rig}) => {
const parentService = await rig.newCommand();
const childParent = await rig.newCommand();
await rig.writeAtomic({
'package.json': {
scripts: {
parentService: 'wireit',
childService: 'wireit',
},
wireit: {
parentService: {
command: parentService.command,
service: true,
dependencies: [
{
script: 'childService',
cascade: false,
},
],
files: ['input/parentService'],
},
childService: {
command: childParent.command,
service: true,
files: ['input/childService'],
},
},
},
});

await rig.write('input/parentService', '1');
await rig.write('input/childService', '1');
const wireit = rig.exec('npm run parentService --watch');

// Services start in bottom-up order.
const childServiceInv1 = await childParent.nextInvocation();
await wireit.waitForLog(/\[childService\] Service started/);
const parentServiceInv1 = await parentService.nextInvocation();
await wireit.waitForLog(/\[parentService\] Service started/);
await wireit.waitForLog(/\[parentService\] Watching for file changes/);

// childService restarts.
await rig.write('input/childService', '2');
await childServiceInv1.closed;
await wireit.waitForLog(/\[childService\] Service stopped/);
const childServiceInv2 = await childParent.nextInvocation();
await wireit.waitForLog(/\[childService\] Service started/);

// Wait a moment to increase confidence.
await new Promise((resolve) => setTimeout(resolve, 200));
assert.ok(parentServiceInv1.isRunning);
assert.ok(childServiceInv2.isRunning);
assert.not(childServiceInv1.isRunning);

// childService fails.
childServiceInv2.exit(1);
await wireit.waitForLog(/\[childService\] Service exited unexpectedly/);
await new Promise((resolve) => setTimeout(resolve, 200));
assert.ok(parentServiceInv1.isRunning);
assert.not(childServiceInv2.isRunning);
assert.not(childServiceInv1.isRunning);

wireit.kill();
await wireit.exit;
assert.not(parentServiceInv1.isRunning);
assert.not(childServiceInv2.isRunning);
assert.not(childServiceInv1.isRunning);
assert.equal(parentService.numInvocations, 1);
assert.equal(childParent.numInvocations, 2);
})
);

test.run();