MCPcopy Create free account
hub / github.com/AdaXH/korct / Process

Class Process

class/Process.ts:11–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9 * 进程管理
10 */
11export class Process {
12 private workers: WorkerFace[] = [];
13 private maxProcess: number = cpus;
14 private callback: VoidFunction;
15 private eventListener: eventFunction[];
16
17 public setCallback(callback: VoidFunction): void {
18 this.callback = callback;
19 }
20
21 public setMaxProcess(maxProcess: number): void {
22 this.maxProcess = maxProcess;
23 }
24
25 public setEventListener(eventListener: eventFunction[]): void {
26 this.eventListener = eventListener;
27 }
28
29 init(): void {
30 if (cluster.isMaster) {
31 for (let i = 0; i < this.maxProcess; i += 1) {
32 const worker = cluster.fork();
33 setLog(worker);
34 worker.on('message', arg => {
35 if (this.eventListener) {
36 this.eventListener.forEach(({ eventName, callback }) => {
37 if (arg?.eventName === eventName) {
38 logger.info(`event: ${eventName}`);
39 callback();
40 }
41 });
42 }
43 });
44 this.workers.push({
45 pid: worker.process.pid,
46 worker,
47 });
48 }
49 cluster.on('exit', (worker, code, sign) => {
50 const newWorker = cluster.fork();
51 setLog(newWorker);
52 logger.warning(`process exit whitd code: ${code}, sing: ${sign}, now loading`);
53 const newWorkes = this.workers
54 .filter(item => worker.process.pid !== item.pid)
55 .concat([
56 {
57 pid: newWorker.process.pid,
58 worker: newWorker,
59 },
60 ]);
61 this.workers = newWorkes;
62 });
63 } else {
64 this.callback();
65 }
66 }
67}
68

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected