power-queues 1.0.4 → 1.0.6
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/dist/index.d.ts +5 -6
- package/dist/index.js +5 -7
- package/dist/index.js.map +1 -1
- package/dist/{Processor.d.ts → src/Processor.d.ts} +2 -1
- package/dist/{Processor.js → src/Processor.js} +2 -2
- package/dist/src/Processor.js.map +1 -0
- package/dist/src/Queue.d.ts +29 -0
- package/dist/src/Queue.js +210 -0
- package/dist/src/Queue.js.map +1 -0
- package/dist/src/QueueProcessor.d.ts +11 -0
- package/dist/src/QueueProcessor.js +34 -0
- package/dist/src/QueueProcessor.js.map +1 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +10 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/types.d.ts +30 -0
- package/dist/src/types.js.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +2 -55
- package/dist/Processor.js.map +0 -1
- package/dist/Queue.d.ts +0 -30
- package/dist/Queue.js +0 -124
- package/dist/Queue.js.map +0 -1
- package/dist/QueueMethod.d.ts +0 -9
- package/dist/QueueMethod.js +0 -29
- package/dist/QueueMethod.js.map +0 -1
- package/dist/RedisManager.d.ts +0 -22
- package/dist/RedisManager.js +0 -120
- package/dist/RedisManager.js.map +0 -1
- package/dist/types.d.ts +0 -14
- package/dist/types.js.map +0 -1
- /package/dist/{types.js → src/types.js} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Queue } from './Queue';
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
export { TaskInterface, Processor, Queue, QueueMethod, RedisManager, };
|
|
1
|
+
import { Processor } from './src/Processor';
|
|
2
|
+
import { QueueProcessor } from './src/QueueProcessor';
|
|
3
|
+
import { Queue } from './src/Queue';
|
|
4
|
+
import { DistLockInterface, LockOptsInterface, TaskOptsInterface, TaskInterface } from './src/types';
|
|
5
|
+
export { Processor, QueueProcessor, Queue, DistLockInterface, LockOptsInterface, TaskOptsInterface, TaskInterface, };
|
package/dist/index.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const Processor_1 = require("./Processor");
|
|
3
|
+
exports.Queue = exports.QueueProcessor = exports.Processor = void 0;
|
|
4
|
+
const Processor_1 = require("./src/Processor");
|
|
5
5
|
Object.defineProperty(exports, "Processor", { enumerable: true, get: function () { return Processor_1.Processor; } });
|
|
6
|
-
const
|
|
6
|
+
const QueueProcessor_1 = require("./src/QueueProcessor");
|
|
7
|
+
Object.defineProperty(exports, "QueueProcessor", { enumerable: true, get: function () { return QueueProcessor_1.QueueProcessor; } });
|
|
8
|
+
const Queue_1 = require("./src/Queue");
|
|
7
9
|
Object.defineProperty(exports, "Queue", { enumerable: true, get: function () { return Queue_1.Queue; } });
|
|
8
|
-
const QueueMethod_1 = require("./QueueMethod");
|
|
9
|
-
Object.defineProperty(exports, "QueueMethod", { enumerable: true, get: function () { return QueueMethod_1.QueueMethod; } });
|
|
10
|
-
const RedisManager_1 = require("./RedisManager");
|
|
11
|
-
Object.defineProperty(exports, "RedisManager", { enumerable: true, get: function () { return RedisManager_1.RedisManager; } });
|
|
12
10
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;AAAA,+CAA4C;AAW3C,0FAXQ,qBAAS,OAWR;AAVV,yDAAsD;AAWrD,+FAXQ,+BAAc,OAWR;AAVf,uCAAoC;AAWnC,sFAXQ,aAAK,OAWR"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { TaskInterface } from './types';
|
|
1
2
|
export declare class Processor {
|
|
2
3
|
readonly name: string;
|
|
3
|
-
execute(queueName: string,
|
|
4
|
+
execute(queueName: string, task: TaskInterface): Promise<any>;
|
|
4
5
|
methods(): Array<Function>;
|
|
5
6
|
method(index: number): Function;
|
|
6
7
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Processor.js","sourceRoot":"","sources":["../../src/Processor.ts"],"names":[],"mappings":";;;AAEA,MAAa,SAAS;IAGrB,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,IAAmB;QACnD,OAAO,IAAI,CAAC;IACb,CAAC;IAED,OAAO;QACN,OAAO,EAAE,CAAC;IACX,CAAC;IAED,MAAM,CAAC,KAAa;QACnB,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;IAC9B,CAAC;CACD;AAdD,8BAcC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { LockOptsInterface, DistLockInterface, TaskInterface } from './types';
|
|
2
|
+
export declare class Queue {
|
|
3
|
+
readonly redisService: any;
|
|
4
|
+
readonly attempts: number;
|
|
5
|
+
readonly portion: number;
|
|
6
|
+
running: boolean;
|
|
7
|
+
wait(ms: number): Promise<unknown>;
|
|
8
|
+
key(...parts: Array<string | number>): string;
|
|
9
|
+
connection(db: string): any;
|
|
10
|
+
checkConnection(db: string): boolean;
|
|
11
|
+
start(db: string, queue: string, attempt?: number): void;
|
|
12
|
+
drop(db: string, pattern: string): Promise<number>;
|
|
13
|
+
unlock(db: string, key: string, token: string): Promise<boolean>;
|
|
14
|
+
lock(db: string, key: string, opts?: LockOptsInterface): Promise<DistLockInterface | null>;
|
|
15
|
+
write(db: string, key: string, value: any, ttlMs?: number): Promise<any>;
|
|
16
|
+
protected onStart(queue: string, portion: Array<TaskInterface>): Promise<void>;
|
|
17
|
+
protected onSuccess(queue: string, task: TaskInterface, result: any): Promise<void>;
|
|
18
|
+
protected onError(queue: string, task: TaskInterface, err: any): Promise<void>;
|
|
19
|
+
protected onFatal(queue: string, task: TaskInterface, err: any): Promise<void>;
|
|
20
|
+
protected onEnd(queue: string, result: any): Promise<void>;
|
|
21
|
+
protected beforeExecute(queue: string, task: TaskInterface): Promise<any>;
|
|
22
|
+
protected afterExecute(queue: string, portion: Array<TaskInterface>, result: any): Promise<any>;
|
|
23
|
+
protected callback(queue: string, task: TaskInterface): Promise<any>;
|
|
24
|
+
protected execute(queue: string, task: TaskInterface): Promise<any>;
|
|
25
|
+
protected retry(db: string, queue: string, task: TaskInterface): Promise<void>;
|
|
26
|
+
private select;
|
|
27
|
+
private process;
|
|
28
|
+
private loop;
|
|
29
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Queue = void 0;
|
|
4
|
+
const crypto = require("crypto");
|
|
5
|
+
const full_utils_1 = require("full-utils");
|
|
6
|
+
const UNLOCK_LUA = `
|
|
7
|
+
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
|
8
|
+
return redis.call("DEL", KEYS[1])
|
|
9
|
+
else
|
|
10
|
+
return 0
|
|
11
|
+
end
|
|
12
|
+
`;
|
|
13
|
+
const EXTEND_LUA = `
|
|
14
|
+
if redis.call("GET", KEYS[1]) == ARGV[1] then
|
|
15
|
+
return redis.call("PEXPIRE", KEYS[1], ARGV[2])
|
|
16
|
+
else
|
|
17
|
+
return 0
|
|
18
|
+
end
|
|
19
|
+
`;
|
|
20
|
+
class Queue {
|
|
21
|
+
constructor() {
|
|
22
|
+
this.attempts = 2;
|
|
23
|
+
this.portion = 1;
|
|
24
|
+
this.running = false;
|
|
25
|
+
}
|
|
26
|
+
wait(ms) {
|
|
27
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
28
|
+
}
|
|
29
|
+
key(...parts) {
|
|
30
|
+
return parts.join(':');
|
|
31
|
+
}
|
|
32
|
+
connection(db) {
|
|
33
|
+
return this.redisService['clients'].get(db);
|
|
34
|
+
}
|
|
35
|
+
checkConnection(db) {
|
|
36
|
+
return !!this.connection(db)
|
|
37
|
+
&& (this.connection(db).status === 'ready'
|
|
38
|
+
|| this.connection(db).status === 'connecting');
|
|
39
|
+
}
|
|
40
|
+
start(db, queue, attempt = 0) {
|
|
41
|
+
if (this.running) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
this.running = true;
|
|
45
|
+
this.loop(db, queue, attempt).catch((err) => {
|
|
46
|
+
this.running = false;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
async drop(db, pattern) {
|
|
50
|
+
if (!this.checkConnection(db)) {
|
|
51
|
+
throw new Error(`Redis connection error "${db}".`);
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
let cursor = '0', total = 0;
|
|
55
|
+
do {
|
|
56
|
+
const [next, keys] = await this.connection(db).scan(cursor, 'MATCH', pattern, 'COUNT', 500);
|
|
57
|
+
cursor = next;
|
|
58
|
+
if ((0, full_utils_1.isArrFilled)(keys)) {
|
|
59
|
+
total += keys.length;
|
|
60
|
+
for (let i = 0; i < keys.length; i += 500) {
|
|
61
|
+
const chunk = keys.slice(i, i + 500);
|
|
62
|
+
if (typeof this.connection(db).unlink === 'function') {
|
|
63
|
+
await this.connection(db).unlink(...chunk);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
await this.connection(db).del(...chunk);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
} while (cursor !== '0');
|
|
71
|
+
return total;
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
}
|
|
75
|
+
throw new Error(`Redis clear error "${db}".`);
|
|
76
|
+
}
|
|
77
|
+
async unlock(db, key, token) {
|
|
78
|
+
return Number(await this.connection(db).eval(UNLOCK_LUA, 1, key, token)) === 1;
|
|
79
|
+
}
|
|
80
|
+
async lock(db, key, opts) {
|
|
81
|
+
if (!this.checkConnection(db)) {
|
|
82
|
+
throw new Error(`Redis connection error "${db}".`);
|
|
83
|
+
}
|
|
84
|
+
const token = crypto.randomBytes(16).toString('hex');
|
|
85
|
+
const retries = Math.max(0, opts?.retries ?? 5);
|
|
86
|
+
const minDelay = Math.max(5, opts?.minDelayMs ?? 20);
|
|
87
|
+
const maxDelay = Math.max(minDelay, opts?.maxDelayMs ?? 60);
|
|
88
|
+
const ttlMs = Number(opts?.ttlMs ?? 3000);
|
|
89
|
+
const lockKey = this.key('lock', key);
|
|
90
|
+
let attempt = 0;
|
|
91
|
+
while (attempt < retries) {
|
|
92
|
+
const ok = await this.connection(db).set(lockKey, token, 'PX', ttlMs, 'NX');
|
|
93
|
+
if (ok === 'OK') {
|
|
94
|
+
return {
|
|
95
|
+
token,
|
|
96
|
+
key: lockKey,
|
|
97
|
+
ttlMs: opts.ttlMs,
|
|
98
|
+
unlock: async () => await this.unlock(db, lockKey, token),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
attempt++;
|
|
102
|
+
if (attempt < retries) {
|
|
103
|
+
await this.wait(minDelay + Math.floor(Math.random() * (maxDelay - minDelay + 1)));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
async write(db, key, value, ttlMs) {
|
|
109
|
+
if (!this.checkConnection(db)) {
|
|
110
|
+
throw new Error(`Redis connection error "${db}".`);
|
|
111
|
+
}
|
|
112
|
+
const payload = ((0, full_utils_1.isArr)(value) || (0, full_utils_1.isObj)(value))
|
|
113
|
+
? (0, full_utils_1.toJSON)(value)
|
|
114
|
+
: String(value);
|
|
115
|
+
return (0, full_utils_1.isNumP)(ttlMs)
|
|
116
|
+
? await this.connection(db).set(key, payload, 'EX', ttlMs)
|
|
117
|
+
: await this.connection(db).set(key, payload);
|
|
118
|
+
}
|
|
119
|
+
async onStart(queue, portion) {
|
|
120
|
+
}
|
|
121
|
+
async onSuccess(queue, task, result) {
|
|
122
|
+
}
|
|
123
|
+
async onError(queue, task, err) {
|
|
124
|
+
}
|
|
125
|
+
async onFatal(queue, task, err) {
|
|
126
|
+
}
|
|
127
|
+
async onEnd(queue, result) {
|
|
128
|
+
}
|
|
129
|
+
async beforeExecute(queue, task) {
|
|
130
|
+
return task;
|
|
131
|
+
}
|
|
132
|
+
async afterExecute(queue, portion, result) {
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
async callback(queue, task) {
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
async execute(queue, task) {
|
|
139
|
+
return await this.callback(queue, task);
|
|
140
|
+
}
|
|
141
|
+
async retry(db, queue, task) {
|
|
142
|
+
try {
|
|
143
|
+
const attempt = (task.opts?.attempt || 0) + 1;
|
|
144
|
+
const key = this.key('queue', queue, attempt);
|
|
145
|
+
await this.connection(db).rpush(key, (0, full_utils_1.toJSON)({
|
|
146
|
+
...task,
|
|
147
|
+
opts: {
|
|
148
|
+
...task.opts || {},
|
|
149
|
+
attempt,
|
|
150
|
+
},
|
|
151
|
+
}));
|
|
152
|
+
}
|
|
153
|
+
catch (err) {
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async select(db, queue, attempt) {
|
|
157
|
+
const arr = await this.connection(db).rpop(this.key('queue', queue, attempt), this.portion);
|
|
158
|
+
if (!(0, full_utils_1.isArrFilled)(arr)) {
|
|
159
|
+
return null;
|
|
160
|
+
}
|
|
161
|
+
return arr
|
|
162
|
+
.map((item) => {
|
|
163
|
+
try {
|
|
164
|
+
const v = (0, full_utils_1.fromJSON)(item);
|
|
165
|
+
if (!(0, full_utils_1.isObjFilled)(v)) {
|
|
166
|
+
return null;
|
|
167
|
+
}
|
|
168
|
+
return v;
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
})
|
|
174
|
+
.filter((item) => !!item);
|
|
175
|
+
}
|
|
176
|
+
async process(db, queue, task) {
|
|
177
|
+
let res;
|
|
178
|
+
try {
|
|
179
|
+
await this.onSuccess(queue, task, res = await this.execute(queue, await this.beforeExecute(queue, task)));
|
|
180
|
+
}
|
|
181
|
+
catch (err) {
|
|
182
|
+
await this.onError(queue, task, err);
|
|
183
|
+
if (task.opts?.attempt < (this.attempts - 1)) {
|
|
184
|
+
await this.retry(db, queue, task);
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
await this.onFatal(queue, task, err);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
await this.onEnd(queue, res);
|
|
191
|
+
return res;
|
|
192
|
+
}
|
|
193
|
+
async loop(db, queue, attempt) {
|
|
194
|
+
while (this.running) {
|
|
195
|
+
if (!this.checkConnection(db)) {
|
|
196
|
+
await this.wait(1000);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const tasks = await this.select(db, queue, attempt);
|
|
200
|
+
if (!(0, full_utils_1.isArrFilled)(tasks)) {
|
|
201
|
+
await this.wait(1000);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
await this.onStart(queue, tasks);
|
|
205
|
+
await this.afterExecute(queue, tasks, await Promise.all(tasks.map((task) => this.process(db, queue, task))));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
exports.Queue = Queue;
|
|
210
|
+
//# sourceMappingURL=Queue.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Queue.js","sourceRoot":"","sources":["../../src/Queue.ts"],"names":[],"mappings":";;;AAAA,iCAAiC;AACjC,2CAQoB;AAOpB,MAAM,UAAU,GAAG;;;;;;CAMlB,CAAC;AAEF,MAAM,UAAU,GAAG;;;;;;CAMlB,CAAC;AAEF,MAAa,KAAK;IAAlB;QAEiB,aAAQ,GAAW,CAAC,CAAC;QACrB,YAAO,GAAW,CAAC,CAAC;QAC7B,YAAO,GAAG,KAAK,CAAC;IA8NxB,CAAC;IA5NA,IAAI,CAAC,EAAU;QACd,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,U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AAO,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;YACtC,CAAC;QACF,CAAC;QACD,MAAM,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,CAAC,CAAC;QAC7B,OAAO,GAAG,CAAC;IACZ,CAAC;IAEO,KAAK,CAAC,IAAI,CAAC,EAAU,EAAE,KAAa,EAAE,OAAe;QAC5D,OAAO,IAAI,CAAC,OAAO,EAAE,CAAC;YACrB,IAAI,CAAC,IAAI,CAAC,eAAe,CAAC,EAAE,CAAC,EAAE,CAAC;gBAC/B,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACtB,SAAS;YACV,CAAC;YACD,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,EAAE,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC;YAEpD,IAAI,CAAC,IAAA,wBAAW,EAAC,KAAK,CAAC,EAAE,CAAC;gBACzB,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;gBACtB,SAAS;YACV,CAAC;YACD,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YACjC,MAAM,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAmB,EAAE,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7H,CAAC;IACF,CAAC;CACD;AAlOD,sBAkOC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Queue } from './Queue';
|
|
2
|
+
import { Processor } from './Processor';
|
|
3
|
+
import { TaskInterface } from './types';
|
|
4
|
+
export declare class QueueProcessor extends Queue {
|
|
5
|
+
private processors;
|
|
6
|
+
protected methods: Array<Function>;
|
|
7
|
+
setProcessors(processors: Processor[]): this;
|
|
8
|
+
getProcessors(): Processor[];
|
|
9
|
+
getProcessor(name: string): Processor | null;
|
|
10
|
+
protected execute(queue: string, task: TaskInterface): Promise<any>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueueProcessor = void 0;
|
|
4
|
+
const Queue_1 = require("./Queue");
|
|
5
|
+
class QueueProcessor extends Queue_1.Queue {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
this.processors = [];
|
|
9
|
+
this.methods = [];
|
|
10
|
+
}
|
|
11
|
+
setProcessors(processors) {
|
|
12
|
+
this.processors = [...processors];
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
getProcessors() {
|
|
16
|
+
return [...this.processors];
|
|
17
|
+
}
|
|
18
|
+
getProcessor(name) {
|
|
19
|
+
return this.getProcessors().find((processor) => processor.name === name) ?? null;
|
|
20
|
+
}
|
|
21
|
+
async execute(queue, task) {
|
|
22
|
+
const processor = this.getProcessor(task.opts?.processor);
|
|
23
|
+
if (!processor) {
|
|
24
|
+
throw new Error('Неизвестный процессор.');
|
|
25
|
+
}
|
|
26
|
+
const method = processor.method(task.opts?.method);
|
|
27
|
+
if (!method) {
|
|
28
|
+
throw new Error('Неизвестный метод.');
|
|
29
|
+
}
|
|
30
|
+
return await method.call(processor, queue, task);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
exports.QueueProcessor = QueueProcessor;
|
|
34
|
+
//# sourceMappingURL=QueueProcessor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueueProcessor.js","sourceRoot":"","sources":["../../src/QueueProcessor.ts"],"names":[],"mappings":";;;AAAA,mCAAgC;AAIhC,MAAa,cAAe,SAAQ,aAAK;IAAzC;;QACS,eAAU,GAAqB,EAAE,CAAC;QAChC,YAAO,GAAoB,EAAE,CAAC;IA4BzC,CAAC;IA1BA,aAAa,CAAC,UAAuB;QACpC,IAAI,CAAC,UAAU,GAAG,CAAE,GAAG,UAAU,CAAE,CAAC;QACpC,OAAO,IAAI,CAAC;IACb,CAAC;IAED,aAAa;QACZ,OAAO,CAAE,GAAG,IAAI,CAAC,UAAU,CAAE,CAAC;IAC/B,CAAC;IAED,YAAY,CAAC,IAAY;QACxB,OAAO,IAAI,CAAC,aAAa,EAAE,CAAC,IAAI,CAAC,CAAC,SAAoB,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,KAAK,IAAI,CAAC,IAAI,IAAI,CAAC;IAC7F,CAAC;IAES,KAAK,CAAC,OAAO,CAAC,KAAa,EAAE,IAAmB;QACzD,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAE1D,IAAI,CAAC,SAAS,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAC3C,CAAC;QACD,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;QAEnD,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACvC,CAAC;QACD,OAAO,MAAM,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;IAClD,CAAC;CACD;AA9BD,wCA8BC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Processor } from './Processor';
|
|
2
|
+
import { QueueProcessor } from './QueueProcessor';
|
|
3
|
+
import { Queue } from './Queue';
|
|
4
|
+
import { DistLockInterface, LockOptsInterface, TaskOptsInterface, TaskInterface } from './types';
|
|
5
|
+
export { Processor, QueueProcessor, Queue, DistLockInterface, LockOptsInterface, TaskOptsInterface, TaskInterface, };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Queue = exports.QueueProcessor = exports.Processor = void 0;
|
|
4
|
+
const Processor_1 = require("./Processor");
|
|
5
|
+
Object.defineProperty(exports, "Processor", { enumerable: true, get: function () { return Processor_1.Processor; } });
|
|
6
|
+
const QueueProcessor_1 = require("./QueueProcessor");
|
|
7
|
+
Object.defineProperty(exports, "QueueProcessor", { enumerable: true, get: function () { return QueueProcessor_1.QueueProcessor; } });
|
|
8
|
+
const Queue_1 = require("./Queue");
|
|
9
|
+
Object.defineProperty(exports, "Queue", { enumerable: true, get: function () { return Queue_1.Queue; } });
|
|
10
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,2CAAwC;AAWvC,0FAXQ,qBAAS,OAWR;AAVV,qDAAkD;AAWjD,+FAXQ,+BAAc,OAWR;AAVf,mCAAgC;AAW/B,sFAXQ,aAAK,OAWR"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface DistLockInterface {
|
|
2
|
+
key: string;
|
|
3
|
+
token: string;
|
|
4
|
+
ttlMs: number;
|
|
5
|
+
unlock: () => Promise<boolean>;
|
|
6
|
+
}
|
|
7
|
+
export interface LockOptsInterface {
|
|
8
|
+
ttlMs: number;
|
|
9
|
+
retries?: number;
|
|
10
|
+
minDelayMs?: number;
|
|
11
|
+
maxDelayMs?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface TaskOptsInterface {
|
|
14
|
+
attempt?: number;
|
|
15
|
+
method?: number;
|
|
16
|
+
processor?: string;
|
|
17
|
+
progress?: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface TaskInterface {
|
|
20
|
+
payload: object;
|
|
21
|
+
id?: string;
|
|
22
|
+
opts?: TaskOptsInterface;
|
|
23
|
+
enqueuedAt?: number;
|
|
24
|
+
}
|
|
25
|
+
export interface TasksInterface {
|
|
26
|
+
payloads: Array<any>;
|
|
27
|
+
id?: string;
|
|
28
|
+
opts?: TaskOptsInterface;
|
|
29
|
+
enqueuedAt?: number;
|
|
30
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|