forked from actionhero/node-resque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Retry.ts
177 lines (150 loc) · 4.21 KB
/
Retry.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// If a job fails, retry it N times before finally placing it into the failed queue
// a port of some of the features in https://github.com/lantins/resque-retry
import * as os from "os";
import { Plugin } from "./../index";
export class Retry extends Plugin {
constructor(worker, func, queue, job, args, options) {
super(worker, func, queue, job, args, options);
if (!this.options.retryLimit) {
this.options.retryLimit = 1;
}
if (!this.options.retryDelay) {
this.options.retryDelay = 1000 * 5;
}
if (!this.options.backoffStrategy) {
this.options.backoffStrategy = null;
}
}
beforeEnqueue() {
return true;
}
afterEnqueue() {
return true;
}
beforePerform() {
return true;
}
async afterPerform() {
if (!this.worker.error) {
await this.cleanup();
return true;
}
const remaining = await this.attemptUp();
await this.saveLastError();
if (remaining <= 0) {
await this.cleanup();
throw this.worker.error;
}
let nextTryDelay = this.options.retryDelay;
if (Array.isArray(this.options.backoffStrategy)) {
let index = this.options.retryLimit - remaining - 1;
if (index > this.options.backoffStrategy.length - 1) {
index = this.options.backoffStrategy.length - 1;
}
nextTryDelay = this.options.backoffStrategy[index];
}
await this.queueObject.enqueueIn(
nextTryDelay,
this.queue,
this.func,
this.args
);
this.job.args = this.args;
this.worker.emit("reEnqueue", this.queue, this.job, {
delay: nextTryDelay,
remaningAttempts: remaining, // @deprecated
remainingAttempts: remaining,
err: this.worker.error,
});
await this.redis().decr(
this.queueObject.connection.key("stat", "processed")
);
await this.redis().decr(
this.queueObject.connection.key("stat", "processed", this.worker.name)
);
await this.redis().incr(this.queueObject.connection.key("stat", "failed"));
await this.redis().incr(
this.queueObject.connection.key("stat", "failed", this.worker.name)
);
delete this.worker.error;
return true;
}
argsKey() {
if (!this.args || this.args.length === 0) {
return "";
}
return this.args
.map(function (elem) {
return typeof elem === "object" ? JSON.stringify(elem) : elem;
})
.join("-");
}
retryKey() {
return this.queueObject.connection
.key("resque-retry", this.func, this.argsKey())
.replace(/\s/, "");
}
failureKey() {
return this.queueObject.connection
.key("failure-resque-retry:" + this.func + ":" + this.argsKey())
.replace(/\s/, "");
}
maxDelay() {
let maxDelay = this.options.retryDelay || 1;
if (Array.isArray(this.options.backoffStrategy)) {
this.options.backoffStrategy.forEach(function (d) {
if (d > maxDelay) {
maxDelay = d;
}
});
}
return maxDelay;
}
redis() {
return this.queueObject.connection.redis;
}
async attemptUp() {
const key = this.retryKey();
await this.redis().setnx(key, -1);
const retryCount = await this.redis().incr(key);
await this.redis().expire(key, this.maxDelay());
return this.options.retryLimit - retryCount - 1;
}
async saveLastError() {
const now = new Date();
const failedAt =
"" +
now.getFullYear() +
"/" +
("0" + (now.getMonth() + 1)).slice(-2) +
"/" +
("0" + now.getDate()).slice(-2) +
" " +
("0" + now.getHours()).slice(-2) +
":" +
("0" + now.getMinutes()).slice(-2) +
":" +
("0" + now.getSeconds()).slice(-2);
const backtrace = this.worker.error.stack
? this.worker.error.stack.split(os.EOL) || []
: [];
const data = {
failed_at: failedAt,
payload: this.args,
exception: String(this.worker.error),
error: String(this.worker.error),
backtrace: backtrace,
worker: this.func,
queue: this.queue,
};
await this.redis().setex(
this.failureKey(),
this.maxDelay(),
JSON.stringify(data)
);
}
async cleanup() {
await this.redis().del(this.retryKey());
await this.redis().del(this.failureKey());
}
}