Skip to content

Commit 83bb85e

Browse files
committed
feat: enhance queue system with new SQLite and Redis adapters, update dependencies, and improve project structure
1 parent 00cc25c commit 83bb85e

19 files changed

Lines changed: 1418 additions & 354 deletions

example-queue-project/README.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ This example shows the **separation of concerns** pattern recommended for produc
1313

1414
## Features Demonstrated
1515

16-
- **Multiple Queue Types**: Database queue (SQLite) and File queue
16+
- **Multiple Queue Types**: SQLite, Redis, SQS, and File queues with clean adapter APIs
17+
- **New Adapter System**: Clean drizzle-style APIs for queue creation
1718
- **Type Safety**: Full TypeScript support with job payload validation
1819
- **Event System**: Comprehensive logging via queue lifecycle events
1920
- **Worker Management**: Using Worker class for clean process organization
@@ -28,7 +29,24 @@ This example shows the **separation of concerns** pattern recommended for produc
2829
pnpm install
2930
```
3031

31-
2. Build the project:
32+
2. Install the queue drivers you want to use (peer dependencies):
33+
34+
For SQLite queue:
35+
```bash
36+
pnpm add better-sqlite3
37+
```
38+
39+
For Redis queue:
40+
```bash
41+
pnpm add redis
42+
```
43+
44+
For SQS queue:
45+
```bash
46+
pnpm add @aws-sdk/client-sqs
47+
```
48+
49+
3. Build the project:
3250
```bash
3351
pnpm run build
3452
```
@@ -88,13 +106,10 @@ In a real production environment, you would:
88106

89107
## Files
90108

91-
- `queues.ts` - Centralized queue configuration, initialization, and event listeners
92-
- `add-job.ts` - Job producer (adds jobs to queues)
93-
- `process-jobs.ts` - Job consumer (processes jobs continuously)
94-
- `types.ts` - Shared job type definitions
95-
- `sqlite-adapter.ts` - Database adapter implementation
96-
- `database.ts` - Database initialization
97-
- `index.ts` - Original combined example (legacy)
109+
- `general-queue.ts` - SQLite queue using new adapter API
110+
- `redis-queue.ts` - Redis queue using new adapter API
111+
- `email-queue.ts` - File and SQS queues using new adapter APIs
112+
- `index.ts` - Main demo entry point
98113

99114
## Try It
100115

example-queue-project/package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
"redis:logs": "docker-compose logs -f redis"
1919
},
2020
"dependencies": {
21+
"@muniter/queue": "link:..",
22+
"dotenv": "^16.3.1"
23+
},
24+
"peerDependencies": {
2125
"@aws-sdk/client-sqs": "^3.831.0",
22-
"@muniter/queue": "file:..",
23-
"dotenv": "^16.3.1",
24-
"redis": "^4.6.0",
25-
"sqlite3": "^5.1.6"
26+
"better-sqlite3": "^12.0.0",
27+
"redis": "^5.5.6"
2628
},
2729
"devDependencies": {
2830
"@types/node": "^20.10.0",
29-
"@types/sqlite3": "^3.1.11",
3031
"tsx": "^4.6.2",
3132
"typescript": "^5.3.3"
3233
}

example-queue-project/src/database.ts

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

example-queue-project/src/email-queue.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { SQS } from "@aws-sdk/client-sqs";
2-
import { FileQueue, SqsQueue } from "@muniter/queue";
1+
import { FileQueue } from "@muniter/queue";
2+
import { createSQSQueue } from "@muniter/queue/sqs";
33

44
interface EmailJobs {
55
"welcome-email": { to: string; name: string };
@@ -11,15 +11,12 @@ export const emailQueueFile = new FileQueue<EmailJobs>({
1111
path: "./email-queue",
1212
});
1313

14-
// SQS-based queue (for production)
15-
const sqsClient = new SQS({
16-
region: "us-east-1",
17-
profile: "javier",
18-
});
19-
20-
export const emailQueueSqs = new SqsQueue<EmailJobs>(
21-
sqsClient,
22-
"https://sqs.us-east-1.amazonaws.com/428011609647/test-queue"
14+
// SQS-based queue (for production) - simple API
15+
export const emailQueueSqs = createSQSQueue<EmailJobs>(
16+
"https://sqs.us-east-1.amazonaws.com/428011609647/test-queue",
17+
{
18+
profile: "javier",
19+
}
2320
);
2421

2522
export const emailQueue = emailQueueSqs;
@@ -52,25 +49,29 @@ emailQueue.onJob("notification", async (payload) => {
5249

5350
emailQueue.on("beforeExec", (event) => {
5451
console.log(
55-
`\n[emailQueue][${emailQueue.constructor.name}][${new Date().toISOString()}] Starting ${event.name} job ${
56-
event.id
57-
}...`
52+
`\n[emailQueue][${
53+
emailQueue.constructor.name
54+
}][${new Date().toISOString()}] Starting ${event.name} job ${event.id}...`
5855
);
5956
});
6057

6158
emailQueue.on("afterExec", (event) => {
6259
console.log(
63-
`[emailQueue][${emailQueue.constructor.name}][${new Date().toISOString()}] Email job ${event.id} (${
60+
`[emailQueue][${
61+
emailQueue.constructor.name
62+
}][${new Date().toISOString()}] Email job ${event.id} (${
6463
event.name
6564
}) completed successfully`
6665
);
6766
});
6867

