-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.js
More file actions
30 lines (25 loc) · 862 Bytes
/
Copy pathqueue.js
File metadata and controls
30 lines (25 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { Router } from 'express';
const QUEUE_KEY = 'queue:emails';
export function queueRouter(redis){
const router = Router();
router.post('/', async (req, res) => {
const job = {
to: req.body.to,
subject: req.body.subject || 'No Subject',
body: req.body.body || 'No content',
createdAt: new Date().toISOString()
}
await redis.lpush(QUEUE_KEY, JSON.stringify(job));
res.json({ status: 'queued', job });
});
router.get('/process-one', async (req, res) => {
const job = await redis.rpop(QUEUE_KEY);
if (job) {
const parsedJob = JSON.parse(job);
res.json({ status: 'Email sent Successfully', job: parsedJob });
} else {
res.json({ message: 'No jobs in the queue' });
}
});
return router;
}