forked from Vong3432/Weekly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskQueue.js
38 lines (30 loc) · 862 Bytes
/
TaskQueue.js
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
31
32
33
34
35
36
37
38
const redis = require("redis")
const TaskManager = async(redisClient) => {
while(true) {
let task;
try {
task = await redisClient.blpopAsync("task_queue", 0);
} catch(error) {
console.log('crashed')
// Redis connect could have closed. Handle those cases here
process.exit(1);
}
try {
await HandleTask(task);
} catch(error) {
// Handling the task failed
console.log("Handle task failed.")
}
}
}
const HandleTask = async(task) => {
// Do the work
console.log(task)
}
// Run the TaskManager function
const _RunTaskManager = async () => {
// Initialize redis
let redisClient = redis.createClient();
await TaskManager(redisClient);
}
module.exports = _RunTaskManager;