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

fix: app subprocess restart on error and better reporting #34106

Merged
merged 18 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
18 changes: 17 additions & 1 deletion packages/apps-engine/deno-runtime/handlers/app/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ export default async function handleApp(method: string, params: unknown): Promis

// We don't want the getStatus method to generate logs, so we handle it separately
if (appMethod === 'getStatus') {
return handleGetStatus();
try {
return handleGetStatus();
ggazzo marked this conversation as resolved.
Show resolved Hide resolved
} catch (e) {
if (!(e instanceof Error)) {
return new JsonRpcError('Unknown error', -32000, e);
}

if ((e.cause as string)?.includes('invalid_param_type')) {
return JsonRpcError.invalidParams(null);
}

if ((e.cause as string)?.includes('invalid_app')) {
return JsonRpcError.internalError({ message: 'App unavailable' });
}

return new JsonRpcError(e.message, -32000, e);
}
}

// `app` will be undefined if the method here is "app:construct"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,18 +318,21 @@ export class DenoRuntimeSubprocessController extends EventEmitter {
}

private waitUntilReady(): Promise<void> {
if (this.state === 'ready') {
return;
}
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => reject(new Error(`[${this.getAppId()}] Timeout: app process not ready`)), this.options.timeout);

if (this.state === 'ready') {
let timeoutId: NodeJS.Timeout;
const handler = () => {
clearTimeout(timeoutId);
return resolve();
}
resolve();
};
timeoutId = setTimeout(() => {
this.off('ready', handler);
reject(new Error(`[${this.getAppId()}] Timeout: app process not ready`));
}, this.options.timeout);

this.once('ready', () => {
clearTimeout(timeoutId);
return resolve();
});
this.once('ready', handler);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export class LivenessManager {

this.controller.once('ready', () => this.ping());
this.subprocess.once('exit', this.handleExit.bind(this));
this.subprocess.once('error', this.handleError.bind(this));
}

/**
Expand Down Expand Up @@ -155,6 +156,11 @@ export class LivenessManager {
this.messenger.send(COMMAND_PING);
}

private handleError(err: Error) {
this.debug('App has failed to start.`', err);
this.restartProcess(err.message);
}

private handleExit(exitCode: number, signal: string) {
this.pingAbortController.emit('abort');

Expand Down Expand Up @@ -194,6 +200,6 @@ export class LivenessManager {
pid: this.subprocess.pid,
});

this.controller.restartApp();
setTimeout(() => this.controller.restartApp(), Math.max(this.restartCount++ * 1000, 10 * 1000));
}
}
Loading