diff --git a/CHANGELOG.md b/CHANGELOG.md index 613c9868..88a02c9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## [Unreleased][unreleased] - Fixed API endpoints local queue settings applying +- Worker task execution global timeout implementation - Reimplement global timeouts.request usage during a Procedure invocation ## [3.0.13][] - 2023-10-22 diff --git a/lib/worker.js b/lib/worker.js index 82801b6e..4c8f113a 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -50,15 +50,26 @@ const handlers = { invoke: async ({ exclusive, data, port }) => { const { method, args } = data; - const { sandbox } = application; + const { sandbox, config } = application; const handler = metarhia.metautil.namespaceByPath(sandbox, method); if (!handler) { const error = new Error('Handler not found'); return void port.postMessage({ name: 'error', error }); } const msg = { name: 'invoke', status: 'done' }; + const { timeout } = config.server.workers; try { - const result = await handler(args); + let result; + if (timeout) { + const ac = new AbortController(); + result = await Promise.race([ + metarhia.metautil.timeout(timeout, ac.signal), + handler(args), + ]); + ac.abort(); + } else { + result = await handler(args); + } port.postMessage({ ...msg, data: result }); } catch (error) { port.postMessage({ name: 'error', error });