forked from darky/bull-repl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
318 lines (294 loc) · 9.95 KB
/
index.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/// <reference types="./typing" />
import {
ConnectParams,
ActiveParams,
WaitingParams,
CompletedParams,
FailedParams,
DelayedParams,
GetParams,
AddParams,
RmParams,
RetryParams,
PromoteParams,
FailParams,
CompleteParams,
CleanParams,
LogsParams,
LogParams
} from "./src/types";
import Vorpal from "vorpal";
import ms from "ms";
import {
showJobs,
getFilter,
getTimeAgoFilter,
searchjsLink,
msLink,
logArray,
getJob,
answer,
logGreen,
throwYellow,
logYellow,
splitJobsByFound,
wrapTryCatch
} from "./src/utils";
import { getQueue, setQueue } from "./src/queue";
export { getQueue, setQueue };
export const vorpal = new Vorpal();
vorpal
.command("connect <queue>", "connect to bull queue")
.option("-p, --prefix <prefix>", "prefix to use for all queue jobs")
.option(
"-r, --redis <redis>",
"redis url in format: redis://[:password@]host[:port][/db-number][?option=value]; default redis://localhost:6379"
)
.action(
wrapTryCatch(async ({ queue: name, options }: ConnectParams) => {
const url = options.redis
? `redis://${options.redis.replace(/^redis:\/\//, "")}`
: "redis://localhost:6379";
const prefix = options.prefix || "bull";
await setQueue(name, url, { prefix });
logGreen(`Connected to ${url}, prefix: ${prefix}, queue: ${name}`);
vorpal.delimiter(`BULL-REPL | ${prefix}.${name}> `).show();
})
);
vorpal.command("stats", "count of jobs by groups").action(
wrapTryCatch(async () => {
const queue = await getQueue();
const [counts, paused] = await Promise.all([
queue.getJobCounts(),
queue.getPausedCount()
]);
console.table({ ...counts, ...{ paused } });
})
);
vorpal
.command("active", "fetch active jobs")
.option("-f, --filter <filter>", `filter jobs via ${searchjsLink}`)
.option("-ta, --timeAgo <timeAgo>", `get jobs since time ago via ${msLink}`)
.action(
wrapTryCatch(async ({ options }: ActiveParams) => {
const queue = await getQueue();
const filter = await getFilter(options.filter);
const timeAgoFilter = await getTimeAgoFilter(options.timeAgo);
showJobs(await queue.getActive(), { ...filter, ...timeAgoFilter });
})
);
vorpal
.command("waiting", "fetch waiting jobs")
.option("-f, --filter <filter>", `filter jobs via ${searchjsLink}`)
.option("-ta, --timeAgo <timeAgo>", `get jobs since time ago via ${msLink}`)
.action(
wrapTryCatch(async ({ options }: WaitingParams) => {
const queue = await getQueue();
const filter = await getFilter(options.filter);
const timeAgoFilter = await getTimeAgoFilter(options.timeAgo);
showJobs(await queue.getWaiting(), { ...filter, ...timeAgoFilter });
})
);
vorpal
.command("completed", "fetch completed jobs")
.option("-f, --filter <filter>", `filter jobs via ${searchjsLink}`)
.option("-ta, --timeAgo <timeAgo>", `get jobs since time ago via ${msLink}`)
.action(
wrapTryCatch(async ({ options }: CompletedParams) => {
const queue = await getQueue();
const filter = await getFilter(options.filter);
const timeAgoFilter = await getTimeAgoFilter(options.timeAgo);
showJobs(await queue.getCompleted(), { ...filter, ...timeAgoFilter });
})
);
vorpal
.command("failed", "fetch failed jobs")
.option("-f, --filter <filter>", `filter jobs via ${searchjsLink}`)
.option("-ta, --timeAgo <timeAgo>", `get jobs since time ago via ${msLink}`)
.action(
wrapTryCatch(async ({ options }: FailedParams) => {
const queue = await getQueue();
const filter = await getFilter(options.filter);
const timeAgoFilter = await getTimeAgoFilter(options.timeAgo);
showJobs(await queue.getFailed(), { ...filter, ...timeAgoFilter });
})
);
vorpal
.command("delayed", "fetch delayed jobs")
.option("-f, --filter <filter>", `filter jobs via ${searchjsLink}`)
.option("-ta, --timeAgo <timeAgo>", `get jobs since time ago via ${msLink}`)
.action(
wrapTryCatch(async ({ options }: DelayedParams) => {
const queue = await getQueue();
const filter = await getFilter(options.filter);
const timeAgoFilter = await getTimeAgoFilter(options.timeAgo);
showJobs(await queue.getDelayed(), { ...filter, ...timeAgoFilter });
})
);
vorpal.command("pause", "pause current queue").action(
wrapTryCatch(async () => {
const queue = await getQueue();
await answer(vorpal, "Pause queue");
await queue.pause(false);
logGreen(`Queue paused`);
})
);
vorpal.command("resume", "resume current queue from pause").action(
wrapTryCatch(async () => {
const queue = await getQueue();
await answer(vorpal, "Resume queue");
await queue.resume(false);
logGreen(`Queue resumed from pause`);
})
);
vorpal.command("get <jobId...>", "get job").action(
wrapTryCatch(async ({ jobId }: GetParams) => {
const { notFoundIds, foundJobs } = await splitJobsByFound(jobId);
notFoundIds.length && logYellow(`Not found jobs: ${notFoundIds}`);
foundJobs.length && showJobs(foundJobs, {});
})
);
vorpal
.command("add <data>", "add job to queue")
.option("-n, --name <name>", "name for named job")
.action(
wrapTryCatch(async function({ data, options }: AddParams) {
const queue = await getQueue();
let jobData: object;
try {
jobData = JSON.parse(data);
} catch (e) {
return throwYellow(`Error occured, seems "data" incorrect json`);
}
await answer(vorpal, "Add");
const jobName: string = options.name || "__default__";
const addedJob = await queue.add(jobName, jobData);
logGreen(`Job with name '${jobName}', id '${addedJob.id}' added`);
})
);
vorpal.command("rm <jobId...>", "remove job").action(
wrapTryCatch(async function({ jobId }: RmParams) {
await answer(vorpal, "Remove");
const { notFoundIds, foundJobs } = await splitJobsByFound(jobId);
await Promise.all(foundJobs.map(j => j.remove()));
notFoundIds.length && logYellow(`Not found jobs: ${notFoundIds}`);
foundJobs.length && logGreen(`Jobs "${foundJobs.map(j => j.id)}" removed`);
})
);
vorpal.command("retry <jobId...>", "retry job").action(
wrapTryCatch(async function({ jobId }: RetryParams) {
await answer(vorpal, "Retry");
const { notFoundIds, foundJobs } = await splitJobsByFound(jobId);
await Promise.all(foundJobs.map(j => j.retry()));
notFoundIds.length && logYellow(`Not found jobs: ${notFoundIds}`);
foundJobs.length && logGreen(`Jobs "${foundJobs.map(j => j.id)}" retried`);
})
);
vorpal.command("retry-failed", "retry all failed jobs").action(
wrapTryCatch(async function() {
const queue = await getQueue();
await answer(vorpal, "Retry failed jobs");
const failedJobs = await queue.getFailed();
await Promise.all(failedJobs.map(j => j.retry()));
logGreen('All failed jobs retried');
})
);
vorpal.command("promote <jobId...>", "promote job").action(
wrapTryCatch(async function({ jobId }: PromoteParams) {
await answer(vorpal, "Promote");
const { notFoundIds, foundJobs } = await splitJobsByFound(jobId);
await Promise.all(foundJobs.map(j => j.promote()));
notFoundIds.length && logYellow(`Not found jobs: ${notFoundIds}`);
foundJobs.length && logGreen(`Jobs "${foundJobs.map(j => j.id)}" promoted`);
})
);
vorpal.command("fail <jobId> <reason>", "fail job").action(
wrapTryCatch(async function({ jobId, reason }: FailParams) {
await getQueue();
const job = await getJob(jobId);
await answer(vorpal, "Fail");
await job.moveToFailed({ message: reason }, true);
logGreen(`Job "${jobId}" failed`);
})
);
vorpal.command("complete <jobId> <data>", "complete job").action(
wrapTryCatch(async function({ jobId, data }: CompleteParams) {
await getQueue();
const job = await getJob(jobId);
let returnValue: string;
try {
returnValue = JSON.parse(data);
} catch (e) {
return throwYellow(`Error occured, seems "data" incorrect json`);
}
await answer(vorpal, "Complete");
await job.moveToCompleted(returnValue, true);
logGreen(`Job "${jobId}" completed`);
})
);
vorpal
.command(
"clean <period>",
`Clean queue for period ago, period format - ${msLink}`
)
.option(
"-s, --status <status>",
"Status of the job to clean, default: completed"
)
.option(
"-l, --limit <limit>",
"Maximum amount of jobs to clean per call, default: all"
)
.action(
wrapTryCatch(async function({ period, options }: CleanParams) {
const queue = await getQueue();
await answer(vorpal, "Clean");
const grace = period && period.length ? ms(period as string) : void 0;
if (!grace) {
return throwYellow("Incorrect period");
}
const status = options.status || "completed";
if (
!["completed", "wait", "active", "delayed", "failed"].includes(status)
) {
return throwYellow(
"Incorrect status, should be: completed or wait or active or delayed or failed"
);
}
const limit = Number.isInteger(options.limit as number)
? options.limit
: void 0;
await queue.clean(grace, status, limit);
logGreen(`Jobs cleaned`);
})
);
vorpal
.command("logs <jobId>", "get logs of job")
.option("-s, --start <start>", "Start of logs")
.option("-e, --end <end>", "End of logs")
.action(
wrapTryCatch(async ({ jobId, options }: LogsParams) => {
const queue = await getQueue();
const { logs, count } = await queue.getJobLogs(
jobId,
options.start,
options.end
);
console.log(`Count of job logs: ${count}`);
if (logs.length) {
console.log("Logs:");
logArray(logs);
}
})
);
vorpal.command("log <jobId> <data>", "add log to job").action(
wrapTryCatch(async function({ jobId, data }: LogParams) {
await getQueue();
const job = await getJob(jobId);
await answer(vorpal, "Add log");
await job.log(data);
logGreen("Log added to job");
})
);
vorpal.history("bull-repl-default");
vorpal.delimiter("BULL-REPL> ").show();