Skip to content

Commit 42de53e

Browse files
committed
improvements
1 parent fa42a9f commit 42de53e

8 files changed

Lines changed: 502 additions & 289 deletions

File tree

DESIGN.md

Lines changed: 448 additions & 0 deletions
Large diffs are not rendered by default.

docs/mongoose-adapter.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
The Mongoose adapter allows you to use Mongoose models with the adapter-queue system, providing seamless integration with your existing Mongoose-based application. It implements the `DatabaseAdapter` interface directly, offering a native Mongoose experience.
44

5+
**Key Feature**: No separate worker processes needed - each queue processes its own jobs through the `run()` method.
6+
57
## Installation
68

79
First, ensure you have both `adapter-queue` and `mongoose` installed:
@@ -32,8 +34,11 @@ queue.setHandlers({
3234
// Push a job
3335
await queue.push('send-email', { to: 'user@example.com' });
3436

35-
// Start the worker
37+
// Process all jobs once
3638
await queue.run();
39+
40+
// Or run continuously (polling every 5 seconds)
41+
await queue.run(true, 5);
3742
```
3843

3944
## Using Custom Models

example-queue-project/mongoose-example.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ async function main() {
6464
});
6565
console.log(`Image job created with ID: ${imageJobId}`);
6666

67-
// Run the queue worker
68-
console.log('\nStarting queue worker...');
67+
// Process all jobs once
68+
console.log('\nProcessing jobs...');
6969
await queue.run();
70+
console.log('All jobs processed!');
7071
}
7172

7273
// Alternative: Using a custom model
@@ -95,8 +96,37 @@ async function customModelExample(): Promise<void> {
9596
});
9697
}
9798

98-
// Run the example
99+
// Continuous processing example
100+
async function continuousProcessingExample(): Promise<void> {
101+
await mongoose.connect('mongodb://localhost:27017/queue-example');
102+
103+
const queue = createMongooseQueue<MyJobs>('continuous-queue');
104+
105+
queue.setHandlers({
106+
'send-email': async (job) => {
107+
console.log(`[${new Date().toISOString()}] Processing email job: ${job.id}`);
108+
await new Promise(resolve => setTimeout(resolve, 1000));
109+
console.log(`Email sent to ${job.payload.to}`);
110+
},
111+
'process-image': async (job) => {
112+
console.log(`[${new Date().toISOString()}] Processing image job: ${job.id}`);
113+
await new Promise(resolve => setTimeout(resolve, 2000));
114+
console.log(`Image processed: ${job.payload.url}`);
115+
}
116+
});
117+
118+
console.log('Starting continuous job processing (polling every 3 seconds)...');
119+
console.log('Add jobs from another process and watch them get processed!');
120+
121+
// This will run forever, polling every 3 seconds
122+
await queue.run(true, 3);
123+
}
124+
125+
// Run the example (uncomment the one you want to test)
99126
main().catch(console.error);
100127

128+
// For continuous processing, run this instead:
129+
// continuousProcessingExample().catch(console.error);
130+
101131
// Export for testing
102-
export { main, customModelExample };
132+
export { main, customModelExample, continuousProcessingExample };

package.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,6 @@
8181
"./mongoose": {
8282
"import": "./dist/src/adapters/mongoose.js",
8383
"types": "./dist/src/adapters/mongoose.d.ts"
84-
},
85-
"./worker": {
86-
"import": "./dist/src/worker/worker.js",
87-
"types": "./dist/src/worker/worker.d.ts"
8884
}
8985
},
9086
"peerDependencies": {

src/cli/worker.ts

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

src/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,17 @@ export type {
2525
} from './interfaces/database.ts';
2626

2727

28-
export type { WorkerOptions } from './worker/worker.ts';
28+
export { DbQueue } from './drivers/db.ts';
29+
export { FileQueue } from './drivers/file.ts';
30+
export { InMemoryQueue } from './drivers/memory.ts';
31+
32+
// Mongoose adapter exports
33+
export {
34+
MongooseQueue,
35+
createMongooseQueue,
36+
createQueueModel,
37+
QueueJob,
38+
MongooseDatabaseAdapter,
39+
QueueJobSchema
40+
} from './adapters/mongoose.ts';
41+
export type { IQueueJob } from './adapters/mongoose.ts';

src/worker/worker.ts

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

tests/worker/worker.test.ts

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

0 commit comments

Comments
 (0)