snugkv-nest 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/queue/cron.d.ts +21 -0
- package/dist/core/queue/cron.js +144 -0
- package/dist/core/queue/cron.js.map +1 -0
- package/dist/core/queue/events.d.ts +32 -0
- package/dist/core/queue/events.js +98 -0
- package/dist/core/queue/events.js.map +1 -0
- package/dist/core/queue/flow.d.ts +35 -0
- package/dist/core/queue/flow.js +64 -0
- package/dist/core/queue/flow.js.map +1 -0
- package/dist/core/queue/index.d.ts +7 -0
- package/dist/core/queue/index.js +27 -0
- package/dist/core/queue/index.js.map +1 -0
- package/dist/core/queue/job.d.ts +68 -0
- package/dist/core/queue/job.js +221 -0
- package/dist/core/queue/job.js.map +1 -0
- package/dist/core/queue/queue.d.ts +152 -0
- package/dist/core/queue/queue.js +346 -0
- package/dist/core/queue/queue.js.map +1 -0
- package/dist/core/queue/types.d.ts +79 -0
- package/dist/core/queue/types.js +85 -0
- package/dist/core/queue/types.js.map +1 -0
- package/dist/core/queue/worker.d.ts +87 -0
- package/dist/core/queue/worker.js +307 -0
- package/dist/core/queue/worker.js.map +1 -0
- package/dist/nest/explorer.d.ts +13 -0
- package/dist/nest/explorer.js +51 -0
- package/dist/nest/explorer.js.map +1 -1
- package/dist/nest/index.d.ts +3 -1
- package/dist/nest/index.js +4 -1
- package/dist/nest/index.js.map +1 -1
- package/dist/nest/module.d.ts +4 -0
- package/dist/nest/module.js +8 -2
- package/dist/nest/module.js.map +1 -1
- package/dist/nest/queue-decorators.d.ts +34 -0
- package/dist/nest/queue-decorators.js +56 -0
- package/dist/nest/queue-decorators.js.map +1 -0
- package/dist/nest/queue-module.d.ts +22 -0
- package/dist/nest/queue-module.js +41 -0
- package/dist/nest/queue-module.js.map +1 -0
- package/dist/nest/runtime.d.ts +1 -0
- package/dist/nest/runtime.js +15 -2
- package/dist/nest/runtime.js.map +1 -1
- package/package.json +6 -3
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Job = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
5
|
+
const client_1 = require("../client");
|
|
6
|
+
const types_1 = require("./types");
|
|
7
|
+
/** A job as stored on the server. Fetched jobs are snapshots; call `getState()` or `Queue.getJob()` for fresh data. */
|
|
8
|
+
class Job {
|
|
9
|
+
queue;
|
|
10
|
+
id;
|
|
11
|
+
name;
|
|
12
|
+
data;
|
|
13
|
+
opts;
|
|
14
|
+
timestamp;
|
|
15
|
+
delay;
|
|
16
|
+
priority;
|
|
17
|
+
progress;
|
|
18
|
+
returnvalue;
|
|
19
|
+
failedReason;
|
|
20
|
+
stacktrace;
|
|
21
|
+
attemptsMade;
|
|
22
|
+
stalledCounter;
|
|
23
|
+
processedOn;
|
|
24
|
+
finishedOn;
|
|
25
|
+
/** Lock token while a worker holds the job. */
|
|
26
|
+
token;
|
|
27
|
+
/** Scheduler that created this job. */
|
|
28
|
+
schedulerId;
|
|
29
|
+
parent;
|
|
30
|
+
/** Children that have not finished. */
|
|
31
|
+
pendingChildren;
|
|
32
|
+
/** Set by a worker. */
|
|
33
|
+
raw;
|
|
34
|
+
constructor(queue, id, raw) {
|
|
35
|
+
this.queue = queue;
|
|
36
|
+
this.id = id;
|
|
37
|
+
this.raw = raw;
|
|
38
|
+
this.name = raw.name ?? '';
|
|
39
|
+
this.data = (0, types_1.parseJson)(raw.data);
|
|
40
|
+
this.timestamp = Number(raw.ts ?? 0);
|
|
41
|
+
this.delay = Number(raw.delay ?? 0);
|
|
42
|
+
this.priority = Number(raw.prio ?? 0);
|
|
43
|
+
this.progress = raw.prg === undefined ? 0 : (0, types_1.parseJson)(raw.prg);
|
|
44
|
+
this.returnvalue = (0, types_1.parseJson)(raw.rv);
|
|
45
|
+
this.failedReason = raw.fr || undefined;
|
|
46
|
+
this.stacktrace = raw.st ? (0, types_1.parseJson)(raw.st) : [];
|
|
47
|
+
if (!Array.isArray(this.stacktrace))
|
|
48
|
+
this.stacktrace = [String(raw.st)];
|
|
49
|
+
this.attemptsMade = Number(raw.atm ?? 0);
|
|
50
|
+
this.stalledCounter = Number(raw.stc ?? 0);
|
|
51
|
+
this.processedOn = raw.po ? Number(raw.po) : undefined;
|
|
52
|
+
this.finishedOn = raw.fo ? Number(raw.fo) : undefined;
|
|
53
|
+
this.token = raw.tk || undefined;
|
|
54
|
+
this.schedulerId = raw.rep || undefined;
|
|
55
|
+
this.parent = raw.pq ? { id: raw.pid ?? '', queue: (0, types_1.queueNameOf)(raw.pq) } : undefined;
|
|
56
|
+
this.pendingChildren = Number(raw.pend ?? 0);
|
|
57
|
+
const bt = raw.bt;
|
|
58
|
+
this.opts = {
|
|
59
|
+
attempts: Number(raw.att ?? 1),
|
|
60
|
+
delay: this.delay,
|
|
61
|
+
priority: this.priority,
|
|
62
|
+
lifo: raw.lifo === '1' || undefined,
|
|
63
|
+
backoff: bt ? { type: bt, delay: Number(raw.bd ?? 0) } : undefined,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
static fromFlat(queue, id, flat) {
|
|
67
|
+
return new Job(queue, id, (0, client_1.pairsToObject)(flat));
|
|
68
|
+
}
|
|
69
|
+
/** Where this job is now. */
|
|
70
|
+
async getState() {
|
|
71
|
+
return (await this.queue.fcall('snugq_state', this.id));
|
|
72
|
+
}
|
|
73
|
+
async updateProgress(progress) {
|
|
74
|
+
await this.queue.fcall('snugq_progress', this.id, (0, types_1.toJson)(progress));
|
|
75
|
+
this.progress = progress;
|
|
76
|
+
}
|
|
77
|
+
/** Adds a line to the job log. Returns the log length. */
|
|
78
|
+
async log(line) {
|
|
79
|
+
return (await this.queue.fcall('snugq_log', this.id, line, this.opts.keepLogs ?? 0));
|
|
80
|
+
}
|
|
81
|
+
async updateData(data) {
|
|
82
|
+
await this.queue.fcall('snugq_update_data', this.id, (0, types_1.toJson)(data));
|
|
83
|
+
this.data = data;
|
|
84
|
+
}
|
|
85
|
+
/** Removes the job and its unfinished children. Returns false if a worker holds it. */
|
|
86
|
+
async remove() {
|
|
87
|
+
return (await this.queue.fcall('snugq_remove', this.id)) === 1;
|
|
88
|
+
}
|
|
89
|
+
/** Puts a failed (or completed) job back in the queue. */
|
|
90
|
+
async retry(state = 'failed', opts = {}) {
|
|
91
|
+
const r = (await this.queue.fcall('snugq_retry', this.id, state, opts.resetAttemptsMade ? 1 : 0));
|
|
92
|
+
if (r === -1)
|
|
93
|
+
throw new Error(`job ${this.id} does not exist`);
|
|
94
|
+
if (r === -5)
|
|
95
|
+
throw new Error(`job ${this.id} is not ${state}`);
|
|
96
|
+
}
|
|
97
|
+
/** Runs a delayed job now. */
|
|
98
|
+
async promote() {
|
|
99
|
+
const r = (await this.queue.fcall('snugq_promote', this.id));
|
|
100
|
+
if (r === -1)
|
|
101
|
+
throw new Error(`job ${this.id} does not exist`);
|
|
102
|
+
if (r === -3)
|
|
103
|
+
throw new Error(`job ${this.id} is not delayed`);
|
|
104
|
+
}
|
|
105
|
+
async changeDelay(delayMs) {
|
|
106
|
+
const r = (await this.queue.fcall('snugq_change_delay', this.id, Math.max(0, Math.floor(delayMs))));
|
|
107
|
+
if (r === -1)
|
|
108
|
+
throw new Error(`job ${this.id} does not exist`);
|
|
109
|
+
if (r === -3)
|
|
110
|
+
throw new Error(`job ${this.id} is not delayed`);
|
|
111
|
+
}
|
|
112
|
+
async changePriority(opts) {
|
|
113
|
+
const r = (await this.queue.fcall('snugq_change_priority', this.id, opts.priority, opts.lifo ? 1 : 0));
|
|
114
|
+
if (r === -1)
|
|
115
|
+
throw new Error(`job ${this.id} does not exist`);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* From a processor: delay this job until `timestamp` (ms since epoch), then throw `new DelayedError()`.
|
|
119
|
+
* It does not use up an attempt.
|
|
120
|
+
*/
|
|
121
|
+
async moveToDelayed(timestamp, token) {
|
|
122
|
+
const t = token ?? this.token;
|
|
123
|
+
if (!t)
|
|
124
|
+
throw new Error('moveToDelayed needs the lock token');
|
|
125
|
+
const r = (await this.queue.fcall('snugq_move_to_delayed', this.id, t, Math.max(0, Math.floor(timestamp - Date.now()))));
|
|
126
|
+
if (r === -2)
|
|
127
|
+
throw new Error(`lock of job ${this.id} was lost`);
|
|
128
|
+
}
|
|
129
|
+
/** Log lines. */
|
|
130
|
+
async getLogs(start = 0, end = -1) {
|
|
131
|
+
await this.queue.ready();
|
|
132
|
+
return this.queue.client.lrange(`${this.queue.base}job:${this.id}:logs`, start, end);
|
|
133
|
+
}
|
|
134
|
+
/** Results of finished children, keyed by `queue:jobId`. */
|
|
135
|
+
async getChildrenValues() {
|
|
136
|
+
await this.queue.ready();
|
|
137
|
+
const h = await this.queue.client.hgetall(`${this.queue.base}job:${this.id}:processed`);
|
|
138
|
+
const out = {};
|
|
139
|
+
for (const [ref, v] of Object.entries(h))
|
|
140
|
+
out[refToKey(ref)] = (0, types_1.parseJson)(v);
|
|
141
|
+
return out;
|
|
142
|
+
}
|
|
143
|
+
/** Reasons of children that failed, keyed by `queue:jobId`. */
|
|
144
|
+
async getFailedChildrenValues() {
|
|
145
|
+
await this.queue.ready();
|
|
146
|
+
const h = await this.queue.client.hgetall(`${this.queue.base}job:${this.id}:failed`);
|
|
147
|
+
const out = {};
|
|
148
|
+
for (const [ref, v] of Object.entries(h))
|
|
149
|
+
out[refToKey(ref)] = v;
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
/** Resolves with the return value when the job completes; rejects when it fails. Needs a started {@link QueueEvents}. */
|
|
153
|
+
async waitUntilFinished(events, ttlMs) {
|
|
154
|
+
await events.waitUntilReady();
|
|
155
|
+
return new Promise((resolve, reject) => {
|
|
156
|
+
let timer;
|
|
157
|
+
const cleanup = () => {
|
|
158
|
+
events.off('completed', onDone);
|
|
159
|
+
events.off('failed', onFail);
|
|
160
|
+
if (timer)
|
|
161
|
+
clearTimeout(timer);
|
|
162
|
+
};
|
|
163
|
+
const onDone = (a) => {
|
|
164
|
+
if (a.jobId !== this.id)
|
|
165
|
+
return;
|
|
166
|
+
cleanup();
|
|
167
|
+
resolve(a.returnvalue);
|
|
168
|
+
};
|
|
169
|
+
const onFail = (a) => {
|
|
170
|
+
if (a.jobId !== this.id)
|
|
171
|
+
return;
|
|
172
|
+
cleanup();
|
|
173
|
+
reject(new Error(a.failedReason));
|
|
174
|
+
};
|
|
175
|
+
events.on('completed', onDone);
|
|
176
|
+
events.on('failed', onFail);
|
|
177
|
+
if (ttlMs) {
|
|
178
|
+
timer = setTimeout(() => {
|
|
179
|
+
cleanup();
|
|
180
|
+
reject(new Error(`job ${this.id} did not finish within ${ttlMs} ms`));
|
|
181
|
+
}, ttlMs);
|
|
182
|
+
}
|
|
183
|
+
// Events may have happened before we listened: look at the stored state too.
|
|
184
|
+
void this.queue.fcall('snugq_state', this.id).then(async (state) => {
|
|
185
|
+
if (state !== 'completed' && state !== 'failed')
|
|
186
|
+
return;
|
|
187
|
+
const h = await this.queue.client.hmget(`${this.queue.base}job:${this.id}`, 'rv', 'fr');
|
|
188
|
+
if (state === 'completed')
|
|
189
|
+
onDone({ jobId: this.id, returnvalue: (0, types_1.parseJson)(h[0] ?? undefined) });
|
|
190
|
+
else
|
|
191
|
+
onFail({ jobId: this.id, failedReason: h[1] ?? 'failed' });
|
|
192
|
+
}, reject);
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
toJSON() {
|
|
196
|
+
return {
|
|
197
|
+
id: this.id,
|
|
198
|
+
name: this.name,
|
|
199
|
+
data: this.data,
|
|
200
|
+
opts: this.opts,
|
|
201
|
+
progress: this.progress,
|
|
202
|
+
timestamp: this.timestamp,
|
|
203
|
+
delay: this.delay,
|
|
204
|
+
priority: this.priority,
|
|
205
|
+
attemptsMade: this.attemptsMade,
|
|
206
|
+
processedOn: this.processedOn,
|
|
207
|
+
finishedOn: this.finishedOn,
|
|
208
|
+
returnvalue: this.returnvalue,
|
|
209
|
+
failedReason: this.failedReason,
|
|
210
|
+
stacktrace: this.stacktrace,
|
|
211
|
+
parent: this.parent,
|
|
212
|
+
schedulerId: this.schedulerId,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
exports.Job = Job;
|
|
217
|
+
function refToKey(ref) {
|
|
218
|
+
const i = ref.lastIndexOf('|');
|
|
219
|
+
return i < 0 ? ref : `${(0, types_1.queueNameOf)(ref.slice(0, i))}:${ref.slice(i + 1)}`;
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=job.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"job.js","sourceRoot":"","sources":["../../../src/core/queue/job.ts"],"names":[],"mappings":";;;AAAA,uDAAuD;AACvD,sCAA0C;AAE1C,mCAA2F;AAE3F,uHAAuH;AACvH,MAAa,GAAG;IA0BO;IAzBZ,EAAE,CAAS;IACX,IAAI,CAAS;IACtB,IAAI,CAAI;IACC,IAAI,CAAc;IAClB,SAAS,CAAS;IAClB,KAAK,CAAS;IACd,QAAQ,CAAS;IAC1B,QAAQ,CAAM;IACd,WAAW,CAAgB;IAC3B,YAAY,CAAqB;IACjC,UAAU,CAAW;IACrB,YAAY,CAAS;IACrB,cAAc,CAAS;IACvB,WAAW,CAAqB;IAChC,UAAU,CAAqB;IAC/B,+CAA+C;IAC/C,KAAK,CAAqB;IAC1B,uCAAuC;IAC9B,WAAW,CAAqB;IAChC,MAAM,CAA4C;IAC3D,uCAAuC;IAC9B,eAAe,CAAS;IACjC,uBAAuB;IACd,GAAG,CAAyB;IAErC,YAAqB,KAAgB,EAAE,EAAU,EAAE,GAA2B;QAAzD,UAAK,GAAL,KAAK,CAAW;QACnC,IAAI,CAAC,EAAE,GAAG,EAAE,CAAC;QACb,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IA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C,GAAW;IAC3B,MAAM,CAAC,GAAG,GAAG,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;IAC/B,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,IAAA,mBAAW,EAAC,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC;AAC7E,CAAC"}
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { EventEmitter } from 'node:events';
|
|
2
|
+
import { SnugKvClient, SnugKvClientOptions } from '../client';
|
|
3
|
+
import { Arg } from '../resp';
|
|
4
|
+
import { Job } from './job';
|
|
5
|
+
import { JobsOptions, JobState, JobType, QueueLike, toJson } from './types';
|
|
6
|
+
export interface QueueOptions {
|
|
7
|
+
/** Share an existing client. It is not closed with the queue. */
|
|
8
|
+
client?: SnugKvClient;
|
|
9
|
+
/** Or give connection settings; the queue then owns and closes its client. */
|
|
10
|
+
connection?: SnugKvClientOptions;
|
|
11
|
+
/** Key prefix. Default `snugq`. All services using one queue must agree on it. */
|
|
12
|
+
prefix?: string;
|
|
13
|
+
/** Options merged into every `add`. */
|
|
14
|
+
defaultJobOptions?: JobsOptions;
|
|
15
|
+
}
|
|
16
|
+
export interface RepeatOptions {
|
|
17
|
+
/** Repeat every N ms (computed on the server). */
|
|
18
|
+
every?: number;
|
|
19
|
+
/** Cron pattern with 5 fields, or 6 with seconds first. */
|
|
20
|
+
pattern?: string;
|
|
21
|
+
/** IANA time zone for the pattern. Default UTC. */
|
|
22
|
+
tz?: string;
|
|
23
|
+
/** Stop after this many runs. */
|
|
24
|
+
limit?: number;
|
|
25
|
+
/** Stop after this time. */
|
|
26
|
+
endDate?: number | Date;
|
|
27
|
+
}
|
|
28
|
+
export interface JobTemplate<D = any> {
|
|
29
|
+
name?: string;
|
|
30
|
+
data?: D;
|
|
31
|
+
opts?: JobsOptions;
|
|
32
|
+
}
|
|
33
|
+
export interface JobScheduler {
|
|
34
|
+
id: string;
|
|
35
|
+
name: string;
|
|
36
|
+
every?: number;
|
|
37
|
+
pattern?: string;
|
|
38
|
+
tz?: string;
|
|
39
|
+
limit?: number;
|
|
40
|
+
endDate?: number;
|
|
41
|
+
/** Runs created so far. */
|
|
42
|
+
count: number;
|
|
43
|
+
/** When the next run is due (ms since epoch). */
|
|
44
|
+
next: number;
|
|
45
|
+
template: {
|
|
46
|
+
name: string;
|
|
47
|
+
data: any;
|
|
48
|
+
opts: JobsOptions;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
export type JobCounts = Record<JobType, number>;
|
|
52
|
+
/** Builds the stored job from what we sent, so `add` needs no second round trip. */
|
|
53
|
+
export declare function localJob(q: QueueLike, id: string, name: string, data: unknown, opts: JobsOptions, parent?: {
|
|
54
|
+
base: string;
|
|
55
|
+
id: string;
|
|
56
|
+
}): Job;
|
|
57
|
+
/**
|
|
58
|
+
* A named queue of jobs. Producers call `add`; a {@link Worker} processes the jobs;
|
|
59
|
+
* {@link QueueEvents} reports what happens. Needs a SnugKV server with the built-in functions on.
|
|
60
|
+
*/
|
|
61
|
+
export declare class Queue<D = any, R = any> extends EventEmitter implements QueueLike {
|
|
62
|
+
readonly name: string;
|
|
63
|
+
readonly client: SnugKvClient;
|
|
64
|
+
readonly base: string;
|
|
65
|
+
readonly prefix: string;
|
|
66
|
+
private readonly owns;
|
|
67
|
+
private readyPromise?;
|
|
68
|
+
private readonly defaults;
|
|
69
|
+
constructor(name: string, options?: QueueOptions);
|
|
70
|
+
ready(): Promise<void>;
|
|
71
|
+
fcall(fn: string, ...args: Arg[]): Promise<any>;
|
|
72
|
+
private fcallRo;
|
|
73
|
+
add(name: string, data: D, opts?: JobsOptions): Promise<Job<D, R>>;
|
|
74
|
+
/** Adds many jobs in one round trip. Not atomic: each job is added on its own. */
|
|
75
|
+
addBulk(jobs: Array<{
|
|
76
|
+
name: string;
|
|
77
|
+
data: D;
|
|
78
|
+
opts?: JobsOptions;
|
|
79
|
+
}>): Promise<Array<Job<D, R>>>;
|
|
80
|
+
/** Base key of another queue with the same prefix. */
|
|
81
|
+
baseOf(queueName: string): string;
|
|
82
|
+
/**
|
|
83
|
+
* Creates or replaces a repeatable job. `every` repeats on the server; `pattern` (cron) is
|
|
84
|
+
* computed by the workers, so keep at least one worker running. Returns true when the first run was created.
|
|
85
|
+
*/
|
|
86
|
+
upsertJobScheduler(id: string, repeat: RepeatOptions, template?: JobTemplate<D>): Promise<boolean>;
|
|
87
|
+
removeJobScheduler(id: string): Promise<boolean>;
|
|
88
|
+
getJobSchedulers(start?: number, end?: number): Promise<JobScheduler[]>;
|
|
89
|
+
getJob(id: string): Promise<Job<D, R> | null>;
|
|
90
|
+
getJobState(id: string): Promise<JobState>;
|
|
91
|
+
/** Jobs in the given states. Lists are in processing order; sorted sets by time or priority. */
|
|
92
|
+
getJobs(types?: JobType | JobType[], start?: number, end?: number, asc?: boolean): Promise<Array<Job<D, R>>>;
|
|
93
|
+
getWaiting: (start?: number, end?: number) => Promise<Job<D, R>[]>;
|
|
94
|
+
getActive: (start?: number, end?: number) => Promise<Job<D, R>[]>;
|
|
95
|
+
getDelayed: (start?: number, end?: number) => Promise<Job<D, R>[]>;
|
|
96
|
+
getPrioritized: (start?: number, end?: number) => Promise<Job<D, R>[]>;
|
|
97
|
+
getWaitingChildren: (start?: number, end?: number) => Promise<Job<D, R>[]>;
|
|
98
|
+
getCompleted: (start?: number, end?: number) => Promise<Job<D, R>[]>;
|
|
99
|
+
getFailed: (start?: number, end?: number) => Promise<Job<D, R>[]>;
|
|
100
|
+
getJobCounts(...types: JobType[]): Promise<Partial<JobCounts>>;
|
|
101
|
+
getJobCountByTypes(...types: JobType[]): Promise<number>;
|
|
102
|
+
/** Jobs that still wait to run: waiting, paused, delayed and prioritized. */
|
|
103
|
+
count(): Promise<number>;
|
|
104
|
+
getJobLogs(id: string, start?: number, end?: number): Promise<{
|
|
105
|
+
logs: string[];
|
|
106
|
+
count: number;
|
|
107
|
+
}>;
|
|
108
|
+
/** Completed or failed jobs per minute, newest last. */
|
|
109
|
+
getMetrics(type: 'completed' | 'failed', minutes?: number): Promise<{
|
|
110
|
+
count: number;
|
|
111
|
+
data: number[];
|
|
112
|
+
minute: number;
|
|
113
|
+
}>;
|
|
114
|
+
pause(): Promise<void>;
|
|
115
|
+
resume(): Promise<void>;
|
|
116
|
+
isPaused(): Promise<boolean>;
|
|
117
|
+
/** Total jobs processed at the same time across all workers. */
|
|
118
|
+
setGlobalConcurrency(n: number): Promise<void>;
|
|
119
|
+
removeGlobalConcurrency(): Promise<void>;
|
|
120
|
+
getGlobalConcurrency(): Promise<number | null>;
|
|
121
|
+
/** At most `max` jobs start per `duration` ms across all workers. */
|
|
122
|
+
setRateLimit(limit: {
|
|
123
|
+
max: number;
|
|
124
|
+
duration: number;
|
|
125
|
+
}): Promise<void>;
|
|
126
|
+
removeRateLimit(): Promise<void>;
|
|
127
|
+
/** Stop all workers from starting jobs for `ms` ms. */
|
|
128
|
+
rateLimit(ms: number): Promise<void>;
|
|
129
|
+
getRateLimitTtl(): Promise<number>;
|
|
130
|
+
remove(id: string): Promise<boolean>;
|
|
131
|
+
/** Removes waiting and prioritized (and delayed, if asked) jobs. Returns how many. */
|
|
132
|
+
drain(includeDelayed?: boolean): Promise<number>;
|
|
133
|
+
/** Removes jobs older than `graceMs`. Returns their ids. `limit` 0 means all. */
|
|
134
|
+
clean(graceMs: number, limit: number, type?: Exclude<JobType, 'active'>): Promise<string[]>;
|
|
135
|
+
/** Puts failed (or completed) jobs back in the queue. Returns how many. */
|
|
136
|
+
retryJobs(opts?: {
|
|
137
|
+
state?: 'failed' | 'completed';
|
|
138
|
+
count?: number;
|
|
139
|
+
resetAttemptsMade?: boolean;
|
|
140
|
+
}): Promise<number>;
|
|
141
|
+
/** Runs delayed jobs now. Returns how many. */
|
|
142
|
+
promoteJobs(count?: number): Promise<number>;
|
|
143
|
+
/** Deletes the queue and all its jobs. Refuses while jobs are active, unless `force`. */
|
|
144
|
+
obliterate(opts?: {
|
|
145
|
+
force?: boolean;
|
|
146
|
+
count?: number;
|
|
147
|
+
}): Promise<void>;
|
|
148
|
+
/** Names of all queues that have had jobs, under a prefix. */
|
|
149
|
+
static list(client: SnugKvClient, prefix?: string): Promise<string[]>;
|
|
150
|
+
close(): Promise<void>;
|
|
151
|
+
}
|
|
152
|
+
export { toJson };
|