-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
errorExample.ts
executable file
·110 lines (95 loc) · 2.58 KB
/
errorExample.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env ts-node
import { Queue, Worker } from "../src";
/* In your projects:
import { Queue, Worker } from "node-resque";
*/
// ////////////////////////
// SET UP THE CONNECTION //
// ////////////////////////
const connectionDetails = {
pkg: "ioredis",
host: "127.0.0.1",
password: null,
port: 6379,
database: 0,
// namespace: 'resque',
// looping: true,
// // options: {password: 'abc'},
};
async function boot() {
// ///////////////////////////
// DEFINE YOUR WORKER TASKS //
// ///////////////////////////
let jobsToComplete = 0;
const jobs = {
brokenJob: {
plugins: [],
pluginOptions: {},
perform: function (a, b) {
jobsToComplete--;
tryShutdown();
throw new Error("broken message from job");
},
},
};
// just a helper for this demo
async function tryShutdown() {
if (jobsToComplete === 0) {
await new Promise((resolve) => {
setTimeout(resolve, 500);
});
await worker.end();
process.exit();
}
}
// /////////////////
// START A WORKER //
// /////////////////
const worker = new Worker(
{ connection: connectionDetails, queues: ["default"] },
jobs,
);
await worker.connect();
worker.start();
// //////////////////////
// REGESTER FOR EVENTS //
// //////////////////////
worker.on("start", () => {
console.log("worker started");
});
worker.on("end", () => {
console.log("worker ended");
});
worker.on("cleaning_worker", (worker, pid) => {
console.log(`cleaning old worker ${worker}`);
});
worker.on("poll", (queue) => {
console.log(`worker polling ${queue}`);
});
worker.on("job", (queue, job) => {
console.log(`working job ${queue} ${JSON.stringify(job)}`);
});
worker.on("reEnqueue", (queue, job, plugin) => {
console.log(`reEnqueue job (${plugin}) ${queue} ${JSON.stringify(job)}`);
});
worker.on("success", (queue, job, result) => {
console.log(`job success ${queue} ${JSON.stringify(job)} >> ${result}`);
});
worker.on("failure", (queue, job, failure) => {
console.log(`job failure ${queue} ${JSON.stringify(job)} >> ${failure}`);
});
worker.on("error", (error, queue, job) => {
console.log(`error ${queue} ${JSON.stringify(job)} >> ${error}`);
});
worker.on("pause", () => {
console.log("worker paused");
});
// /////////////////////
// CONNECT TO A QUEUE //
// /////////////////////
const queue = new Queue({ connection: connectionDetails }, jobs);
await queue.connect();
await queue.enqueue("default", "brokenJob", [1, 2]);
jobsToComplete = 1;
}
boot();