Skip to content

Commit 3d5b476

Browse files
committed
cleanup
1 parent 324c036 commit 3d5b476

14 files changed

Lines changed: 222 additions & 262 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ node_modules/
33
*.sqlite
44
*.sqlite3
55
dist/
6-
*.data
6+
*.data
7+
queue.index.json
8+
queue.index.json*

example-queue-project/.email-queue/index.json

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

example-queue-project/README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Queue Example Project
2+
3+
This example demonstrates how to use `@muniter/queue` in a realistic production setup with separate processes for job producers and consumers.
4+
5+
## Architecture
6+
7+
This example shows the **separation of concerns** pattern recommended for production applications:
8+
9+
- **Queue Configuration** (`queues.ts`) - Centralized queue initialization, configuration, and event listeners
10+
- **Job Producers** (`add-job.ts`) - Add jobs to queues (e.g., from web servers, API endpoints)
11+
- **Job Consumers** (`process-jobs.ts`) - Process jobs continuously (e.g., background worker processes)
12+
- **Shared Types** (`types.ts`) - Job type definitions shared between producers and consumers
13+
14+
## Features Demonstrated
15+
16+
- **Multiple Queue Types**: Database queue (SQLite) and File queue
17+
- **Type Safety**: Full TypeScript support with job payload validation
18+
- **Event System**: Comprehensive logging via queue lifecycle events
19+
- **Worker Management**: Using Worker class for clean process organization
20+
- **Error Handling**: Simulated failures and proper error events
21+
- **Delayed Jobs**: Jobs that run after a specified delay
22+
- **Production Pattern**: Separate entry points for different concerns
23+
24+
## Setup
25+
26+
1. Install dependencies:
27+
```bash
28+
npm install
29+
```
30+
31+
2. Build the project:
32+
```bash
33+
npm run build
34+
```
35+
36+
## Usage
37+
38+
### 1. Start the Job Processors (Workers)
39+
40+
In one terminal, start the background workers that will process jobs:
41+
42+
```bash
43+
npm run process-jobs
44+
```
45+
46+
This will:
47+
- Initialize the database and file queue
48+
- Register handlers for all job types
49+
- Start workers that continuously poll for new jobs
50+
- Display real-time processing logs
51+
52+
### 2. Add Jobs to the Queues
53+
54+
In another terminal, add jobs to be processed:
55+
56+
```bash
57+
npm run add-job
58+
```
59+
60+
This will add various types of jobs:
61+
- Welcome emails
62+
- Notification emails
63+
- Image processing tasks
64+
- Report generation (with delay)
65+
- Batch processing jobs
66+
67+
You'll see the jobs being processed in real-time in the worker terminal.
68+
69+
## Job Types
70+
71+
### Email Jobs (File Queue)
72+
- `welcome-email`: Send welcome emails to new users
73+
- `notification`: Send system notifications
74+
75+
### General Jobs (Database Queue)
76+
- `process-image`: Resize and optimize images
77+
- `generate-report`: Generate business reports
78+
79+
## Production Deployment
80+
81+
In a real production environment, you would:
82+
83+
1. **Web Server** - Run `add-job.ts` logic in your API endpoints to add jobs
84+
2. **Worker Processes** - Deploy `process-jobs.ts` as separate background services
85+
3. **Scaling** - Run multiple worker processes for high throughput
86+
4. **Monitoring** - Use the event system to send metrics to monitoring services
87+
5. **Error Handling** - Implement retry logic and dead letter queues
88+
89+
## Files
90+
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)
98+
99+
## Try It
100+
101+
1. Start workers: `npm run process-jobs`
102+
2. In another terminal: `npm run add-job`
103+
3. Watch the jobs get processed in real-time!
104+
4. Try adding jobs multiple times to see concurrent processing
105+
5. Stop workers with Ctrl+C

example-queue-project/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"dev": "tsx src/index.ts",
1111
"start": "node dist/index.js",
1212
"add-job": "tsx src/add-job.ts",
13-
"process-jobs": "tsx src/process-jobs.ts"
13+
"process-jobs": "tsx src/process-jobs.ts",
14+
"demo": "echo 'Run \"npm run process-jobs\" in one terminal, then \"npm run add-job\" in another!'"
1415
},
1516
"dependencies": {
1617
"@muniter/queue": "file:..",

example-queue-project/queue-data-events/index.json

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

example-queue-project/queue-data/index.json

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

example-queue-project/src/database.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sqlite3 from 'sqlite3';
2-
import { promisify } from 'util';
32
import path from 'path';
43
import { fileURLToPath } from 'url';
54

example-queue-project/src/debug.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { FileQueue } from "@muniter/queue";
2+
3+
interface EmailJobs {
4+
'welcome-email': { to: string; name: string };
5+
'notification': { to: string; subject: string; body: string };
6+
}
7+
8+
9+
export const emailQueue = new FileQueue<EmailJobs>({
10+
path: "./email-queue",
11+
})
12+
13+
// Register job handlers for email queue
14+
emailQueue.onJob('welcome-email', async (payload) => {
15+
const { to, name } = payload;
16+
console.log(`Sending welcome email to ${to} (${name})`);
17+
await new Promise(resolve => setTimeout(resolve, 1000));
18+
19+
if (Math.random() > 0.8) {
20+
throw new Error('Email service temporarily unavailable');
21+
}
22+
23+
console.log(`Welcome email sent successfully to ${to}`);
24+
});
25+
26+
emailQueue.onJob('notification', async (payload) => {
27+
const { to, subject, body } = payload;
28+
console.log(`Sending notification email to ${to}: ${subject}`);
29+
await new Promise(resolve => setTimeout(resolve, 500));
30+
console.log(`Notification sent successfully`);
31+
});

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

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

0 commit comments

Comments
 (0)