-
Notifications
You must be signed in to change notification settings - Fork 9
/
worker.ts
35 lines (31 loc) · 1.15 KB
/
worker.ts
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
import { parentPort, workerData, MessagePort } from "worker_threads";
import { exec, ChildProcess } from "child_process";
import { WorkerDataType, WorkerTaskResponse, ProcessFlags } from "./Models";
const dataParameter: WorkerDataType = workerData as WorkerDataType;
const callbackFn = (errror: any, data: string): void => {
// console.log("DONE", d);
const parent = parentPort as MessagePort;
const processResponse: WorkerTaskResponse = {
flag: ProcessFlags.DONE.toString(),
pid: proc.pid,
data: data,
clientId: dataParameter.clientId,
};
parent.postMessage(processResponse);
};
const STDOUT_ON_DATA_EVENT = 'data';
const proc: ChildProcess = exec(dataParameter.command, callbackFn);
console.dir(proc.pid);
if (proc != null && proc.stdout != null) {
proc.stdout.on(STDOUT_ON_DATA_EVENT, (data: string) => {
console.log("ONGOING", dataParameter.clientId, data);
const parent = parentPort as MessagePort;
const processResponse: WorkerTaskResponse = {
flag: ProcessFlags.ONGOING.toString(),
pid: proc.pid,
data: data,
clientId: dataParameter.clientId,
};
parent.postMessage(processResponse);
});
}