Skip to content

Commit fb07331

Browse files
committed
feat: add Redis support for email queue processing with Docker configuration
1 parent c90f59d commit fb07331

7 files changed

Lines changed: 493 additions & 2 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: '3.8'
2+
3+
services:
4+
redis:
5+
image: redis:7-alpine
6+
container_name: queue-redis
7+
ports:
8+
- "6379:6379"
9+
healthcheck:
10+
test: ["CMD", "redis-cli", "ping"]
11+
interval: 10s
12+
timeout: 3s
13+
retries: 5
14+
15+
redis-commander:
16+
image: rediscommander/redis-commander:latest
17+
container_name: queue-redis-ui
18+
environment:
19+
- REDIS_HOSTS=local:redis:6379
20+
ports:
21+
- "8081:8081"
22+
depends_on:
23+
- redis

example-queue-project/package-lock.json

Lines changed: 95 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example-queue-project/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,17 @@
1111
"start": "node dist/index.js",
1212
"push": "tsx src/index.ts --push",
1313
"run": "tsx src/index.ts --run",
14-
"demo": "echo 'Run \"npm run run\" in one terminal, then \"npm run push\" in another!'"
14+
"demo": "echo 'Run \"npm run run\" in one terminal, then \"npm run push\" in another!'",
15+
"redis:start": "docker-compose up -d redis",
16+
"redis:stop": "docker-compose down",
17+
"redis:ui": "docker-compose up -d redis-commander",
18+
"redis:logs": "docker-compose logs -f redis"
1519
},
1620
"dependencies": {
1721
"@aws-sdk/client-sqs": "^3.831.0",
1822
"@muniter/queue": "file:..",
1923
"dotenv": "^16.3.1",
24+
"redis": "^4.6.0",
2025
"sqlite3": "^5.1.6"
2126
},
2227
"devDependencies": {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { RedisQueue } from "@muniter/queue";
2+
import { createClient } from "redis";
3+
4+
interface EmailJobs {
5+
"welcome-email": { to: string; name: string };
6+
"notification": { to: string; subject: string; body: string };
7+
}
8+
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');
20+
21+
// Register job handlers
22+
emailQueue.onJob("welcome-email", async (payload) => {
23+
const { to, name } = payload;
24+
console.log(`[Redis] Sending welcome email to ${to} (${name})`);
25+
await new Promise((resolve) => setTimeout(resolve, 1000));
26+
console.log(`[Redis] Welcome email sent successfully to ${to}`);
27+
});
28+
29+
emailQueue.onJob("notification", async (payload) => {
30+
const { to, subject, body } = payload;
31+
console.log(`[Redis] Sending notification email to ${to}: ${subject}`);
32+
await new Promise((resolve) => setTimeout(resolve, 500));
33+
console.log(`[Redis] Notification sent successfully`);
34+
});
35+
36+
// Event listeners
37+
emailQueue.on("beforeExec", (event) => {
38+
console.log(
39+
`\n[Redis Queue][${new Date().toISOString()}] Starting ${event.name} job ${event.id}...`
40+
);
41+
});
42+
43+
emailQueue.on("afterExec", (event) => {
44+
console.log(
45+
`[Redis Queue][${new Date().toISOString()}] Job ${event.id} (${event.name}) completed successfully`
46+
);
47+
});
48+
49+
emailQueue.on("afterError", (event) => {
50+
console.error(
51+
`[Redis Queue][${new Date().toISOString()}] Job ${event.id} (${event.name}) failed:`,
52+
event.error
53+
);
54+
});

0 commit comments

Comments
 (0)