diff --git a/src/server/ProxiedApp.ts b/src/server/ProxiedApp.ts index 865ed218c..adc98e26c 100644 --- a/src/server/ProxiedApp.ts +++ b/src/server/ProxiedApp.ts @@ -49,9 +49,17 @@ export class ProxiedApp { return logger; } + // We'll need to refactor this method to remove the rest parameters so we can pass an options parameter public async call(method: `${AppMethod}`, ...args: Array): Promise { + let options; + + // Pre events need to be fast as they block the user + if (method.startsWith('checkPre') || method.startsWith('executePre')) { + options = { timeout: 1000 }; + } + try { - return await this.appRuntime.sendRequest({ method: `app:${method}`, params: args }); + return await this.appRuntime.sendRequest({ method: `app:${method}`, params: args }, options); } catch (e) { if (e.code === AppsEngineException.JSONRPC_ERROR_CODE) { throw new AppsEngineException(e.message); diff --git a/src/server/runtime/deno/AppsEngineDenoRuntime.ts b/src/server/runtime/deno/AppsEngineDenoRuntime.ts index 0b4288ea2..5e0455464 100644 --- a/src/server/runtime/deno/AppsEngineDenoRuntime.ts +++ b/src/server/runtime/deno/AppsEngineDenoRuntime.ts @@ -83,6 +83,10 @@ export type DenoSystemUsageRecord = { external: number; }; +export type DenoRuntimeOptions = { + timeout: number; +}; + export class DenoRuntimeSubprocessController extends EventEmitter { private readonly deno: child_process.ChildProcess; @@ -259,14 +263,14 @@ export class DenoRuntimeSubprocessController extends EventEmitter { this.deno.stdin.write(encoder.encode(message)); } - public async sendRequest(message: Pick): Promise { + public async sendRequest(message: Pick, options = this.options): Promise { const id = String(Math.random().toString(36)).substring(2); const start = Date.now(); const request = jsonrpc.request(id, message.method, message.params); - const promise = this.waitForResponse(request).finally(() => { + const promise = this.waitForResponse(request, options).finally(() => { this.debug('Request %s for method %s took %dms', id, message.method, Date.now() - start); }); @@ -291,7 +295,7 @@ export class DenoRuntimeSubprocessController extends EventEmitter { }); } - private waitForResponse(req: jsonrpc.RequestObject): Promise { + private waitForResponse(req: jsonrpc.RequestObject, options = this.options): Promise { return new Promise((resolve, reject) => { const responseCallback = (result: unknown, error: jsonrpc.IParsedObjectError['payload']['error']) => { clearTimeout(timeoutId); @@ -308,7 +312,7 @@ export class DenoRuntimeSubprocessController extends EventEmitter { const timeoutId = setTimeout(() => { this.off(eventName, responseCallback); reject(new Error(`[${this.getAppId()}] Request "${req.id}" for method "${req.method}" timed out`)); - }, this.options.timeout); + }, options.timeout); this.once(eventName, responseCallback); });