bunqueue 1.9.0 → 1.9.2
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/dist/application/operations/queueControl.d.ts.map +1 -1
- package/dist/application/operations/queueControl.js +6 -1
- package/dist/application/operations/queueControl.js.map +1 -1
- package/dist/application/queueManager.d.ts +115 -9
- package/dist/application/queueManager.d.ts.map +1 -1
- package/dist/application/queueManager.js +535 -26
- package/dist/application/queueManager.js.map +1 -1
- package/dist/cli/commands/server.d.ts.map +1 -1
- package/dist/cli/commands/server.js +4 -9
- package/dist/cli/commands/server.js.map +1 -1
- package/dist/client/index.d.ts +1 -0
- package/dist/client/index.d.ts.map +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/client/queue/dlqOps.d.ts +24 -0
- package/dist/client/queue/dlqOps.d.ts.map +1 -0
- package/dist/client/queue/dlqOps.js +73 -0
- package/dist/client/queue/dlqOps.js.map +1 -0
- package/dist/client/queue/helpers.d.ts +20 -0
- package/dist/client/queue/helpers.d.ts.map +1 -0
- package/dist/client/queue/helpers.js +34 -0
- package/dist/client/queue/helpers.js.map +1 -0
- package/dist/client/queue/index.d.ts +8 -0
- package/dist/client/queue/index.d.ts.map +1 -0
- package/dist/client/queue/index.js +8 -0
- package/dist/client/queue/index.js.map +1 -0
- package/dist/client/queue/queue.d.ts +60 -0
- package/dist/client/queue/queue.d.ts.map +1 -0
- package/dist/client/queue/queue.js +322 -0
- package/dist/client/queue/queue.js.map +1 -0
- package/dist/client/queue.d.ts +3 -78
- package/dist/client/queue.d.ts.map +1 -1
- package/dist/client/queue.js +3 -463
- package/dist/client/queue.js.map +1 -1
- package/dist/client/sandboxed/index.d.ts +8 -0
- package/dist/client/sandboxed/index.d.ts.map +1 -0
- package/dist/client/sandboxed/index.js +7 -0
- package/dist/client/sandboxed/index.js.map +1 -0
- package/dist/client/sandboxed/types.d.ts +62 -0
- package/dist/client/sandboxed/types.d.ts.map +1 -0
- package/dist/client/sandboxed/types.js +6 -0
- package/dist/client/sandboxed/types.js.map +1 -0
- package/dist/client/sandboxed/worker.d.ts +38 -0
- package/dist/client/sandboxed/worker.d.ts.map +1 -0
- package/dist/client/sandboxed/worker.js +176 -0
- package/dist/client/sandboxed/worker.js.map +1 -0
- package/dist/client/sandboxed/wrapper.d.ts +13 -0
- package/dist/client/sandboxed/wrapper.d.ts.map +1 -0
- package/dist/client/sandboxed/wrapper.js +65 -0
- package/dist/client/sandboxed/wrapper.js.map +1 -0
- package/dist/client/sandboxedWorker.d.ts +4 -87
- package/dist/client/sandboxedWorker.d.ts.map +1 -1
- package/dist/client/sandboxedWorker.js +3 -296
- package/dist/client/sandboxedWorker.js.map +1 -1
- package/dist/client/tcp/client.d.ts +43 -0
- package/dist/client/tcp/client.d.ts.map +1 -0
- package/dist/client/tcp/client.js +288 -0
- package/dist/client/tcp/client.js.map +1 -0
- package/dist/client/tcp/connection.d.ts +48 -0
- package/dist/client/tcp/connection.d.ts.map +1 -0
- package/dist/client/tcp/connection.js +150 -0
- package/dist/client/tcp/connection.js.map +1 -0
- package/dist/client/tcp/health.d.ts +47 -0
- package/dist/client/tcp/health.d.ts.map +1 -0
- package/dist/client/tcp/health.js +95 -0
- package/dist/client/tcp/health.js.map +1 -0
- package/dist/client/tcp/index.d.ts +13 -0
- package/dist/client/tcp/index.d.ts.map +1 -0
- package/dist/client/tcp/index.js +12 -0
- package/dist/client/tcp/index.js.map +1 -0
- package/dist/client/tcp/lineBuffer.d.ts +17 -0
- package/dist/client/tcp/lineBuffer.d.ts.map +1 -0
- package/dist/client/tcp/lineBuffer.js +32 -0
- package/dist/client/tcp/lineBuffer.js.map +1 -0
- package/dist/client/tcp/reconnect.d.ts +38 -0
- package/dist/client/tcp/reconnect.d.ts.map +1 -0
- package/dist/client/tcp/reconnect.js +70 -0
- package/dist/client/tcp/reconnect.js.map +1 -0
- package/dist/client/tcp/shared.d.ts +11 -0
- package/dist/client/tcp/shared.d.ts.map +1 -0
- package/dist/client/tcp/shared.js +20 -0
- package/dist/client/tcp/shared.js.map +1 -0
- package/dist/client/tcp/types.d.ts +69 -0
- package/dist/client/tcp/types.d.ts.map +1 -0
- package/dist/client/tcp/types.js +19 -0
- package/dist/client/tcp/types.js.map +1 -0
- package/dist/client/tcpClient.d.ts +4 -71
- package/dist/client/tcpClient.d.ts.map +1 -1
- package/dist/client/tcpClient.js +3 -405
- package/dist/client/tcpClient.js.map +1 -1
- package/dist/client/tcpPool.d.ts +14 -1
- package/dist/client/tcpPool.d.ts.map +1 -1
- package/dist/client/tcpPool.js +37 -2
- package/dist/client/tcpPool.js.map +1 -1
- package/dist/client/types.d.ts +7 -0
- package/dist/client/types.d.ts.map +1 -1
- package/dist/client/types.js.map +1 -1
- package/dist/client/worker/ackBatcher.d.ts +40 -0
- package/dist/client/worker/ackBatcher.d.ts.map +1 -0
- package/dist/client/worker/ackBatcher.js +137 -0
- package/dist/client/worker/ackBatcher.js.map +1 -0
- package/dist/client/worker/index.d.ts +11 -0
- package/dist/client/worker/index.d.ts.map +1 -0
- package/dist/client/worker/index.js +10 -0
- package/dist/client/worker/index.js.map +1 -0
- package/dist/client/worker/jobParser.d.ts +10 -0
- package/dist/client/worker/jobParser.d.ts.map +1 -0
- package/dist/client/worker/jobParser.js +43 -0
- package/dist/client/worker/jobParser.js.map +1 -0
- package/dist/client/worker/processor.d.ts +24 -0
- package/dist/client/worker/processor.d.ts.map +1 -0
- package/dist/client/worker/processor.js +86 -0
- package/dist/client/worker/processor.js.map +1 -0
- package/dist/client/worker/types.d.ts +38 -0
- package/dist/client/worker/types.d.ts.map +1 -0
- package/dist/client/worker/types.js +14 -0
- package/dist/client/worker/types.js.map +1 -0
- package/dist/client/worker/worker.d.ts +53 -0
- package/dist/client/worker/worker.d.ts.map +1 -0
- package/dist/client/worker/worker.js +367 -0
- package/dist/client/worker/worker.js.map +1 -0
- package/dist/client/worker.d.ts +3 -69
- package/dist/client/worker.d.ts.map +1 -1
- package/dist/client/worker.js +3 -472
- package/dist/client/worker.js.map +1 -1
- package/dist/domain/queue/shard.d.ts +19 -2
- package/dist/domain/queue/shard.d.ts.map +1 -1
- package/dist/domain/queue/shard.js +36 -4
- package/dist/domain/queue/shard.js.map +1 -1
- package/dist/domain/types/command.d.ts +9 -0
- package/dist/domain/types/command.d.ts.map +1 -1
- package/dist/domain/types/job.d.ts +27 -0
- package/dist/domain/types/job.d.ts.map +1 -1
- package/dist/domain/types/job.js +34 -0
- package/dist/domain/types/job.js.map +1 -1
- package/dist/domain/types/response.d.ts +15 -1
- package/dist/domain/types/response.d.ts.map +1 -1
- package/dist/domain/types/response.js +16 -0
- package/dist/domain/types/response.js.map +1 -1
- package/dist/infrastructure/server/handlers/core.d.ts +1 -1
- package/dist/infrastructure/server/handlers/core.d.ts.map +1 -1
- package/dist/infrastructure/server/handlers/core.js +74 -15
- package/dist/infrastructure/server/handlers/core.js.map +1 -1
- package/dist/infrastructure/server/handlers/monitoring.d.ts.map +1 -1
- package/dist/infrastructure/server/handlers/monitoring.js +6 -4
- package/dist/infrastructure/server/handlers/monitoring.js.map +1 -1
- package/dist/infrastructure/server/http.d.ts.map +1 -1
- package/dist/infrastructure/server/http.js +67 -0
- package/dist/infrastructure/server/http.js.map +1 -1
- package/dist/infrastructure/server/tcp.d.ts.map +1 -1
- package/dist/infrastructure/server/tcp.js +9 -1
- package/dist/infrastructure/server/tcp.js.map +1 -1
- package/dist/infrastructure/server/types.d.ts +2 -0
- package/dist/infrastructure/server/types.d.ts.map +1 -1
- package/dist/main.js +8 -0
- package/dist/main.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sandboxed Worker
|
|
3
|
+
* Runs job processors in isolated Bun Worker processes
|
|
4
|
+
*/
|
|
5
|
+
import { getSharedManager } from '../manager';
|
|
6
|
+
import { createWrapperScript, cleanupWrapperScript } from './wrapper';
|
|
7
|
+
/**
|
|
8
|
+
* Sandboxed Worker - runs processors in isolated Bun Worker processes
|
|
9
|
+
*/
|
|
10
|
+
export class SandboxedWorker {
|
|
11
|
+
queueName;
|
|
12
|
+
options;
|
|
13
|
+
workers = [];
|
|
14
|
+
running = false;
|
|
15
|
+
pullPromise = null;
|
|
16
|
+
wrapperPath = null;
|
|
17
|
+
manager;
|
|
18
|
+
constructor(queueName, options) {
|
|
19
|
+
this.queueName = queueName;
|
|
20
|
+
this.manager = options.manager ?? getSharedManager();
|
|
21
|
+
this.options = {
|
|
22
|
+
processor: options.processor,
|
|
23
|
+
concurrency: options.concurrency ?? 1,
|
|
24
|
+
maxMemory: options.maxMemory ?? 256,
|
|
25
|
+
timeout: options.timeout ?? 30000,
|
|
26
|
+
autoRestart: options.autoRestart ?? true,
|
|
27
|
+
maxRestarts: options.maxRestarts ?? 10,
|
|
28
|
+
pollInterval: options.pollInterval ?? 10,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** Start the sandboxed worker pool */
|
|
32
|
+
start() {
|
|
33
|
+
if (this.running)
|
|
34
|
+
return;
|
|
35
|
+
this.running = true;
|
|
36
|
+
this.wrapperPath = createWrapperScript(this.queueName, this.options.processor);
|
|
37
|
+
for (let i = 0; i < this.options.concurrency; i++) {
|
|
38
|
+
this.spawnWorker(i);
|
|
39
|
+
}
|
|
40
|
+
this.pullPromise = this.pullLoop();
|
|
41
|
+
}
|
|
42
|
+
/** Stop all workers gracefully */
|
|
43
|
+
async stop() {
|
|
44
|
+
this.running = false;
|
|
45
|
+
for (const wp of this.workers) {
|
|
46
|
+
if (wp.timeoutId)
|
|
47
|
+
clearTimeout(wp.timeoutId);
|
|
48
|
+
wp.worker.terminate();
|
|
49
|
+
}
|
|
50
|
+
this.workers.length = 0;
|
|
51
|
+
if (this.pullPromise)
|
|
52
|
+
await this.pullPromise;
|
|
53
|
+
cleanupWrapperScript(this.wrapperPath);
|
|
54
|
+
}
|
|
55
|
+
/** Get worker pool stats */
|
|
56
|
+
getStats() {
|
|
57
|
+
const busy = this.workers.filter((w) => w.busy).length;
|
|
58
|
+
const restarts = this.workers.reduce((sum, w) => sum + w.restarts, 0);
|
|
59
|
+
return { total: this.workers.length, busy, idle: this.workers.length - busy, restarts };
|
|
60
|
+
}
|
|
61
|
+
spawnWorker(index) {
|
|
62
|
+
if (!this.wrapperPath)
|
|
63
|
+
return;
|
|
64
|
+
const worker = new Worker(this.wrapperPath, { smol: this.options.maxMemory <= 64 });
|
|
65
|
+
const wp = {
|
|
66
|
+
worker,
|
|
67
|
+
busy: false,
|
|
68
|
+
currentJob: null,
|
|
69
|
+
restarts: this.workers[index]?.restarts ?? 0,
|
|
70
|
+
timeoutId: null,
|
|
71
|
+
};
|
|
72
|
+
worker.onmessage = (event) => {
|
|
73
|
+
this.handleMessage(wp, event.data);
|
|
74
|
+
};
|
|
75
|
+
worker.onerror = (error) => {
|
|
76
|
+
console.error(`[SandboxedWorker] Worker ${index} error:`, error.message);
|
|
77
|
+
this.handleCrash(wp, index);
|
|
78
|
+
};
|
|
79
|
+
if (this.workers[index])
|
|
80
|
+
this.workers[index] = wp;
|
|
81
|
+
else
|
|
82
|
+
this.workers.push(wp);
|
|
83
|
+
}
|
|
84
|
+
async pullLoop() {
|
|
85
|
+
while (this.running) {
|
|
86
|
+
const idle = this.workers.find((w) => !w.busy);
|
|
87
|
+
if (!idle) {
|
|
88
|
+
await Bun.sleep(this.options.pollInterval);
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
const job = await this.manager.pull(this.queueName, 1000);
|
|
92
|
+
if (job)
|
|
93
|
+
this.dispatch(idle, job);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
dispatch(wp, job) {
|
|
97
|
+
wp.busy = true;
|
|
98
|
+
wp.currentJob = job;
|
|
99
|
+
wp.timeoutId = setTimeout(() => {
|
|
100
|
+
this.handleTimeout(wp, job);
|
|
101
|
+
}, this.options.timeout);
|
|
102
|
+
const request = {
|
|
103
|
+
type: 'job',
|
|
104
|
+
job: { id: String(job.id), data: job.data, queue: job.queue, attempts: job.attempts },
|
|
105
|
+
};
|
|
106
|
+
wp.worker.postMessage(request);
|
|
107
|
+
}
|
|
108
|
+
handleMessage(wp, msg) {
|
|
109
|
+
if (!wp.currentJob || msg.jobId !== String(wp.currentJob.id))
|
|
110
|
+
return;
|
|
111
|
+
switch (msg.type) {
|
|
112
|
+
case 'result':
|
|
113
|
+
this.complete(wp, msg.result);
|
|
114
|
+
break;
|
|
115
|
+
case 'error':
|
|
116
|
+
this.fail(wp, msg.error ?? 'Unknown error');
|
|
117
|
+
break;
|
|
118
|
+
case 'progress':
|
|
119
|
+
if (msg.progress !== undefined) {
|
|
120
|
+
this.manager.updateProgress(wp.currentJob.id, msg.progress).catch(() => { });
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
complete(wp, result) {
|
|
126
|
+
if (wp.timeoutId) {
|
|
127
|
+
clearTimeout(wp.timeoutId);
|
|
128
|
+
wp.timeoutId = null;
|
|
129
|
+
}
|
|
130
|
+
if (wp.currentJob) {
|
|
131
|
+
const jobId = wp.currentJob.id;
|
|
132
|
+
this.manager.ack(jobId, result).catch((e) => {
|
|
133
|
+
console.error(`[SandboxedWorker] Failed to ack job ${jobId}:`, e);
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
wp.busy = false;
|
|
137
|
+
wp.currentJob = null;
|
|
138
|
+
}
|
|
139
|
+
fail(wp, error) {
|
|
140
|
+
if (wp.timeoutId) {
|
|
141
|
+
clearTimeout(wp.timeoutId);
|
|
142
|
+
wp.timeoutId = null;
|
|
143
|
+
}
|
|
144
|
+
if (wp.currentJob) {
|
|
145
|
+
const jobId = wp.currentJob.id;
|
|
146
|
+
this.manager.fail(jobId, error).catch((e) => {
|
|
147
|
+
console.error(`[SandboxedWorker] Failed to fail job ${jobId}:`, e);
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
wp.busy = false;
|
|
151
|
+
wp.currentJob = null;
|
|
152
|
+
}
|
|
153
|
+
handleTimeout(wp, job) {
|
|
154
|
+
console.error(`[SandboxedWorker] Job ${job.id} timed out after ${this.options.timeout}ms`);
|
|
155
|
+
wp.worker.terminate();
|
|
156
|
+
this.manager.fail(job.id, `Job timed out after ${this.options.timeout}ms`).catch(() => { });
|
|
157
|
+
const index = this.workers.indexOf(wp);
|
|
158
|
+
if (index !== -1)
|
|
159
|
+
this.handleCrash(wp, index);
|
|
160
|
+
}
|
|
161
|
+
handleCrash(wp, index) {
|
|
162
|
+
if (wp.currentJob)
|
|
163
|
+
this.manager.fail(wp.currentJob.id, 'Worker crashed').catch(() => { });
|
|
164
|
+
wp.busy = false;
|
|
165
|
+
wp.currentJob = null;
|
|
166
|
+
wp.restarts++;
|
|
167
|
+
if (this.options.autoRestart && wp.restarts < this.options.maxRestarts && this.running) {
|
|
168
|
+
console.log(`[SandboxedWorker] Restarting worker ${index} (attempt ${wp.restarts})`);
|
|
169
|
+
this.spawnWorker(index);
|
|
170
|
+
}
|
|
171
|
+
else if (wp.restarts >= this.options.maxRestarts) {
|
|
172
|
+
console.error(`[SandboxedWorker] Worker ${index} exceeded max restarts (${this.options.maxRestarts})`);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../../../src/client/sandboxed/worker.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,gBAAgB,EAAsB,MAAM,YAAY,CAAC;AASlE,OAAO,EAAE,mBAAmB,EAAE,oBAAoB,EAAE,MAAM,WAAW,CAAC;AAEtE;;GAEG;AACH,MAAM,OAAO,eAAe;IACT,SAAS,CAAS;IAClB,OAAO,CAAiC;IACxC,OAAO,GAAoB,EAAE,CAAC;IACvC,OAAO,GAAG,KAAK,CAAC;IAChB,WAAW,GAAyB,IAAI,CAAC;IACzC,WAAW,GAAkB,IAAI,CAAC;IAC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CAAC;CACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Wrapper Script Generator
|
|
3
|
+
* Creates the wrapper script that loads processor in worker process
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Create wrapper script file that loads the processor
|
|
7
|
+
*/
|
|
8
|
+
export declare function createWrapperScript(queueName: string, processorPath: string): string;
|
|
9
|
+
/**
|
|
10
|
+
* Cleanup wrapper script file
|
|
11
|
+
*/
|
|
12
|
+
export declare function cleanupWrapperScript(wrapperPath: string | null): void;
|
|
13
|
+
//# sourceMappingURL=wrapper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapper.d.ts","sourceRoot":"","sources":["../../../src/client/sandboxed/wrapper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAMH;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,MAAM,CA4CpF;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,WAAW,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAQrE"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Worker Wrapper Script Generator
|
|
3
|
+
* Creates the wrapper script that loads processor in worker process
|
|
4
|
+
*/
|
|
5
|
+
import { join } from 'path';
|
|
6
|
+
import { writeFileSync, existsSync, mkdirSync, unlinkSync } from 'fs';
|
|
7
|
+
import { tmpdir } from 'os';
|
|
8
|
+
/**
|
|
9
|
+
* Create wrapper script file that loads the processor
|
|
10
|
+
*/
|
|
11
|
+
export function createWrapperScript(queueName, processorPath) {
|
|
12
|
+
const fullPath = processorPath.startsWith('/')
|
|
13
|
+
? processorPath
|
|
14
|
+
: join(process.cwd(), processorPath);
|
|
15
|
+
const wrapperCode = `
|
|
16
|
+
// Sandboxed Worker Wrapper
|
|
17
|
+
const processor = (await import('${fullPath}')).default;
|
|
18
|
+
|
|
19
|
+
self.onmessage = async (event) => {
|
|
20
|
+
const { type, job } = event.data;
|
|
21
|
+
if (type !== 'job') return;
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
const result = await processor({
|
|
25
|
+
id: job.id,
|
|
26
|
+
data: job.data,
|
|
27
|
+
queue: job.queue,
|
|
28
|
+
attempts: job.attempts,
|
|
29
|
+
progress: (value) => {
|
|
30
|
+
self.postMessage({ type: 'progress', jobId: job.id, progress: value });
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
self.postMessage({ type: 'result', jobId: job.id, result });
|
|
35
|
+
} catch (err) {
|
|
36
|
+
self.postMessage({
|
|
37
|
+
type: 'error',
|
|
38
|
+
jobId: job.id,
|
|
39
|
+
error: err instanceof Error ? err.message : String(err),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
`;
|
|
44
|
+
const tempDir = join(tmpdir(), 'bunqueue-workers');
|
|
45
|
+
if (!existsSync(tempDir)) {
|
|
46
|
+
mkdirSync(tempDir, { recursive: true });
|
|
47
|
+
}
|
|
48
|
+
const wrapperPath = join(tempDir, `worker-${queueName}-${Date.now()}.ts`);
|
|
49
|
+
writeFileSync(wrapperPath, wrapperCode);
|
|
50
|
+
return wrapperPath;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Cleanup wrapper script file
|
|
54
|
+
*/
|
|
55
|
+
export function cleanupWrapperScript(wrapperPath) {
|
|
56
|
+
if (wrapperPath && existsSync(wrapperPath)) {
|
|
57
|
+
try {
|
|
58
|
+
unlinkSync(wrapperPath);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
// Ignore cleanup errors
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=wrapper.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wrapper.js","sourceRoot":"","sources":["../../../src/client/sandboxed/wrapper.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2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* SandboxedWorker - Re-exports from sandboxed module
|
|
3
|
+
* @deprecated Import from './sandboxed' instead
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export interface SandboxedWorkerOptions {
|
|
8
|
-
/** Path to processor file (must export default async function) */
|
|
9
|
-
processor: string;
|
|
10
|
-
/** Number of worker processes (default: 1) */
|
|
11
|
-
concurrency?: number;
|
|
12
|
-
/** Max memory per worker in MB - uses smol mode if <= 64 (default: 256) */
|
|
13
|
-
maxMemory?: number;
|
|
14
|
-
/** Job timeout in ms (default: 30000) */
|
|
15
|
-
timeout?: number;
|
|
16
|
-
/** Auto-restart crashed workers (default: true) */
|
|
17
|
-
autoRestart?: boolean;
|
|
18
|
-
/** Max restarts before giving up (default: 10) */
|
|
19
|
-
maxRestarts?: number;
|
|
20
|
-
/** Poll interval when no workers are idle (default: 10ms) */
|
|
21
|
-
pollInterval?: number;
|
|
22
|
-
/** Custom QueueManager (for testing, defaults to shared manager) */
|
|
23
|
-
manager?: SharedManager;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Sandboxed Worker - runs processors in isolated Bun Worker processes
|
|
27
|
-
*
|
|
28
|
-
* @example
|
|
29
|
-
* ```typescript
|
|
30
|
-
* import { Queue, SandboxedWorker } from 'bunqueue/client';
|
|
31
|
-
*
|
|
32
|
-
* const queue = new Queue('cpu-intensive');
|
|
33
|
-
*
|
|
34
|
-
* // Create processor file: processor.ts
|
|
35
|
-
* // export default async (job) => {
|
|
36
|
-
* // const result = heavyComputation(job.data);
|
|
37
|
-
* // return result;
|
|
38
|
-
* // };
|
|
39
|
-
*
|
|
40
|
-
* const worker = new SandboxedWorker('cpu-intensive', {
|
|
41
|
-
* processor: './processor.ts',
|
|
42
|
-
* concurrency: 4,
|
|
43
|
-
* timeout: 60000,
|
|
44
|
-
* });
|
|
45
|
-
*
|
|
46
|
-
* await worker.start();
|
|
47
|
-
* // Workers process jobs in isolated processes
|
|
48
|
-
* // If one crashes, others continue working
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
export declare class SandboxedWorker {
|
|
52
|
-
private readonly queueName;
|
|
53
|
-
private readonly options;
|
|
54
|
-
private readonly workers;
|
|
55
|
-
private running;
|
|
56
|
-
private pullPromise;
|
|
57
|
-
private wrapperPath;
|
|
58
|
-
private readonly manager;
|
|
59
|
-
constructor(queueName: string, options: SandboxedWorkerOptions);
|
|
60
|
-
/** Start the sandboxed worker pool */
|
|
61
|
-
start(): void;
|
|
62
|
-
/** Stop all workers gracefully */
|
|
63
|
-
stop(): Promise<void>;
|
|
64
|
-
/** Get worker pool stats */
|
|
65
|
-
getStats(): {
|
|
66
|
-
total: number;
|
|
67
|
-
busy: number;
|
|
68
|
-
idle: number;
|
|
69
|
-
restarts: number;
|
|
70
|
-
};
|
|
71
|
-
/** Create wrapper script file that loads the processor */
|
|
72
|
-
private createWrapperScript;
|
|
73
|
-
/** Spawn a single worker process */
|
|
74
|
-
private spawnWorker;
|
|
75
|
-
/** Main loop - pull jobs and dispatch to workers */
|
|
76
|
-
private pullLoop;
|
|
77
|
-
/** Dispatch job to a worker process */
|
|
78
|
-
private dispatchJob;
|
|
79
|
-
/** Handle message from worker */
|
|
80
|
-
private handleWorkerMessage;
|
|
81
|
-
/** Complete a job successfully */
|
|
82
|
-
private completeJob;
|
|
83
|
-
/** Fail a job */
|
|
84
|
-
private failJob;
|
|
85
|
-
/** Handle job timeout */
|
|
86
|
-
private handleJobTimeout;
|
|
87
|
-
/** Handle worker crash and potentially restart */
|
|
88
|
-
private handleWorkerCrash;
|
|
89
|
-
}
|
|
5
|
+
export type { SandboxedWorkerOptions } from './sandboxed';
|
|
6
|
+
export { SandboxedWorker } from './sandboxed';
|
|
90
7
|
//# sourceMappingURL=sandboxedWorker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sandboxedWorker.d.ts","sourceRoot":"","sources":["../../src/client/sandboxedWorker.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,
|
|
1
|
+
{"version":3,"file":"sandboxedWorker.d.ts","sourceRoot":"","sources":["../../src/client/sandboxedWorker.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,YAAY,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC"}
|
|
@@ -1,299 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* SandboxedWorker - Re-exports from sandboxed module
|
|
3
|
+
* @deprecated Import from './sandboxed' instead
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
|
-
import { join } from 'path';
|
|
7
|
-
import { writeFileSync, unlinkSync, existsSync, mkdirSync } from 'fs';
|
|
8
|
-
import { tmpdir } from 'os';
|
|
9
|
-
/**
|
|
10
|
-
* Sandboxed Worker - runs processors in isolated Bun Worker processes
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* import { Queue, SandboxedWorker } from 'bunqueue/client';
|
|
15
|
-
*
|
|
16
|
-
* const queue = new Queue('cpu-intensive');
|
|
17
|
-
*
|
|
18
|
-
* // Create processor file: processor.ts
|
|
19
|
-
* // export default async (job) => {
|
|
20
|
-
* // const result = heavyComputation(job.data);
|
|
21
|
-
* // return result;
|
|
22
|
-
* // };
|
|
23
|
-
*
|
|
24
|
-
* const worker = new SandboxedWorker('cpu-intensive', {
|
|
25
|
-
* processor: './processor.ts',
|
|
26
|
-
* concurrency: 4,
|
|
27
|
-
* timeout: 60000,
|
|
28
|
-
* });
|
|
29
|
-
*
|
|
30
|
-
* await worker.start();
|
|
31
|
-
* // Workers process jobs in isolated processes
|
|
32
|
-
* // If one crashes, others continue working
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export class SandboxedWorker {
|
|
36
|
-
queueName;
|
|
37
|
-
options;
|
|
38
|
-
workers = [];
|
|
39
|
-
running = false;
|
|
40
|
-
pullPromise = null;
|
|
41
|
-
wrapperPath = null;
|
|
42
|
-
manager;
|
|
43
|
-
constructor(queueName, options) {
|
|
44
|
-
this.queueName = queueName;
|
|
45
|
-
this.manager = options.manager ?? getSharedManager();
|
|
46
|
-
this.options = {
|
|
47
|
-
processor: options.processor,
|
|
48
|
-
concurrency: options.concurrency ?? 1,
|
|
49
|
-
maxMemory: options.maxMemory ?? 256,
|
|
50
|
-
timeout: options.timeout ?? 30000,
|
|
51
|
-
autoRestart: options.autoRestart ?? true,
|
|
52
|
-
maxRestarts: options.maxRestarts ?? 10,
|
|
53
|
-
pollInterval: options.pollInterval ?? 10,
|
|
54
|
-
};
|
|
55
|
-
}
|
|
56
|
-
/** Start the sandboxed worker pool */
|
|
57
|
-
start() {
|
|
58
|
-
if (this.running)
|
|
59
|
-
return;
|
|
60
|
-
this.running = true;
|
|
61
|
-
// Create wrapper script for workers
|
|
62
|
-
this.wrapperPath = this.createWrapperScript();
|
|
63
|
-
// Spawn worker processes
|
|
64
|
-
for (let i = 0; i < this.options.concurrency; i++) {
|
|
65
|
-
this.spawnWorker(i);
|
|
66
|
-
}
|
|
67
|
-
// Start pulling jobs
|
|
68
|
-
this.pullPromise = this.pullLoop();
|
|
69
|
-
}
|
|
70
|
-
/** Stop all workers gracefully */
|
|
71
|
-
async stop() {
|
|
72
|
-
this.running = false;
|
|
73
|
-
// Clear all timeouts
|
|
74
|
-
for (const wp of this.workers) {
|
|
75
|
-
if (wp.timeoutId) {
|
|
76
|
-
clearTimeout(wp.timeoutId);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
// Terminate all workers
|
|
80
|
-
for (const wp of this.workers) {
|
|
81
|
-
wp.worker.terminate();
|
|
82
|
-
}
|
|
83
|
-
this.workers.length = 0;
|
|
84
|
-
// Wait for pull loop to finish
|
|
85
|
-
if (this.pullPromise) {
|
|
86
|
-
await this.pullPromise;
|
|
87
|
-
}
|
|
88
|
-
// Cleanup wrapper script
|
|
89
|
-
if (this.wrapperPath && existsSync(this.wrapperPath)) {
|
|
90
|
-
try {
|
|
91
|
-
unlinkSync(this.wrapperPath);
|
|
92
|
-
}
|
|
93
|
-
catch {
|
|
94
|
-
// Ignore cleanup errors
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
/** Get worker pool stats */
|
|
99
|
-
getStats() {
|
|
100
|
-
const busy = this.workers.filter((w) => w.busy).length;
|
|
101
|
-
const restarts = this.workers.reduce((sum, w) => sum + w.restarts, 0);
|
|
102
|
-
return {
|
|
103
|
-
total: this.workers.length,
|
|
104
|
-
busy,
|
|
105
|
-
idle: this.workers.length - busy,
|
|
106
|
-
restarts,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
/** Create wrapper script file that loads the processor */
|
|
110
|
-
createWrapperScript() {
|
|
111
|
-
const processorPath = this.options.processor.startsWith('/')
|
|
112
|
-
? this.options.processor
|
|
113
|
-
: join(process.cwd(), this.options.processor);
|
|
114
|
-
const wrapperCode = `
|
|
115
|
-
// Sandboxed Worker Wrapper
|
|
116
|
-
const processor = (await import('${processorPath}')).default;
|
|
117
|
-
|
|
118
|
-
self.onmessage = async (event) => {
|
|
119
|
-
const { type, job } = event.data;
|
|
120
|
-
if (type !== 'job') return;
|
|
121
|
-
|
|
122
|
-
try {
|
|
123
|
-
const result = await processor({
|
|
124
|
-
id: job.id,
|
|
125
|
-
data: job.data,
|
|
126
|
-
queue: job.queue,
|
|
127
|
-
attempts: job.attempts,
|
|
128
|
-
progress: (value) => {
|
|
129
|
-
self.postMessage({ type: 'progress', jobId: job.id, progress: value });
|
|
130
|
-
},
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
self.postMessage({ type: 'result', jobId: job.id, result });
|
|
134
|
-
} catch (err) {
|
|
135
|
-
self.postMessage({
|
|
136
|
-
type: 'error',
|
|
137
|
-
jobId: job.id,
|
|
138
|
-
error: err instanceof Error ? err.message : String(err),
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
};
|
|
142
|
-
`;
|
|
143
|
-
// Write to temp file
|
|
144
|
-
const tempDir = join(tmpdir(), 'bunqueue-workers');
|
|
145
|
-
if (!existsSync(tempDir)) {
|
|
146
|
-
mkdirSync(tempDir, { recursive: true });
|
|
147
|
-
}
|
|
148
|
-
const wrapperPath = join(tempDir, `worker-${this.queueName}-${Date.now()}.ts`);
|
|
149
|
-
writeFileSync(wrapperPath, wrapperCode);
|
|
150
|
-
return wrapperPath;
|
|
151
|
-
}
|
|
152
|
-
/** Spawn a single worker process */
|
|
153
|
-
spawnWorker(index) {
|
|
154
|
-
if (!this.wrapperPath)
|
|
155
|
-
return;
|
|
156
|
-
const worker = new Worker(this.wrapperPath, {
|
|
157
|
-
smol: this.options.maxMemory <= 64,
|
|
158
|
-
});
|
|
159
|
-
const wp = {
|
|
160
|
-
worker,
|
|
161
|
-
busy: false,
|
|
162
|
-
currentJob: null,
|
|
163
|
-
restarts: this.workers[index]?.restarts ?? 0,
|
|
164
|
-
timeoutId: null,
|
|
165
|
-
};
|
|
166
|
-
// Handle messages from worker
|
|
167
|
-
worker.onmessage = (event) => {
|
|
168
|
-
this.handleWorkerMessage(wp, event.data);
|
|
169
|
-
};
|
|
170
|
-
// Handle worker errors/crashes
|
|
171
|
-
worker.onerror = (error) => {
|
|
172
|
-
console.error(`[SandboxedWorker] Worker ${index} error:`, error.message);
|
|
173
|
-
this.handleWorkerCrash(wp, index);
|
|
174
|
-
};
|
|
175
|
-
if (this.workers[index]) {
|
|
176
|
-
this.workers[index] = wp;
|
|
177
|
-
}
|
|
178
|
-
else {
|
|
179
|
-
this.workers.push(wp);
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
/** Main loop - pull jobs and dispatch to workers */
|
|
183
|
-
async pullLoop() {
|
|
184
|
-
while (this.running) {
|
|
185
|
-
// Find idle worker
|
|
186
|
-
const idleWorker = this.workers.find((w) => !w.busy);
|
|
187
|
-
if (!idleWorker) {
|
|
188
|
-
await Bun.sleep(this.options.pollInterval);
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
// Pull job from queue using manager
|
|
192
|
-
const job = await this.manager.pull(this.queueName, 1000);
|
|
193
|
-
if (!job)
|
|
194
|
-
continue;
|
|
195
|
-
// Dispatch to worker
|
|
196
|
-
this.dispatchJob(idleWorker, job);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
/** Dispatch job to a worker process */
|
|
200
|
-
dispatchJob(wp, job) {
|
|
201
|
-
wp.busy = true;
|
|
202
|
-
wp.currentJob = job;
|
|
203
|
-
// Set timeout
|
|
204
|
-
wp.timeoutId = setTimeout(() => {
|
|
205
|
-
this.handleJobTimeout(wp, job);
|
|
206
|
-
}, this.options.timeout);
|
|
207
|
-
// Send job to worker
|
|
208
|
-
const request = {
|
|
209
|
-
type: 'job',
|
|
210
|
-
job: {
|
|
211
|
-
id: String(job.id),
|
|
212
|
-
data: job.data,
|
|
213
|
-
queue: job.queue,
|
|
214
|
-
attempts: job.attempts,
|
|
215
|
-
},
|
|
216
|
-
};
|
|
217
|
-
wp.worker.postMessage(request);
|
|
218
|
-
}
|
|
219
|
-
/** Handle message from worker */
|
|
220
|
-
handleWorkerMessage(wp, msg) {
|
|
221
|
-
if (!wp.currentJob || msg.jobId !== String(wp.currentJob.id))
|
|
222
|
-
return;
|
|
223
|
-
switch (msg.type) {
|
|
224
|
-
case 'result':
|
|
225
|
-
this.completeJob(wp, msg.result);
|
|
226
|
-
break;
|
|
227
|
-
case 'error':
|
|
228
|
-
this.failJob(wp, msg.error ?? 'Unknown error');
|
|
229
|
-
break;
|
|
230
|
-
case 'progress':
|
|
231
|
-
if (msg.progress !== undefined) {
|
|
232
|
-
this.manager.updateProgress(wp.currentJob.id, msg.progress).catch(() => { });
|
|
233
|
-
}
|
|
234
|
-
break;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
/** Complete a job successfully */
|
|
238
|
-
completeJob(wp, result) {
|
|
239
|
-
if (wp.timeoutId) {
|
|
240
|
-
clearTimeout(wp.timeoutId);
|
|
241
|
-
wp.timeoutId = null;
|
|
242
|
-
}
|
|
243
|
-
if (wp.currentJob) {
|
|
244
|
-
const jobId = wp.currentJob.id;
|
|
245
|
-
this.manager.ack(jobId, result).catch((err) => {
|
|
246
|
-
console.error(`[SandboxedWorker] Failed to ack job ${jobId}:`, err);
|
|
247
|
-
});
|
|
248
|
-
}
|
|
249
|
-
wp.busy = false;
|
|
250
|
-
wp.currentJob = null;
|
|
251
|
-
}
|
|
252
|
-
/** Fail a job */
|
|
253
|
-
failJob(wp, error) {
|
|
254
|
-
if (wp.timeoutId) {
|
|
255
|
-
clearTimeout(wp.timeoutId);
|
|
256
|
-
wp.timeoutId = null;
|
|
257
|
-
}
|
|
258
|
-
if (wp.currentJob) {
|
|
259
|
-
const jobId = wp.currentJob.id;
|
|
260
|
-
this.manager.fail(jobId, error).catch((err) => {
|
|
261
|
-
console.error(`[SandboxedWorker] Failed to fail job ${jobId}:`, err);
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
wp.busy = false;
|
|
265
|
-
wp.currentJob = null;
|
|
266
|
-
}
|
|
267
|
-
/** Handle job timeout */
|
|
268
|
-
handleJobTimeout(wp, job) {
|
|
269
|
-
console.error(`[SandboxedWorker] Job ${job.id} timed out after ${this.options.timeout}ms`);
|
|
270
|
-
// Terminate the stuck worker
|
|
271
|
-
wp.worker.terminate();
|
|
272
|
-
// Fail the job
|
|
273
|
-
this.manager.fail(job.id, `Job timed out after ${this.options.timeout}ms`).catch(() => { });
|
|
274
|
-
// Restart worker if allowed
|
|
275
|
-
const index = this.workers.indexOf(wp);
|
|
276
|
-
if (index !== -1) {
|
|
277
|
-
this.handleWorkerCrash(wp, index);
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
/** Handle worker crash and potentially restart */
|
|
281
|
-
handleWorkerCrash(wp, index) {
|
|
282
|
-
// Fail current job if any
|
|
283
|
-
if (wp.currentJob) {
|
|
284
|
-
this.manager.fail(wp.currentJob.id, 'Worker crashed').catch(() => { });
|
|
285
|
-
}
|
|
286
|
-
wp.busy = false;
|
|
287
|
-
wp.currentJob = null;
|
|
288
|
-
wp.restarts++;
|
|
289
|
-
// Check if we should restart
|
|
290
|
-
if (this.options.autoRestart && wp.restarts < this.options.maxRestarts && this.running) {
|
|
291
|
-
console.log(`[SandboxedWorker] Restarting worker ${index} (attempt ${wp.restarts})`);
|
|
292
|
-
this.spawnWorker(index);
|
|
293
|
-
}
|
|
294
|
-
else if (wp.restarts >= this.options.maxRestarts) {
|
|
295
|
-
console.error(`[SandboxedWorker] Worker ${index} exceeded max restarts (${this.options.maxRestarts})`);
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
}
|
|
5
|
+
export { SandboxedWorker } from './sandboxed';
|
|
299
6
|
//# sourceMappingURL=sandboxedWorker.js.map
|