@entity-access/server-pages 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/ClusterInstance.ts +106 -0
- package/src/Invokable.ts +71 -0
package/package.json
CHANGED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/* eslint-disable no-console */
|
|
2
|
+
import cluster, { Worker } from "cluster";
|
|
3
|
+
import { IProcessLike, Invokable } from "./Invokable.js";
|
|
4
|
+
|
|
5
|
+
export class RecycledWorker<T = any> {
|
|
6
|
+
|
|
7
|
+
public get worker() {
|
|
8
|
+
return this.currentWorker;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
private currentWorker: Worker;
|
|
12
|
+
private destroyed: boolean;
|
|
13
|
+
|
|
14
|
+
private eventMap: Map<string,any> = new Map();
|
|
15
|
+
|
|
16
|
+
constructor(env?) {
|
|
17
|
+
this.currentWorker = cluster.fork(env);
|
|
18
|
+
this.currentWorker.on("exit" , () => {
|
|
19
|
+
if (this.destroyed) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
this.currentWorker = cluster.fork(env);
|
|
23
|
+
for (const [msg, handler] of this.eventMap) {
|
|
24
|
+
this.currentWorker.on(msg, handler);
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public on(msg: string, handler) {
|
|
30
|
+
this.eventMap.set(msg, handler);
|
|
31
|
+
if (this.destroyed) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.currentWorker.on(msg, handler);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
public send(a) {
|
|
38
|
+
if (this.destroyed) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
this.currentWorker.send(a);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
public destroy() {
|
|
45
|
+
this.destroyed = true;
|
|
46
|
+
this.currentWorker = null;
|
|
47
|
+
this.currentWorker.destroy();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export default abstract class ClusterInstance<T> extends Invokable {
|
|
53
|
+
|
|
54
|
+
protected isPrimary: boolean;
|
|
55
|
+
|
|
56
|
+
protected readonly workers: RecycledWorker[] = [];
|
|
57
|
+
|
|
58
|
+
public run(arg: T) {
|
|
59
|
+
this.isPrimary = cluster.isPrimary;
|
|
60
|
+
if (cluster.isPrimary) {
|
|
61
|
+
console.log(`Initializing Primary Cluster`);
|
|
62
|
+
this.setupPrimary(arg).catch(console.error);
|
|
63
|
+
} else {
|
|
64
|
+
this.setupWorker(arg).catch(console.error);
|
|
65
|
+
console.log(`Initializing Cluster Worker`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
protected abstract runPrimary(arg: T): Promise<void>;
|
|
70
|
+
protected abstract runWorker(arg: T): Promise<void>;
|
|
71
|
+
|
|
72
|
+
protected fork(env?) {
|
|
73
|
+
const worker = new RecycledWorker(env);
|
|
74
|
+
this.install(worker);
|
|
75
|
+
this.workers.push(worker);
|
|
76
|
+
return worker;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
protected async setupPrimary(arg: T) {
|
|
80
|
+
try {
|
|
81
|
+
await this.runPrimary(arg);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
console.error(error);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
protected async setupWorker(arg: T) {
|
|
88
|
+
try {
|
|
89
|
+
|
|
90
|
+
this.install(process);
|
|
91
|
+
|
|
92
|
+
await this.runWorker(arg);
|
|
93
|
+
|
|
94
|
+
process.send({ cmd: "ready"});
|
|
95
|
+
} catch (error) {
|
|
96
|
+
console.error(error);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
protected invoke(invoke: string, ...args: any[]): Promise<any> {
|
|
101
|
+
if (!this.isPrimary) {
|
|
102
|
+
return Invokable.invoke(this, process, invoke, args);
|
|
103
|
+
}
|
|
104
|
+
return Promise.all(this.workers.map((worker) => Invokable.invoke(this, worker, invoke, args)));
|
|
105
|
+
}
|
|
106
|
+
}
|
package/src/Invokable.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
|
|
2
|
+
export interface IProcessLike {
|
|
3
|
+
on(key: "message", fx: (data: any) => any);
|
|
4
|
+
send?(data);
|
|
5
|
+
postMessage?(data);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const defaultResult = null;
|
|
9
|
+
|
|
10
|
+
let newId = 1;
|
|
11
|
+
|
|
12
|
+
export class Invokable {
|
|
13
|
+
|
|
14
|
+
protected static invoke<T = any>(caller: Invokable, process: IProcessLike, invoke: string, ... args: any[]): Promise<T> {
|
|
15
|
+
const id = newId++;
|
|
16
|
+
return new Promise<T>((resolve, reject) => {
|
|
17
|
+
caller.promiseMap.set(id, { resolve, reject});
|
|
18
|
+
const send = process.send ?? process.postMessage;
|
|
19
|
+
send.call(process, { id, invoke, args });
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private promiseMap: Map<number, { resolve, reject}> = new Map();
|
|
24
|
+
|
|
25
|
+
protected install(process: IProcessLike) {
|
|
26
|
+
process.on("message", ({
|
|
27
|
+
id,
|
|
28
|
+
invoke,
|
|
29
|
+
args = [],
|
|
30
|
+
result: resultReceived,
|
|
31
|
+
error: errorReceived
|
|
32
|
+
}) => {
|
|
33
|
+
|
|
34
|
+
const send = process.send ?? process.postMessage;
|
|
35
|
+
|
|
36
|
+
if (!invoke) {
|
|
37
|
+
if (typeof resultReceived !== "undefined") {
|
|
38
|
+
const p = this.promiseMap.get(id);
|
|
39
|
+
if (p) {
|
|
40
|
+
this.promiseMap.delete(id);
|
|
41
|
+
p.resolve(resultReceived);
|
|
42
|
+
}
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
if (errorReceived) {
|
|
46
|
+
const p = this.promiseMap.get(id);
|
|
47
|
+
if (p) {
|
|
48
|
+
this.promiseMap.delete(id);
|
|
49
|
+
p.reject(errorReceived);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
try {
|
|
55
|
+
const r = this[invoke](... args) ?? defaultResult;
|
|
56
|
+
if (r.then) {
|
|
57
|
+
r.then((result = defaultResult) =>
|
|
58
|
+
send.call(process, { id, result })
|
|
59
|
+
, (error) =>
|
|
60
|
+
send.call(process, { id, error })
|
|
61
|
+
);
|
|
62
|
+
} else {
|
|
63
|
+
send.call(process, { id, result: r });
|
|
64
|
+
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
send.call(process, { id, error });
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
}
|