nestworker 1.1.20 → 2.0.4
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/LICENSE +21 -21
- package/README.md +334 -143
- package/dist/core/worker.interfaces.d.ts +36 -0
- package/dist/{models/task.js → core/worker.interfaces.js} +1 -1
- package/dist/core/worker.interfaces.js.map +1 -0
- package/dist/core/worker.module.d.ts +15 -0
- package/dist/core/worker.module.js +43 -0
- package/dist/core/worker.module.js.map +1 -0
- package/dist/core/worker.pool.d.ts +20 -0
- package/dist/core/worker.pool.js +210 -0
- package/dist/core/worker.pool.js.map +1 -0
- package/dist/core/worker.service.d.ts +87 -0
- package/dist/core/worker.service.js +133 -0
- package/dist/core/worker.service.js.map +1 -0
- package/dist/decorators/worker-task.decorator.d.ts +37 -0
- package/dist/decorators/worker-task.decorator.js +48 -0
- package/dist/decorators/worker-task.decorator.js.map +1 -0
- package/dist/di/di-serializer.d.ts +18 -0
- package/dist/di/di-serializer.js +110 -0
- package/dist/di/di-serializer.js.map +1 -0
- package/dist/di/worker-container.d.ts +58 -0
- package/dist/di/worker-container.js +110 -0
- package/dist/di/worker-container.js.map +1 -0
- package/dist/discovery/discovery.service.d.ts +20 -0
- package/dist/discovery/discovery.service.js +94 -0
- package/dist/discovery/discovery.service.js.map +1 -0
- package/dist/example/config.service.d.ts +13 -0
- package/dist/example/config.service.js +35 -0
- package/dist/example/config.service.js.map +1 -0
- package/dist/example/image.service.d.ts +9 -0
- package/dist/example/image.service.js +108 -0
- package/dist/example/image.service.js.map +1 -0
- package/dist/example/main.d.ts +1 -0
- package/dist/example/main.js +59 -0
- package/dist/example/main.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/worker/worker-runtime.js +55 -0
- package/dist/worker/worker-runtime.js.map +1 -0
- package/package.json +58 -55
- package/CONTRIBUTING.md +0 -33
- package/dist/main.d.ts +0 -3
- package/dist/main.js +0 -29
- package/dist/main.js.map +0 -1
- package/dist/models/index.d.ts +0 -1
- package/dist/models/index.js +0 -18
- package/dist/models/index.js.map +0 -1
- package/dist/models/task.d.ts +0 -5
- package/dist/models/task.js.map +0 -1
- package/dist/tsconfig.build.tsbuildinfo +0 -1
- package/dist/worker/executor.d.ts +0 -5
- package/dist/worker/executor.js +0 -28
- package/dist/worker/executor.js.map +0 -1
- package/dist/worker/skeleton.d.ts +0 -6
- package/dist/worker/skeleton.js +0 -35
- package/dist/worker/skeleton.js.map +0 -1
- package/dist/worker/worker.js +0 -6
- package/dist/worker/worker.js.map +0 -1
- package/nest-cli.json +0 -8
- package/tsconfig.build.json +0 -4
- package/tsconfig.json +0 -25
- /package/dist/worker/{worker.d.ts → worker-runtime.d.ts} +0 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.WorkerPool = void 0;
|
|
7
|
+
const node_worker_threads_1 = require("node:worker_threads");
|
|
8
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
+
const PRIORITY_WEIGHT = {
|
|
11
|
+
HIGH: 3,
|
|
12
|
+
NORMAL: 2,
|
|
13
|
+
LOW: 1,
|
|
14
|
+
};
|
|
15
|
+
class WorkerPool {
|
|
16
|
+
services;
|
|
17
|
+
size;
|
|
18
|
+
workers = [];
|
|
19
|
+
idle = [];
|
|
20
|
+
queue = [];
|
|
21
|
+
destroyed = false;
|
|
22
|
+
active = new Map();
|
|
23
|
+
constructor(services, size = node_os_1.default.cpus().length) {
|
|
24
|
+
this.services = services;
|
|
25
|
+
this.size = size;
|
|
26
|
+
for (let i = 0; i < this.size; i++) {
|
|
27
|
+
const worker = this.spawnWorker();
|
|
28
|
+
this.workers.push(worker);
|
|
29
|
+
this.idle.push(worker);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
execute(job) {
|
|
33
|
+
if (this.destroyed) {
|
|
34
|
+
return Promise.reject(new Error('WorkerPool destroyed'));
|
|
35
|
+
}
|
|
36
|
+
return new Promise(async (resolve, reject) => {
|
|
37
|
+
const task = {
|
|
38
|
+
job,
|
|
39
|
+
resolve: resolve,
|
|
40
|
+
reject,
|
|
41
|
+
};
|
|
42
|
+
await this.preemptIfNeeded(task);
|
|
43
|
+
// ALWAYS enqueue first
|
|
44
|
+
// otherwise priority is bypassed
|
|
45
|
+
this.enqueue(task);
|
|
46
|
+
// process queue
|
|
47
|
+
this.schedule();
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
async preemptIfNeeded(incoming) {
|
|
51
|
+
if (incoming.job.priority !== 'HIGH') {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
for (const [worker, running] of this.active) {
|
|
55
|
+
if (running.priority === 'LOW') {
|
|
56
|
+
// stop LOW task
|
|
57
|
+
await worker.terminate();
|
|
58
|
+
// requeue interrupted LOW task
|
|
59
|
+
this.enqueue(running.task);
|
|
60
|
+
// remove dead worker
|
|
61
|
+
this.active.delete(worker);
|
|
62
|
+
const idx = this.workers.indexOf(worker);
|
|
63
|
+
if (idx >= 0) {
|
|
64
|
+
this.workers.splice(idx, 1);
|
|
65
|
+
}
|
|
66
|
+
// create replacement worker
|
|
67
|
+
const replacement = this.spawnWorker();
|
|
68
|
+
this.workers.push(replacement);
|
|
69
|
+
// dispatch HIGH immediately
|
|
70
|
+
this.dispatch(replacement, incoming);
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
enqueue(task) {
|
|
77
|
+
const weight = PRIORITY_WEIGHT[task.job.priority];
|
|
78
|
+
let lo = 0;
|
|
79
|
+
let hi = this.queue.length;
|
|
80
|
+
while (lo < hi) {
|
|
81
|
+
const mid = (lo + hi) >>> 1;
|
|
82
|
+
if (PRIORITY_WEIGHT[this.queue[mid].job.priority] < weight) {
|
|
83
|
+
hi = mid;
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
lo = mid + 1;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
this.queue.splice(lo, 0, task);
|
|
90
|
+
}
|
|
91
|
+
schedule() {
|
|
92
|
+
if (this.destroyed)
|
|
93
|
+
return;
|
|
94
|
+
while (this.idle.length > 0 &&
|
|
95
|
+
this.queue.length > 0) {
|
|
96
|
+
const worker = this.idle.pop();
|
|
97
|
+
// highest priority task first
|
|
98
|
+
const task = this.queue.shift();
|
|
99
|
+
this.dispatch(worker, task);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
dispatch(worker, task) {
|
|
103
|
+
let settled = false;
|
|
104
|
+
let timeoutHandle;
|
|
105
|
+
const cleanup = () => {
|
|
106
|
+
worker.removeListener('message', onMessage);
|
|
107
|
+
worker.removeListener('error', onError);
|
|
108
|
+
worker.removeListener('exit', onExit);
|
|
109
|
+
if (timeoutHandle) {
|
|
110
|
+
clearTimeout(timeoutHandle);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
const recycle = () => {
|
|
114
|
+
cleanup();
|
|
115
|
+
if (this.destroyed)
|
|
116
|
+
return;
|
|
117
|
+
this.idle.push(worker);
|
|
118
|
+
// continue processing
|
|
119
|
+
this.schedule();
|
|
120
|
+
};
|
|
121
|
+
const settle = (fn) => {
|
|
122
|
+
if (settled)
|
|
123
|
+
return;
|
|
124
|
+
settled = true;
|
|
125
|
+
Promise.resolve(fn())
|
|
126
|
+
.catch(() => { })
|
|
127
|
+
.finally(() => {
|
|
128
|
+
recycle();
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
const onMessage = (result) => {
|
|
132
|
+
settle(() => {
|
|
133
|
+
if (result.ok) {
|
|
134
|
+
task.resolve(result.data);
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
task.reject(new Error(result.error?.message ??
|
|
138
|
+
'Worker error'));
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
const onError = async (err) => {
|
|
143
|
+
settle(async () => {
|
|
144
|
+
task.reject(err);
|
|
145
|
+
await this.replaceWorker(worker);
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
const onExit = async (code) => {
|
|
149
|
+
if (this.destroyed)
|
|
150
|
+
return;
|
|
151
|
+
settle(async () => {
|
|
152
|
+
task.reject(new Error(`Worker exited unexpectedly with code ${code} ` +
|
|
153
|
+
`while running "${task.job.serviceName}.${task.job.methodName}"`));
|
|
154
|
+
await this.replaceWorker(worker);
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
worker.once('message', onMessage);
|
|
158
|
+
worker.once('error', onError);
|
|
159
|
+
worker.once('exit', onExit);
|
|
160
|
+
if (task.job.timeout &&
|
|
161
|
+
task.job.timeout > 0) {
|
|
162
|
+
timeoutHandle = setTimeout(async () => {
|
|
163
|
+
if (settled)
|
|
164
|
+
return;
|
|
165
|
+
settled = true;
|
|
166
|
+
cleanup();
|
|
167
|
+
task.reject(new Error(`Task "${task.job.serviceName}.${task.job.methodName}" timed out after ${task.job.timeout}ms`));
|
|
168
|
+
try {
|
|
169
|
+
await worker.terminate();
|
|
170
|
+
}
|
|
171
|
+
catch { }
|
|
172
|
+
await this.replaceWorker(worker);
|
|
173
|
+
this.schedule();
|
|
174
|
+
}, task.job.timeout);
|
|
175
|
+
}
|
|
176
|
+
worker.postMessage(task.job);
|
|
177
|
+
}
|
|
178
|
+
spawnWorker() {
|
|
179
|
+
return new node_worker_threads_1.Worker(node_path_1.default.resolve(__dirname, '../worker/worker-runtime.js'), {
|
|
180
|
+
workerData: {
|
|
181
|
+
services: this.services,
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
async replaceWorker(oldWorker) {
|
|
186
|
+
const workerIndex = this.workers.indexOf(oldWorker);
|
|
187
|
+
if (workerIndex >= 0) {
|
|
188
|
+
this.workers.splice(workerIndex, 1);
|
|
189
|
+
}
|
|
190
|
+
const idleIndex = this.idle.indexOf(oldWorker);
|
|
191
|
+
if (idleIndex >= 0) {
|
|
192
|
+
this.idle.splice(idleIndex, 1);
|
|
193
|
+
}
|
|
194
|
+
const newWorker = this.spawnWorker();
|
|
195
|
+
this.workers.push(newWorker);
|
|
196
|
+
this.idle.push(newWorker);
|
|
197
|
+
}
|
|
198
|
+
async destroy() {
|
|
199
|
+
this.destroyed = true;
|
|
200
|
+
for (const queued of this.queue) {
|
|
201
|
+
queued.reject(new Error('WorkerPool destroyed'));
|
|
202
|
+
}
|
|
203
|
+
this.queue.length = 0;
|
|
204
|
+
await Promise.allSettled(this.workers.map((worker) => worker.terminate()));
|
|
205
|
+
this.workers.length = 0;
|
|
206
|
+
this.idle.length = 0;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
exports.WorkerPool = WorkerPool;
|
|
210
|
+
//# sourceMappingURL=worker.pool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.pool.js","sourceRoot":"","sources":["../../src/core/worker.pool.ts"],"names":[],"mappings":";;;;;;AAAA,6DAA6C;AAC7C,sDAAyB;AACzB,0DAA6B;AAU7B,MAAM,eAAe,GAAiC;IACpD,IAAI,EAAE,CAAC;IACP,MAAM,EAAE,CAAC;IACT,GAAG,EAAE,CAAC;CACP,CAAC;AAcF,MAAa,UAAU;IAYF;IACA;IAZF,OAAO,GAAa,EAAE,CAAC;IAEvB,IAAI,GAAa,EAAE,CAAC;IAEpB,KAAK,GAAkB,EAAE,CAAC;IAEnC,SAAS,GAAG,KAAK,CAAC;IACT,MAAM,GACrB,IAAI,GAAG,EAAuB,CAAC;IAEjC,YACmB,QAA6B,EAC7B,OAAO,iBAAE,CAAC,IAAI,EAAE,CAAC,MAAM;QADvB,aAAQ,GAAR,QAAQ,CAA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AE,CAAC;YACrB,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;QACtC,CAAC;QAED,MAAM,SAAS,GACb,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAE/B,IAAI,SAAS,IAAI,CAAC,EAAE,CAAC;YACnB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;QACjC,CAAC;QAED,MAAM,SAAS,GAAG,IAAI,CAAC,WAAW,EAAE,CAAC;QAErC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAE7B,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,OAAO;QACX,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;QAEtB,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAChC,MAAM,CAAC,MAAM,CACX,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAClC,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;QAEtB,MAAM,OAAO,CAAC,UAAU,CACtB,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAC1B,MAAM,CAAC,SAAS,EAAE,CACnB,CACF,CAAC;QAEF,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC;IACvB,CAAC;CACF;AA9TD,gCA8TC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { OnModuleDestroy } from '@nestjs/common';
|
|
2
|
+
import { WorkerDiscoveryService } from '../discovery/discovery.service';
|
|
3
|
+
import type { TaskPriority, WorkerModuleOptions } from './worker.interfaces';
|
|
4
|
+
/**
|
|
5
|
+
* WorkerService
|
|
6
|
+
*
|
|
7
|
+
* Main entry point for executing worker-thread tasks.
|
|
8
|
+
*
|
|
9
|
+
* Features:
|
|
10
|
+
* - Lazy worker pool initialization
|
|
11
|
+
* - Automatic worker discovery
|
|
12
|
+
* - Priority queue support
|
|
13
|
+
* - Timeout support
|
|
14
|
+
* - Worker thread execution
|
|
15
|
+
* - NestJS dependency integration
|
|
16
|
+
*
|
|
17
|
+
* Lifecycle:
|
|
18
|
+
* 1. Discovers worker-enabled services
|
|
19
|
+
* 2. Serializes metadata for worker runtime
|
|
20
|
+
* 3. Creates WorkerPool
|
|
21
|
+
* 4. Dispatches tasks to workers
|
|
22
|
+
* 5. Handles graceful shutdown
|
|
23
|
+
*
|
|
24
|
+
* Example:
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* await workerService.run(
|
|
28
|
+
* 'ImageService',
|
|
29
|
+
* 'resizeImage',
|
|
30
|
+
* [500],
|
|
31
|
+
* {
|
|
32
|
+
* priority: 'HIGH',
|
|
33
|
+
* timeout: 5000,
|
|
34
|
+
* },
|
|
35
|
+
* );
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare class WorkerService implements OnModuleDestroy {
|
|
39
|
+
private readonly discovery;
|
|
40
|
+
private readonly options;
|
|
41
|
+
private pool;
|
|
42
|
+
private readonly taskOptions;
|
|
43
|
+
constructor(discovery: WorkerDiscoveryService, options: WorkerModuleOptions);
|
|
44
|
+
private initPool;
|
|
45
|
+
/**
|
|
46
|
+
* Executes a worker task.
|
|
47
|
+
*
|
|
48
|
+
* Parameters:
|
|
49
|
+
* - serviceName: target service
|
|
50
|
+
* - methodName: target method
|
|
51
|
+
* - args: serialized arguments
|
|
52
|
+
* - overrides: runtime priority/timeout overrides
|
|
53
|
+
*
|
|
54
|
+
* Priority:
|
|
55
|
+
* - HIGH
|
|
56
|
+
* - NORMAL
|
|
57
|
+
* - LOW
|
|
58
|
+
*
|
|
59
|
+
* Timeout:
|
|
60
|
+
* Automatically terminates timed-out workers.
|
|
61
|
+
*
|
|
62
|
+
* Example:
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* await workerService.run(
|
|
66
|
+
* 'ImageService',
|
|
67
|
+
* 'generateThumbnail',
|
|
68
|
+
* [1920, 1080],
|
|
69
|
+
* {
|
|
70
|
+
* priority: 'HIGH',
|
|
71
|
+
* timeout: 3000,
|
|
72
|
+
* },
|
|
73
|
+
* );
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
run<T = unknown>(serviceName: string, methodName: string, args?: unknown[], overrides?: {
|
|
77
|
+
priority?: TaskPriority;
|
|
78
|
+
timeout?: number;
|
|
79
|
+
}): Promise<T>;
|
|
80
|
+
/**
|
|
81
|
+
* Gracefully shuts down worker pool.
|
|
82
|
+
*
|
|
83
|
+
* Called automatically by NestJS
|
|
84
|
+
* during application shutdown.
|
|
85
|
+
*/
|
|
86
|
+
onModuleDestroy(): Promise<void>;
|
|
87
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.WorkerService = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const worker_pool_1 = require("./worker.pool");
|
|
18
|
+
const discovery_service_1 = require("../discovery/discovery.service");
|
|
19
|
+
const di_serializer_1 = require("../di/di-serializer");
|
|
20
|
+
/**
|
|
21
|
+
* WorkerService
|
|
22
|
+
*
|
|
23
|
+
* Main entry point for executing worker-thread tasks.
|
|
24
|
+
*
|
|
25
|
+
* Features:
|
|
26
|
+
* - Lazy worker pool initialization
|
|
27
|
+
* - Automatic worker discovery
|
|
28
|
+
* - Priority queue support
|
|
29
|
+
* - Timeout support
|
|
30
|
+
* - Worker thread execution
|
|
31
|
+
* - NestJS dependency integration
|
|
32
|
+
*
|
|
33
|
+
* Lifecycle:
|
|
34
|
+
* 1. Discovers worker-enabled services
|
|
35
|
+
* 2. Serializes metadata for worker runtime
|
|
36
|
+
* 3. Creates WorkerPool
|
|
37
|
+
* 4. Dispatches tasks to workers
|
|
38
|
+
* 5. Handles graceful shutdown
|
|
39
|
+
*
|
|
40
|
+
* Example:
|
|
41
|
+
*
|
|
42
|
+
* ```ts
|
|
43
|
+
* await workerService.run(
|
|
44
|
+
* 'ImageService',
|
|
45
|
+
* 'resizeImage',
|
|
46
|
+
* [500],
|
|
47
|
+
* {
|
|
48
|
+
* priority: 'HIGH',
|
|
49
|
+
* timeout: 5000,
|
|
50
|
+
* },
|
|
51
|
+
* );
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
let WorkerService = class WorkerService {
|
|
55
|
+
discovery;
|
|
56
|
+
options;
|
|
57
|
+
pool = null;
|
|
58
|
+
taskOptions = new Map();
|
|
59
|
+
constructor(discovery, options) {
|
|
60
|
+
this.discovery = discovery;
|
|
61
|
+
this.options = options;
|
|
62
|
+
}
|
|
63
|
+
initPool() {
|
|
64
|
+
if (this.pool)
|
|
65
|
+
return;
|
|
66
|
+
const tasks = this.discovery.scan();
|
|
67
|
+
for (const task of tasks) {
|
|
68
|
+
this.taskOptions.set(`${task.serviceName}.${task.methodName}`, { priority: task.priority, timeout: task.timeout });
|
|
69
|
+
}
|
|
70
|
+
const serialized = (0, di_serializer_1.serializeForWorker)(tasks);
|
|
71
|
+
this.pool = new worker_pool_1.WorkerPool(serialized, this.options.poolSize);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Executes a worker task.
|
|
75
|
+
*
|
|
76
|
+
* Parameters:
|
|
77
|
+
* - serviceName: target service
|
|
78
|
+
* - methodName: target method
|
|
79
|
+
* - args: serialized arguments
|
|
80
|
+
* - overrides: runtime priority/timeout overrides
|
|
81
|
+
*
|
|
82
|
+
* Priority:
|
|
83
|
+
* - HIGH
|
|
84
|
+
* - NORMAL
|
|
85
|
+
* - LOW
|
|
86
|
+
*
|
|
87
|
+
* Timeout:
|
|
88
|
+
* Automatically terminates timed-out workers.
|
|
89
|
+
*
|
|
90
|
+
* Example:
|
|
91
|
+
*
|
|
92
|
+
* ```ts
|
|
93
|
+
* await workerService.run(
|
|
94
|
+
* 'ImageService',
|
|
95
|
+
* 'generateThumbnail',
|
|
96
|
+
* [1920, 1080],
|
|
97
|
+
* {
|
|
98
|
+
* priority: 'HIGH',
|
|
99
|
+
* timeout: 3000,
|
|
100
|
+
* },
|
|
101
|
+
* );
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
run(serviceName, methodName, args = [], overrides = {}) {
|
|
105
|
+
this.initPool();
|
|
106
|
+
const key = `${serviceName}.${methodName}`;
|
|
107
|
+
const defaults = this.taskOptions.get(key) ??
|
|
108
|
+
{ priority: 'NORMAL' };
|
|
109
|
+
return this.pool.execute({
|
|
110
|
+
serviceName,
|
|
111
|
+
methodName,
|
|
112
|
+
args,
|
|
113
|
+
priority: overrides.priority ?? defaults.priority,
|
|
114
|
+
timeout: overrides.timeout ?? defaults.timeout,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Gracefully shuts down worker pool.
|
|
119
|
+
*
|
|
120
|
+
* Called automatically by NestJS
|
|
121
|
+
* during application shutdown.
|
|
122
|
+
*/
|
|
123
|
+
async onModuleDestroy() {
|
|
124
|
+
await this.pool?.destroy();
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
exports.WorkerService = WorkerService;
|
|
128
|
+
exports.WorkerService = WorkerService = __decorate([
|
|
129
|
+
(0, common_1.Injectable)(),
|
|
130
|
+
__param(1, (0, common_1.Inject)('WORKER_OPTIONS')),
|
|
131
|
+
__metadata("design:paramtypes", [discovery_service_1.WorkerDiscoveryService, Object])
|
|
132
|
+
], WorkerService);
|
|
133
|
+
//# sourceMappingURL=worker.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.service.js","sourceRoot":"","sources":["../../src/core/worker.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,2CAAmE;AACnE,+CAAyC;AACzC,sEAAsE;AACtE,uDAAuD;AAGvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEI,IAAM,aAAa,GAAnB,MAAM,aAAa;IAWL;IAEA;IAZX,IAAI,GAAsB,IAAI,CAAC;IACtB,WAAW,GAAG,IAAI,GAAG,EAMnC,CAAC;IAEJ,YACmB,SAAiC,EAEjC,OAA4B;QAF5B,cAAS,GAAT,SAAS,CAAwB;QAEjC,YAAO,GAAP,OAAO,CAAqB;IAE/C,CAAC;IAEO,QAAQ;QACd,IAAI,IAAI,CAAC,IAAI;YAAE,OAAO;QAEtB,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;QAEpC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YACzB,IAAI,CAAC,WAAW,CAAC,GAAG,CAClB,GAAG,IAAI,CAAC,WAAW,IAAI,IAAI,CAAC,UAAU,EAAE,EACxC,EAAC,QAAQ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_META = "worker:method";
|
|
4
|
+
export declare const WORKER_DEPS_META = "worker:deps";
|
|
5
|
+
export interface WorkerTaskOptions {
|
|
6
|
+
priority?: 'HIGH' | 'NORMAL' | 'LOW';
|
|
7
|
+
timeout?: number;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* @WorkerClass() – marks a provider as a container of worker tasks.
|
|
11
|
+
*
|
|
12
|
+
* The discovery service scans all NestJS providers for this marker,
|
|
13
|
+
* then inspects each method for @WorkerTask().
|
|
14
|
+
*
|
|
15
|
+
* Declare any injectable deps that your @WorkerTask methods need via
|
|
16
|
+
* the `deps` option. These are resolved from the live NestJS container,
|
|
17
|
+
* serialised, and reconstructed inside each worker thread.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* @WorkerClass({ deps: [ConfigService] })
|
|
21
|
+
* export class ImageService { ... }
|
|
22
|
+
*/
|
|
23
|
+
export declare function WorkerClass(options?: {
|
|
24
|
+
deps?: (abstract new (...a: unknown[]) => unknown)[];
|
|
25
|
+
}): ClassDecorator;
|
|
26
|
+
/**
|
|
27
|
+
* @WorkerTask() – marks a method to be offloaded to a worker thread.
|
|
28
|
+
*
|
|
29
|
+
* The method must be pure with respect to thread-crossing:
|
|
30
|
+
* its arguments and return value must be structured-clone compatible.
|
|
31
|
+
* Any NestJS deps it needs should be declared on @WorkerClass({ deps }).
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* @WorkerTask({ priority: 'HIGH', timeout: 5000 })
|
|
35
|
+
* resizeImage(value: number): number { ... }
|
|
36
|
+
*/
|
|
37
|
+
export declare function WorkerTask(options?: WorkerTaskOptions): MethodDecorator;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WORKER_DEPS_META = exports.WORKER_METHOD_META = exports.WORKER_CLASS_META = void 0;
|
|
4
|
+
exports.WorkerClass = WorkerClass;
|
|
5
|
+
exports.WorkerTask = WorkerTask;
|
|
6
|
+
require("reflect-metadata");
|
|
7
|
+
exports.WORKER_CLASS_META = 'worker:class';
|
|
8
|
+
exports.WORKER_METHOD_META = 'worker:method';
|
|
9
|
+
exports.WORKER_DEPS_META = 'worker:deps';
|
|
10
|
+
/**
|
|
11
|
+
* @WorkerClass() – marks a provider as a container of worker tasks.
|
|
12
|
+
*
|
|
13
|
+
* The discovery service scans all NestJS providers for this marker,
|
|
14
|
+
* then inspects each method for @WorkerTask().
|
|
15
|
+
*
|
|
16
|
+
* Declare any injectable deps that your @WorkerTask methods need via
|
|
17
|
+
* the `deps` option. These are resolved from the live NestJS container,
|
|
18
|
+
* serialised, and reconstructed inside each worker thread.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* @WorkerClass({ deps: [ConfigService] })
|
|
22
|
+
* export class ImageService { ... }
|
|
23
|
+
*/
|
|
24
|
+
function WorkerClass(options = {}) {
|
|
25
|
+
return (target) => {
|
|
26
|
+
Reflect.defineMetadata(exports.WORKER_CLASS_META, true, target);
|
|
27
|
+
if (options.deps?.length) {
|
|
28
|
+
Reflect.defineMetadata(exports.WORKER_DEPS_META, options.deps, target);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* @WorkerTask() – marks a method to be offloaded to a worker thread.
|
|
34
|
+
*
|
|
35
|
+
* The method must be pure with respect to thread-crossing:
|
|
36
|
+
* its arguments and return value must be structured-clone compatible.
|
|
37
|
+
* Any NestJS deps it needs should be declared on @WorkerClass({ deps }).
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* @WorkerTask({ priority: 'HIGH', timeout: 5000 })
|
|
41
|
+
* resizeImage(value: number): number { ... }
|
|
42
|
+
*/
|
|
43
|
+
function WorkerTask(options = {}) {
|
|
44
|
+
return (target, propertyKey) => {
|
|
45
|
+
Reflect.defineMetadata(exports.WORKER_METHOD_META, options, target, propertyKey);
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=worker-task.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker-task.decorator.js","sourceRoot":"","sources":["../../src/decorators/worker-task.decorator.ts"],"names":[],"mappings":";;;AAyBA,kCASC;AAaD,gCAIC;AAnDD,4BAA0B;AAEb,QAAA,iBAAiB,GAAG,cAAc,CAAC;AACnC,QAAA,kBAAkB,GAAG,eAAe,CAAC;AACrC,QAAA,gBAAgB,GAAG,aAAa,CAAC;AAO9C;;;;;;;;;;;;;GAaG;AACH,SAAgB,WAAW,CACzB,UAAoE,EAAE;IAEtE,OAAO,CAAC,MAAM,EAAE,EAAE;QAChB,OAAO,CAAC,cAAc,CAAC,yBAAiB,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;QACxD,IAAI,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,CAAC;YACzB,OAAO,CAAC,cAAc,CAAC,wBAAgB,EAAE,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QACjE,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CAAC,UAA6B,EAAE;IACxD,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,EAAE;QAC7B,OAAO,CAAC,cAAc,CAAC,0BAAkB,EAAE,OAAO,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;IAC3E,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { DiscoveredTask } from '../core/worker.interfaces';
|
|
2
|
+
import type { SerializedService } from './worker-container';
|
|
3
|
+
/**
|
|
4
|
+
* serializeForWorker – converts the live NestJS service graph into a
|
|
5
|
+
* structured-clone-safe SerializedService[] payload for workerData.
|
|
6
|
+
*
|
|
7
|
+
* KEY DESIGN DECISION: class source extraction instead of file paths.
|
|
8
|
+
*
|
|
9
|
+
* We cannot pass filePaths to workers because require(filePath) inside a
|
|
10
|
+
* worker thread re-executes the compiled .ts output which imports @nestjs/common
|
|
11
|
+
* at the top level — crashing the isolated worker context.
|
|
12
|
+
*
|
|
13
|
+
* Instead we extract each class's constructor and prototype methods as plain JS
|
|
14
|
+
* source strings on the main thread (where NestJS is fully loaded), then send
|
|
15
|
+
* those strings via workerData. WorkerContainer eval()s them in the worker —
|
|
16
|
+
* no require(), no NestJS imports, no crash.
|
|
17
|
+
*/
|
|
18
|
+
export declare function serializeForWorker(tasks: DiscoveredTask[]): SerializedService[];
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.serializeForWorker = serializeForWorker;
|
|
4
|
+
const worker_task_decorator_1 = require("../decorators/worker-task.decorator");
|
|
5
|
+
/**
|
|
6
|
+
* serializeForWorker – converts the live NestJS service graph into a
|
|
7
|
+
* structured-clone-safe SerializedService[] payload for workerData.
|
|
8
|
+
*
|
|
9
|
+
* KEY DESIGN DECISION: class source extraction instead of file paths.
|
|
10
|
+
*
|
|
11
|
+
* We cannot pass filePaths to workers because require(filePath) inside a
|
|
12
|
+
* worker thread re-executes the compiled .ts output which imports @nestjs/common
|
|
13
|
+
* at the top level — crashing the isolated worker context.
|
|
14
|
+
*
|
|
15
|
+
* Instead we extract each class's constructor and prototype methods as plain JS
|
|
16
|
+
* source strings on the main thread (where NestJS is fully loaded), then send
|
|
17
|
+
* those strings via workerData. WorkerContainer eval()s them in the worker —
|
|
18
|
+
* no require(), no NestJS imports, no crash.
|
|
19
|
+
*/
|
|
20
|
+
function serializeForWorker(tasks) {
|
|
21
|
+
const byService = new Map();
|
|
22
|
+
for (const task of tasks) {
|
|
23
|
+
if (!byService.has(task.serviceName)) {
|
|
24
|
+
byService.set(task.serviceName, { representative: task, methods: [] });
|
|
25
|
+
}
|
|
26
|
+
byService.get(task.serviceName).methods.push({
|
|
27
|
+
methodName: task.methodName,
|
|
28
|
+
priority: task.priority,
|
|
29
|
+
timeout: task.timeout,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const result = [];
|
|
33
|
+
for (const [serviceName, { representative, methods }] of byService) {
|
|
34
|
+
const { metatype, instance, deps } = representative;
|
|
35
|
+
const depTypes = Reflect.getMetadata(worker_task_decorator_1.WORKER_DEPS_META, metatype) ?? [];
|
|
36
|
+
const serializedDeps = depTypes.map((DepType, i) => {
|
|
37
|
+
const depInstance = deps[i];
|
|
38
|
+
const propertyKey = findDepPropertyKey(instance, depInstance) ?? camelCase(DepType.name);
|
|
39
|
+
return {
|
|
40
|
+
name: DepType.name,
|
|
41
|
+
classSource: extractClassSource(DepType),
|
|
42
|
+
snapshot: snapshotInstance(depInstance),
|
|
43
|
+
propertyKey,
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
result.push({
|
|
47
|
+
name: serviceName,
|
|
48
|
+
classSource: extractClassSource(metatype),
|
|
49
|
+
methods,
|
|
50
|
+
deps: serializedDeps,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Extract a plain, self-contained class source string from a constructor.
|
|
57
|
+
*
|
|
58
|
+
* In TypeScript compiled output, Function.prototype.toString() on a class
|
|
59
|
+
* returns the full class body including all methods — but WITHOUT the
|
|
60
|
+
* import statements or decorator calls at the top of the file.
|
|
61
|
+
*
|
|
62
|
+
* Example output:
|
|
63
|
+
* "class ConfigService {
|
|
64
|
+
* constructor() { this.config = { MULTIPLIER: '3' }; }
|
|
65
|
+
* get(k) { return this.config[k]; }
|
|
66
|
+
* getNumber(k) { return Number(this.config[k]); }
|
|
67
|
+
* }"
|
|
68
|
+
*
|
|
69
|
+
* This is eval()-safe and has no NestJS dependencies.
|
|
70
|
+
*/
|
|
71
|
+
function extractClassSource(metatype) {
|
|
72
|
+
const src = metatype.toString();
|
|
73
|
+
// Compiled TS classes always start with "class ClassName"
|
|
74
|
+
// Strip any leading helper assignments TypeScript sometimes emits
|
|
75
|
+
const classStart = src.indexOf('class ');
|
|
76
|
+
if (classStart === -1) {
|
|
77
|
+
throw new Error(`Could not extract class source for "${metatype.name}". ` +
|
|
78
|
+
`Ensure the project is compiled with "target": "ES2022" or higher ` +
|
|
79
|
+
`so classes are emitted as native class declarations, not functions.`);
|
|
80
|
+
}
|
|
81
|
+
return src.slice(classStart);
|
|
82
|
+
}
|
|
83
|
+
function findDepPropertyKey(serviceInstance, depInstance) {
|
|
84
|
+
if (!serviceInstance || typeof serviceInstance !== 'object')
|
|
85
|
+
return undefined;
|
|
86
|
+
for (const [key, value] of Object.entries(serviceInstance)) {
|
|
87
|
+
if (value === depInstance)
|
|
88
|
+
return key;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
function snapshotInstance(instance) {
|
|
93
|
+
if (!instance || typeof instance !== 'object')
|
|
94
|
+
return {};
|
|
95
|
+
const out = {};
|
|
96
|
+
for (const [k, v] of Object.entries(instance)) {
|
|
97
|
+
if (typeof v === 'function' || typeof v === 'symbol')
|
|
98
|
+
continue;
|
|
99
|
+
try {
|
|
100
|
+
structuredClone(v);
|
|
101
|
+
out[k] = v;
|
|
102
|
+
}
|
|
103
|
+
catch { /* skip non-cloneable */ }
|
|
104
|
+
}
|
|
105
|
+
return out;
|
|
106
|
+
}
|
|
107
|
+
function camelCase(name) {
|
|
108
|
+
return name.charAt(0).toLowerCase() + name.slice(1);
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=di-serializer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"di-serializer.js","sourceRoot":"","sources":["../../src/di/di-serializer.ts"],"names":[],"mappings":";;AAmBA,gDA8CC;AA/DD,+EAAuE;AAEvE;;;;;;;;;;;;;;GAcG;AACH,SAAgB,kBAAkB,CAAC,KAAuB;IACxD,MAAM,SAAS,GAAG,IAAI,GAAG,EAGtB,CAAC;IAEJ,KAAK,MAAM,IAAI,IAA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