6968
emailQueue.on("afterError", (event) => {
7069
console.error(
71-
`[emailQueue][${emailQueue.constructor.name}][${new Date().toISOString()}] Email job ${event.id} (${
70+
`[emailQueue][${
71+
emailQueue.constructor.name
72+
}][${new Date().toISOString()}] Email job ${event.id} (${
7273
event.name
7374
}) failed:`,
7475
event.error
7576
);
76-
});
77+
});

example-queue-project/src/general-queue.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { DbQueue } from "@muniter/queue";
2-
import { SQLiteDatabaseAdapter } from "./sqlite-adapter.js";
1+
import { createSQLiteQueue } from "@muniter/queue/sqlite";
32

43
export interface GeneralJobs {
54
'process-image': { url: string; width: number; height: number };
65
'generate-report': { type: string; period: string };
76
}
87

9-
10-
export const generalQueue = new DbQueue<GeneralJobs>(new SQLiteDatabaseAdapter());
8+
export const generalQueue = createSQLiteQueue<GeneralJobs>('queue.db');
119

1210
// Register job handlers for general queue
1311
generalQueue.onJob('process-image', async (payload) => {

example-queue-project/src/index.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1-
import { initializeDatabase } from './database.js';
21
import { emailQueue } from './email-queue.js';
32
import { generalQueue } from './general-queue.js';
3+
import { emailQueue as redisEmailQueue } from './redis-queue.js';
44
import { parseArgs } from 'util';
55

66
async function push() {
7-
console.log('Initializing database...');
8-
await initializeDatabase();
7+
console.log('Adding jobs to demonstrate different queue types...');
8+
console.log('- SQLite queue (general tasks)');
9+
console.log('- SQS queue (production email)');
10+
console.log('- Redis queue (high-performance email)');
11+
console.log('');
912

10-
console.log('Adding some test jobs...');
11-
// Add jobs using the new type-safe API
13+
// SQS queue - using new createQueue(queueUrl) API
1214
const emailJobId1 = await emailQueue.addJob('welcome-email', {
1315
payload: {
1416
to: 'user@example.com',
1517
name: 'John Doe'
1618
}
1719
});
18-
console.log(`Welcome email job added with ID: ${emailJobId1}`);
20+
console.log(`[SQS] Welcome email job added with ID: ${emailJobId1}`);
1921

20-
const emailJobId2 = await emailQueue.addJob('notification', {
22+
// Redis queue - using new createRedisQueue(url) API
23+
const emailJobId2 = await redisEmailQueue.addJob('notification', {
2124
payload: {
2225
to: 'admin@example.com',
2326
subject: 'Daily Report',
2427
body: 'Here is your daily report...'
2528
}
26-
// Note: FileQueue doesn't support priority - would cause TypeScript error
27-
// For demo purposes, we removed the priority option
2829
});
29-
console.log(`Priority notification job added with ID: ${emailJobId2}`);
30+
console.log(`[Redis] Notification job added with ID: ${emailJobId2}`);
3031

32+
// SQLite queue - using new createQueue('db.sqlite') API
3133
const imageJobId = await generalQueue.addJob('process-image', {
3234
payload: {
3335
url: 'https://example.com/image.jpg',
3436
width: 800,
3537
height: 600
3638
}
3739
});
38-
console.log(`Image job added with ID: ${imageJobId}`);
40+
console.log(`[SQLite] Image job added with ID: ${imageJobId}`);
3941

4042
await generalQueue.addJob('generate-report', {
4143
payload: {
@@ -44,6 +46,7 @@ async function push() {
4446
},
4547
delay: 2
4648
});
49+
console.log(`[SQLite] Report job added with 2s delay`);
4750
}
4851

4952
async function run() {

example-queue-project/src/redis-queue.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1-
import { RedisQueue } from "@muniter/queue";
2-
import { createClient } from "redis";
1+
import { createRedisQueue } from "@muniter/queue/redis";
32

43
interface EmailJobs {
54
"welcome-email": { to: string; name: string };
65
"notification": { to: string; subject: string; body: string };
76
}
87

9-
// Create Redis client directly
10-
const redisClient = createClient({ url: 'redis://localhost:6379' });
11-
12-
redisClient.on('error', (err) => console.error('Redis Client Error:', err));
13-
redisClient.on('connect', () => console.log('Redis Client Connected'));
14-
redisClient.on('ready', () => console.log('Redis Client Ready'));
15-
16-
await redisClient.connect();
17-
18-
// Create Redis queue - no adapter needed!
19-
export const emailQueue = new RedisQueue<EmailJobs>(redisClient, 'email');
8+
// Create Redis queue with simple API
9+
export const emailQueue = createRedisQueue<EmailJobs>('redis://localhost:6379');
2010

2111
// Register job handlers
2212
emailQueue.onJob("welcome-email", async (payload) => {

0 commit comments

Comments
 (0)