-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathcall-worker.ts
More file actions
62 lines (55 loc) · 1.33 KB
/
call-worker.ts
File metadata and controls
62 lines (55 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Logger } from '@openfn/logger';
import createPool from '../worker/pool';
import { CallWorker } from '../types';
// All events coming out of the worker need to include a type key
export type WorkerEvent = {
type: string;
[key: string]: any;
};
type WorkerOptions = {
maxWorkers?: number;
maxWorkerMemoryMb?: number; // kernel-level memory limit per child process (cgroup v2)
env?: any;
timeout?: number; // ms
proxyStdout?: boolean; // print internal stdout to console
};
// Create a worker pool and return helper functions
// to use and destroy it
export default function initWorkers(
workerPath: string,
options: WorkerOptions = {},
logger: Logger
) {
const {
env = {},
maxWorkers = 5,
maxWorkerMemoryMb,
proxyStdout = false,
} = options;
const workers = createPool(
workerPath,
{
maxWorkers,
maxWorkerMemoryMb,
env,
proxyStdout,
},
logger
);
const callWorker: CallWorker = (
task,
args = [],
events = {},
options = {}
) => {
return workers.exec(task, args, {
...options,
on: ({ type, ...args }: WorkerEvent) => {
// just call the callback
events[type]?.(args);
},
});
};
const closeWorkers = async (instant?: boolean) => workers.destroy(instant);
return { callWorker, closeWorkers, workers };
}