Skip to content

Commit

Permalink
Add started-broken state
Browse files Browse the repository at this point in the history
  • Loading branch information
aomarks committed Dec 8, 2022
1 parent 483bd83 commit 705545b
Showing 1 changed file with 54 additions and 5 deletions.
59 changes: 54 additions & 5 deletions src/execution/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ type ServiceState =
child: ScriptChildProcess;
fingerprint: Fingerprint;
}
| {
id: 'started-broken';
child: ScriptChildProcess;
fingerprint: Fingerprint;
failure: Failure;
}
| {
id: 'stopping';
child: ScriptChildProcess;
Expand Down Expand Up @@ -270,6 +276,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'starting':
case 'readying':
case 'started':
case 'started-broken':
case 'stopping':
case 'failing': {
return this._state.fingerprint;
Expand Down Expand Up @@ -297,6 +304,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
detach(): {child: ScriptChildProcess; fingerprint: Fingerprint} | undefined {
switch (this._state.id) {
case 'started':
case 'started-broken':
case 'stopping':
case 'failing': {
const {child, fingerprint} = this._state;
Expand Down Expand Up @@ -367,6 +375,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'starting':
case 'readying':
case 'started':
case 'started-broken':
case 'stopping':
case 'stopped':
case 'failed':
Expand Down Expand Up @@ -409,6 +418,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'starting':
case 'readying':
case 'started':
case 'started-broken':
case 'stopping':
case 'failing':
case 'detached': {
Expand All @@ -424,9 +434,16 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
switch (this._state.id) {
case 'executingDeps': {
this._state.deferredFingerprint.resolve(result);
this._enterFailedState(result.error[0]);
const failure = result.error[0];
const detached = this._state.adoptee?.detach();
if (detached !== undefined) {
this._enterStartedBrokenState(failure, detached);
} else {
this._enterFailedState(failure);
}
return;
}
case 'started-broken':
case 'stopped':
case 'failed': {
return;
Expand Down Expand Up @@ -496,6 +513,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'starting':
case 'readying':
case 'started':
case 'started-broken':
case 'stopping':
case 'failing':
case 'detached': {
Expand Down Expand Up @@ -536,6 +554,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'starting':
case 'readying':
case 'started':
case 'started-broken':
case 'stopping':
case 'failing':
case 'detached': {
Expand Down Expand Up @@ -598,6 +617,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'started': {
return Promise.resolve({ok: true, value: undefined});
}
case 'started-broken':
case 'failing':
case 'failed': {
return Promise.resolve({ok: false, error: this._state.failure});
Expand Down Expand Up @@ -691,6 +711,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'starting':
case 'readying':
case 'started':
case 'started-broken':
case 'stopping':
case 'stopped':
case 'failing':
Expand All @@ -709,7 +730,13 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
// TODO(aomarks) The inconsistency between using single vs multiple
// failure result types is inconvenient. It's ok to just use the first
// one here, but would make more sense to return all of them.
this._terminated.resolve({ok: false, error: result.error[0]});
const failure = result.error[0];
const detached = this._state.adoptee?.detach();
if (detached !== undefined) {
this._enterStartedBrokenState(failure, detached);
} else {
this._enterFailedState(failure);
}
return;
}
case 'failing':
Expand All @@ -726,6 +753,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'starting':
case 'readying':
case 'started':
case 'started-broken':
case 'detached': {
throw unexpectedState(this._state);
}
Expand All @@ -737,7 +765,8 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri

private _onDepServiceExit() {
switch (this._state.id) {
case 'started': {
case 'started':
case 'started-broken': {
this._state.child.kill();
this._state = {
id: 'failing',
Expand Down Expand Up @@ -831,6 +860,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'depsStarting':
case 'readying':
case 'started':
case 'started-broken':
case 'stopped':
case 'failed':
case 'detached': {
Expand Down Expand Up @@ -868,6 +898,7 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
case 'unstarted':
case 'depsStarting':
case 'started':
case 'started-broken':
case 'stopped':
case 'failed':
case 'detached': {
Expand Down Expand Up @@ -901,7 +932,8 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
this._enterFailedState(event);
return;
}
case 'started': {
case 'started':
case 'started-broken': {
const event = {
script: this._config,
type: 'failure',
Expand Down Expand Up @@ -946,7 +978,8 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
*/
abort(): Promise<void> {
switch (this._state.id) {
case 'started': {
case 'started':
case 'started-broken': {
this._state.child.kill();
this._state = {
id: 'stopping',
Expand Down Expand Up @@ -1013,6 +1046,22 @@ export class ServiceScriptExecution extends BaseExecutionWithCommand<ServiceScri
this._servicesNotNeeded.resolve();
}

private _enterStartedBrokenState(
failure: Failure,
{child, fingerprint}: {child: ScriptChildProcess; fingerprint: Fingerprint}
) {
this._startLoggingChildStdio(child);
void child.completed.then(() => {
this._onChildExited();
});
this._state = {
id: 'started-broken',
child,
fingerprint,
failure,
};
}

private _startLoggingChildStdio(child: ScriptChildProcess) {
child.stdout.on('data', (data: string | Buffer) => {
this._logger.log({
Expand Down

0 comments on commit 705545b

Please sign in to comment.