Skip to content

Commit

Permalink
Worker task execution timeout (global)
Browse files Browse the repository at this point in the history
The setting server.workers.timeout in application configuration is required but allows zero value to switch off the limit.

Closes: #1950
  • Loading branch information
KLarpen committed Dec 19, 2023
1 parent fa4d0bb commit 5989e07
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## [Unreleased][unreleased]

- Fixed API endpoints local queue settings applying
- Worker task execution global timeout implementation

## [3.0.13][] - 2023-10-22

Expand Down
15 changes: 13 additions & 2 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down

0 comments on commit 5989e07

Please sign in to comment.