Skip to content

Commit 324c036

Browse files
committed
simplify workers
1 parent 85b6ea3 commit 324c036

5 files changed

Lines changed: 9 additions & 98 deletions

File tree

README.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ A TypeScript queue system inspired by Yii2-Queue architecture, providing a clean
77
- **Driver-based architecture**: Swap between DB, SQS, and File drivers seamlessly
88
- **Event-based jobs**: Register handlers for job types without complex classes
99
- **Type-safe API**: Full TypeScript support with driver-specific option validation
10-
- **Worker isolation**: Run jobs in separate processes for better stability
1110
- **Multiple backends**: Database, Amazon SQS, and File storage drivers
1211
- **Event system**: Hook into queue lifecycle events
1312

@@ -219,9 +218,9 @@ await worker.start(true, 3); // repeat=true, timeout=3 seconds
219218
// Process once then exit
220219
await worker.start(false);
221220

222-
// With isolation (jobs run in child processes)
223-
const isolatedWorker = new Worker(queue, { isolate: true });
224-
await isolatedWorker.start();
221+
// With custom timeout
222+
const worker = new Worker(queue, { timeout: 5 });
223+
await worker.start();
225224
```
226225

227226
## Event Handling
@@ -298,9 +297,6 @@ npm run queue:worker -- --driver sqs --queue-url https://sqs.us-east-1.amazonaws
298297
# File driver
299298
npm run queue:worker -- --driver file --path ./queue-data
300299

301-
# Isolated mode
302-
npm run queue:worker -- --isolate
303-
304300
# Run once and exit
305301
npm run queue:worker -- --no-repeat
306302

src/cli/worker.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { SqsQueue } from '../drivers/sqs.ts';
66

77
interface WorkerConfig {
88
driver: 'db' | 'sqs';
9-
isolate?: boolean;
109
repeat?: boolean;
1110
timeout?: number;
1211
dbAdapter?: any;
@@ -18,7 +17,6 @@ function parseArgs(): WorkerConfig {
1817
const args = process.argv.slice(2);
1918
const config: WorkerConfig = {
2019
driver: 'db',
21-
isolate: false,
2220
repeat: true,
2321
timeout: 3
2422
};
@@ -30,9 +28,6 @@ function parseArgs(): WorkerConfig {
3028
case '--driver':
3129
config.driver = args[++i] as 'db' | 'sqs';
3230
break;
33-
case '--isolate':
34-
config.isolate = true;
35-
break;
3631
case '--no-repeat':
3732
config.repeat = false;
3833
break;
@@ -60,14 +55,13 @@ Usage: node worker.js [options]
6055
6156
Options:
6257
--driver <type> Queue driver: 'db' or 'sqs' (default: db)
63-
--isolate Run jobs in isolated child processes
6458
--no-repeat Run once and exit (default: run continuously)
6559
--timeout <sec> Polling timeout in seconds (default: 3)
6660
--queue-url <url> SQS queue URL (required for SQS driver)
6761
--help Show this help message
6862
6963
Examples:
70-
node worker.js --driver db --isolate
64+
node worker.js --driver db
7165
node worker.js --driver sqs --queue-url https://sqs.us-east-1.amazonaws.com/123/test
7266
node worker.js --no-repeat --timeout 10
7367
`);
@@ -102,12 +96,10 @@ async function main(): Promise<void> {
10296
}
10397

10498
const worker = new Worker(queue, {
105-
isolate: config.isolate,
10699
timeout: config.timeout
107100
});
108101

109102
console.log(`Starting worker with ${config.driver} driver...`);
110-
console.log(`Isolate: ${config.isolate}`);
111103
console.log(`Repeat: ${config.repeat}`);
112104
console.log(`Timeout: ${config.timeout}s`);
113105

src/worker/worker-child.ts

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/worker/worker.ts

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,18 @@
1-
import { spawn, ChildProcess } from 'child_process';
2-
import path from 'path';
31
import { Queue } from '../core/queue.ts';
4-
import type { QueueMessage } from '../interfaces/job.ts';
52

63
export interface WorkerOptions {
7-
isolate?: boolean;
84
timeout?: number;
9-
childScriptPath?: string;
105
}
116

127
export class Worker {
138
constructor(
149
private queue: Queue,
1510
private options: WorkerOptions = {}
16-
) {
17-
if (this.options.isolate) {
18-
this.setupIsolatedHandler();
19-
}
20-
}
11+
) {}
2112

2213
async start(repeat: boolean = true, timeout: number = 3): Promise<void> {
23-
await this.queue.run(repeat, timeout);
24-
}
25-
26-
private setupIsolatedHandler(): void {
27-
const originalHandleMessage = this.queue['handleMessage'].bind(this.queue);
28-
29-
this.queue['handleMessage'] = async (message: QueueMessage): Promise<boolean> => {
30-
return new Promise<boolean>((resolve) => {
31-
const childScriptPath = this.options.childScriptPath ||
32-
path.resolve(__dirname, 'worker-child.js');
33-
34-
const child: ChildProcess = spawn(process.execPath, [childScriptPath], {
35-
stdio: ['pipe', 'inherit', 'inherit'],
36-
timeout: (message.meta.ttr || 300) * 1000
37-
});
38-
39-
child.stdin!.end(message.payload);
40-
41-
child.on('close', (code) => {
42-
resolve(code === 0);
43-
});
44-
45-
child.on('error', (error) => {
46-
console.error('Child process error:', error);
47-
resolve(false);
48-
});
49-
});
50-
};
14+
const actualTimeout = this.options.timeout ?? timeout;
15+
await this.queue.run(repeat, actualTimeout);
5116
}
5217
}
5318

tests/worker/worker.test.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,6 @@ describe('Worker', () => {
6363
expect(processedJobs.sort()).toEqual(['job0', 'job1', 'job2', 'job3', 'job4']);
6464
});
6565

66-
it('should handle worker with isolation', async () => {
67-
const processedJobs: string[] = [];
68-
69-
queue.onJob('simple-job', async (payload) => {
70-
processedJobs.push(payload.data);
71-
});
72-
73-
await queue.addJob('simple-job', { payload: { data: 'isolated test' } });
74-
75-
const worker = new Worker(queue, { isolate: true });
76-
await worker.start(false, 1);
77-
78-
// Note: In isolation mode, the job may not execute the registered handler
79-
// This test mainly verifies that the worker doesn't crash
80-
expect(true).toBe(true); // Worker didn't crash
81-
});
8266

8367
it('should handle worker start and stop', async () => {
8468
const worker = new Worker(queue);
@@ -88,8 +72,8 @@ describe('Worker', () => {
8872
});
8973

9074
it('should support different worker options', async () => {
91-
const worker1 = new Worker(queue, { isolate: false });
92-
const worker2 = new Worker(queue, { isolate: true, timeout: 5 });
75+
const worker1 = new Worker(queue);
76+
const worker2 = new Worker(queue, { timeout: 5 });
9377

9478
expect(worker1).toBeInstanceOf(Worker);
9579
expect(worker2).toBeInstanceOf(Worker);

0 commit comments

Comments
 (0)