|
| 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 |
0 commit comments