Repository navigation
Expand file tree
/
Copy pathworker.js
More file actions
84 lines (71 loc) · 2.24 KB
/
Copy pathworker.js
File metadata and controls
84 lines (71 loc) · 2.24 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const redisClient = require("./redis/client.js");
const {
checkoutJob,
updateJobStatus,
getJob,
acquireLock,
acknowledgeJob,
incrementAttempts,
moveToDelayed,
moveToDeadLetter,
} = require("./redis/schema/jobQueue.js");
const POLL_TIMEOUT = 5;
const simulateWork = (jobId, payload) => {
return new Promise((resolve, reject) => {
const delayMs = 2000 + Math.random() * 1000;
setTimeout(() => {
if (Math.random() < 0.2) { //20% chance of failure
reject(new Error(`Simulated failure for job ${jobId}`));
} else {
resolve();
}
}, delayMs);
});
};
const processJob = async (jobId) => {
const locked = await acquireLock(jobId);
if (!locked) {
console.log(`[worker] job ${jobId} is locked by another worker, skipping`);
return;
}
await updateJobStatus(jobId, "active");
await incrementAttempts(jobId);
const job = await getJob(jobId);
console.log(`[worker] processing job ${jobId}`, job.payload);
try {
await simulateWork(jobId, job.payload);
await acknowledgeJob(jobId);
console.log(`[worker] job ${jobId} completed successfully`);
} catch (err) {
console.error(`[worker] job ${jobId} failed: ${err.message}`);
//exponential retry logic
if (job.attempts < job.max_attempts) {
const delayMs = Math.pow(2, job.attempts) * 1000;
const executeAt = Date.now() + delayMs;
await moveToDelayed(jobId, executeAt);
console.log(`[worker] job ${jobId} delayed for ${delayMs}ms (attempt ${job.attempts}/${job.max_attempts})`);
} else {
await moveToDeadLetter(jobId);
console.log(`[worker] job ${jobId} moved to dead-letter queue (exhausted ${job.max_attempts} attempts)`);
}
}
};
const run = async () => {
await redisClient.connect();
console.log("[worker] connected to Redis, waiting for jobs...");
while (true) {
try {
const jobId = await checkoutJob(POLL_TIMEOUT);
if (!jobId) continue;
console.log(`[worker] checked out job ${jobId}`);
await processJob(jobId);
} catch (err) {
console.error("[worker] error:", err.message);
await new Promise((r) => setTimeout(r, 1000));
}
}
};
run().catch((err) => {
console.error("[worker] fatal error:", err);
process.exit(1);
});