power-queues 1.0.2 → 1.0.4
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/Processor.d.ts +4 -15
- package/dist/Processor.js +4 -51
- package/dist/Processor.js.map +1 -1
- package/dist/Queue.d.ts +28 -35
- package/dist/Queue.js +91 -140
- package/dist/Queue.js.map +1 -1
- package/dist/QueueMethod.d.ts +9 -0
- package/dist/QueueMethod.js +29 -0
- package/dist/QueueMethod.js.map +1 -0
- package/dist/RedisManager.d.ts +22 -0
- package/dist/RedisManager.js +120 -0
- package/dist/RedisManager.js.map +1 -0
- package/dist/index.d.ts +5 -6
- package/dist/index.js +7 -11
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types.d.ts +14 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +12 -6
- package/dist/QueuePortion.d.ts +0 -8
- package/dist/QueuePortion.js +0 -34
- package/dist/QueuePortion.js.map +0 -1
- package/dist/QueuePortionProcessor.d.ts +0 -8
- package/dist/QueuePortionProcessor.js +0 -75
- package/dist/QueuePortionProcessor.js.map +0 -1
- package/dist/QueuePortionProcessorMethod.d.ts +0 -12
- package/dist/QueuePortionProcessorMethod.js +0 -116
- package/dist/QueuePortionProcessorMethod.js.map +0 -1
- package/dist/QueueProcessor.d.ts +0 -14
- package/dist/QueueProcessor.js +0 -61
- package/dist/QueueProcessor.js.map +0 -1
package/dist/Processor.d.ts
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
1
|
export declare class Processor {
|
|
2
|
-
readonly
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
excecute(queueName: string, attemptIndex: number, data: any): Promise<any>;
|
|
7
|
-
success(data: any): Promise<any>;
|
|
8
|
-
successMethod(data: any): Promise<any>;
|
|
9
|
-
timestamp(date?: Date): string;
|
|
10
|
-
orderMethods(): Array<Function>;
|
|
11
|
-
errorMethods(): Array<Function>;
|
|
12
|
-
getMethods(): Array<Function>;
|
|
13
|
-
getMethodsLength(): number;
|
|
14
|
-
getMethod(index: number): Function;
|
|
15
|
-
isErrorMethod(method: Function): boolean;
|
|
16
|
-
attemptsTimeout(attemptIndex: number): number;
|
|
2
|
+
readonly name: string;
|
|
3
|
+
execute(queueName: string, attemptIndex: number, data: any): Promise<any>;
|
|
4
|
+
methods(): Array<Function>;
|
|
5
|
+
method(index: number): Function;
|
|
17
6
|
}
|
package/dist/Processor.js
CHANGED
|
@@ -2,61 +2,14 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Processor = void 0;
|
|
4
4
|
class Processor {
|
|
5
|
-
|
|
6
|
-
this.timeoutCoefficient = 3;
|
|
7
|
-
this.timeoutInitial = 3000;
|
|
8
|
-
}
|
|
9
|
-
get name() {
|
|
10
|
-
return this.defaultName ?? this.constructor.name;
|
|
11
|
-
}
|
|
12
|
-
async excecute(queueName, attemptIndex, data) {
|
|
13
|
-
return data;
|
|
14
|
-
}
|
|
15
|
-
async success(data) {
|
|
5
|
+
async execute(queueName, attemptIndex, data) {
|
|
16
6
|
return data;
|
|
17
7
|
}
|
|
18
|
-
|
|
19
|
-
return data;
|
|
20
|
-
}
|
|
21
|
-
timestamp(date = new Date()) {
|
|
22
|
-
const pad = (n) => String(n).padStart(2, '0');
|
|
23
|
-
const year = date.getFullYear();
|
|
24
|
-
const month = pad(date.getMonth() + 1);
|
|
25
|
-
const day = pad(date.getDate());
|
|
26
|
-
const hours = pad(date.getHours());
|
|
27
|
-
const minutes = pad(date.getMinutes());
|
|
28
|
-
const seconds = pad(date.getSeconds());
|
|
29
|
-
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
|
30
|
-
}
|
|
31
|
-
orderMethods() {
|
|
8
|
+
methods() {
|
|
32
9
|
return [];
|
|
33
10
|
}
|
|
34
|
-
|
|
35
|
-
return [];
|
|
36
|
-
}
|
|
37
|
-
getMethods() {
|
|
38
|
-
return [...this.orderMethods()];
|
|
39
|
-
}
|
|
40
|
-
getMethodsLength() {
|
|
41
|
-
return this.orderMethods().length;
|
|
42
|
-
}
|
|
43
|
-
getMethod(index) {
|
|
44
|
-
return this.orderMethods()[index];
|
|
45
|
-
}
|
|
46
|
-
isErrorMethod(method) {
|
|
47
|
-
return !!this.errorMethods().find((item) => item.name === method.name);
|
|
48
|
-
}
|
|
49
|
-
attemptsTimeout(attemptIndex) {
|
|
50
|
-
const coefficient = this.timeoutCoefficient;
|
|
51
|
-
let i = 0, timeout = this.timeoutInitial;
|
|
52
|
-
while (i < attemptIndex) {
|
|
53
|
-
timeout += (timeout * coefficient);
|
|
54
|
-
i++;
|
|
55
|
-
}
|
|
56
|
-
if (timeout === 0) {
|
|
57
|
-
timeout = this.timeoutInitial;
|
|
58
|
-
}
|
|
59
|
-
return timeout;
|
|
11
|
+
method(index) {
|
|
12
|
+
return this.methods()[index];
|
|
60
13
|
}
|
|
61
14
|
}
|
|
62
15
|
exports.Processor = Processor;
|
package/dist/Processor.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Processor.js","sourceRoot":"","sources":["../src/Processor.ts"],"names":[],"mappings":";;;AACA,MAAa,SAAS;
|
|
1
|
+
{"version":3,"file":"Processor.js","sourceRoot":"","sources":["../src/Processor.ts"],"names":[],"mappings":";;;AACA,MAAa,SAAS;IAGrB,KAAK,CAAC,OAAO,CAAC,SAAiB,EAAE,YAAoB,EAAE,IAAS;QAC/D,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"}
|
package/dist/Queue.d.ts
CHANGED
|
@@ -1,37 +1,30 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { RedisManager } from './RedisManager';
|
|
2
|
+
import { Processor } from './Processor';
|
|
3
|
+
import { TaskInterface } from './types';
|
|
2
4
|
export declare class Queue {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
protected
|
|
10
|
-
protected
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
errorWrapper(queueName: string, attemptIndex: number, data: any, err: any): Promise<void>;
|
|
29
|
-
errorMessage(queueName: string, attemptIndex: number, data: any, err: any): Promise<void>;
|
|
30
|
-
error(queueName: string, attemptIndex: number, data: any, err: any): Promise<void>;
|
|
31
|
-
dropKeys(pattern: string, opts?: {
|
|
32
|
-
count?: number;
|
|
33
|
-
batch?: number;
|
|
34
|
-
pauseMs?: number;
|
|
35
|
-
useUnlink?: boolean;
|
|
36
|
-
}): Promise<number>;
|
|
5
|
+
readonly redisManager: RedisManager;
|
|
6
|
+
readonly db: string;
|
|
7
|
+
readonly attempts: number;
|
|
8
|
+
readonly portionSize: number;
|
|
9
|
+
running: boolean;
|
|
10
|
+
private processors;
|
|
11
|
+
protected execute(queue: string, task: TaskInterface): Promise<any>;
|
|
12
|
+
protected beforeExecute(queue: string, task: TaskInterface): Promise<any>;
|
|
13
|
+
protected afterExecute(queue: string, task: TaskInterface, result: any): Promise<any>;
|
|
14
|
+
protected onSuccess(queue: string, task: TaskInterface, result: any): Promise<void>;
|
|
15
|
+
protected onError(queue: string, task: TaskInterface, err: any): Promise<void>;
|
|
16
|
+
protected onFatal(queue: string, task: TaskInterface, err: any): Promise<void>;
|
|
17
|
+
protected onStart(queue: string, task: TaskInterface): Promise<void>;
|
|
18
|
+
protected onEnd(queue: string, result: any): Promise<void>;
|
|
19
|
+
private process;
|
|
20
|
+
private loop;
|
|
21
|
+
protected retry(queue: string, task: TaskInterface): Promise<void>;
|
|
22
|
+
setProcessors(processors: Processor[]): this;
|
|
23
|
+
getProcessors(): Processor[];
|
|
24
|
+
getProcessor(name: string): Processor | null;
|
|
25
|
+
start(queue: string, attempt: number): void;
|
|
26
|
+
stop(): void;
|
|
27
|
+
key(name: string, attempt: number): string;
|
|
28
|
+
select(db: string, queue: string, attempt: number): Promise<TaskInterface[] | null>;
|
|
29
|
+
push(db: string, queue: string, task: TaskInterface): Promise<void>;
|
|
37
30
|
}
|
package/dist/Queue.js
CHANGED
|
@@ -1,172 +1,123 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Queue = void 0;
|
|
4
|
-
const
|
|
4
|
+
const full_utils_1 = require("full-utils");
|
|
5
5
|
class Queue {
|
|
6
6
|
constructor() {
|
|
7
|
-
this.
|
|
8
|
-
this.
|
|
9
|
-
this.
|
|
10
|
-
this.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const pad = (n) => String(n).padStart(2, '0');
|
|
17
|
-
const year = date.getFullYear();
|
|
18
|
-
const month = pad(date.getMonth() + 1);
|
|
19
|
-
const day = pad(date.getDate());
|
|
20
|
-
const hours = pad(date.getHours());
|
|
21
|
-
const minutes = pad(date.getMinutes());
|
|
22
|
-
const seconds = pad(date.getSeconds());
|
|
23
|
-
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
|
24
|
-
}
|
|
25
|
-
queueKey(queueName, attemptIndex) {
|
|
26
|
-
return `queue.${queueName}.${attemptIndex}`;
|
|
27
|
-
}
|
|
28
|
-
readyKey(queueName, attemptIndex) {
|
|
29
|
-
return `ready.${queueName}.${attemptIndex}`;
|
|
30
|
-
}
|
|
31
|
-
start(queueName) {
|
|
32
|
-
this.listen(queueName);
|
|
33
|
-
}
|
|
34
|
-
listen(queueName) {
|
|
35
|
-
let i = 0;
|
|
36
|
-
while (i < this.attempts) {
|
|
37
|
-
this.attempt(queueName, i);
|
|
38
|
-
i++;
|
|
7
|
+
this.attempts = 3;
|
|
8
|
+
this.portionSize = 1;
|
|
9
|
+
this.running = false;
|
|
10
|
+
this.processors = [];
|
|
11
|
+
}
|
|
12
|
+
async execute(queue, task) {
|
|
13
|
+
const processor = this.getProcessor(task.processor);
|
|
14
|
+
if (!processor) {
|
|
15
|
+
throw new Error('Undefined processor.');
|
|
39
16
|
}
|
|
17
|
+
return await processor.execute.call(processor, task);
|
|
40
18
|
}
|
|
41
|
-
async
|
|
42
|
-
|
|
19
|
+
async beforeExecute(queue, task) {
|
|
20
|
+
return task;
|
|
43
21
|
}
|
|
44
|
-
async
|
|
45
|
-
|
|
46
|
-
const threadId = this.threadId;
|
|
47
|
-
await this.redis.rpush(readyKey, this.threadId);
|
|
48
|
-
await this.wait();
|
|
49
|
-
setImmediate(() => this.process(queueName, attemptIndex));
|
|
50
|
-
return;
|
|
22
|
+
async afterExecute(queue, task, result) {
|
|
23
|
+
return result;
|
|
51
24
|
}
|
|
52
|
-
async
|
|
25
|
+
async onSuccess(queue, task, result) {
|
|
26
|
+
}
|
|
27
|
+
async onError(queue, task, err) {
|
|
28
|
+
}
|
|
29
|
+
async onFatal(queue, task, err) {
|
|
30
|
+
}
|
|
31
|
+
async onStart(queue, task) {
|
|
32
|
+
}
|
|
33
|
+
async onEnd(queue, result) {
|
|
34
|
+
}
|
|
35
|
+
async process(queue, task) {
|
|
36
|
+
let res;
|
|
53
37
|
try {
|
|
54
|
-
await this.
|
|
38
|
+
await this.onStart(queue, task);
|
|
39
|
+
await this.onSuccess(queue, task, res = await this.afterExecute(queue, task, await this.execute(queue, await this.beforeExecute(queue, task))));
|
|
55
40
|
}
|
|
56
41
|
catch (err) {
|
|
57
|
-
await this.
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
async processOne(queueName, attemptIndex) {
|
|
64
|
-
const readyKey = this.readyKey(queueName, attemptIndex);
|
|
65
|
-
const readyThreadId = await this.redis.lpop(readyKey);
|
|
66
|
-
if (readyThreadId) {
|
|
67
|
-
if (readyThreadId === this.threadId) {
|
|
68
|
-
const data = await this.select(queueName, attemptIndex);
|
|
69
|
-
const allow = await this.allow(queueName, attemptIndex, data);
|
|
70
|
-
if (allow) {
|
|
71
|
-
try {
|
|
72
|
-
await this.excecuteWrapper(queueName, attemptIndex, data);
|
|
73
|
-
}
|
|
74
|
-
catch (err) {
|
|
75
|
-
this.retry(queueName, attemptIndex, data, err);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
42
|
+
await this.onError(queue, task, err);
|
|
43
|
+
if (task.attempt < this.attempts) {
|
|
44
|
+
await this.retry(queue, task);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
await this.onFatal(queue, task, err);
|
|
78
48
|
}
|
|
79
|
-
await this.redis.rpush(readyKey, this.threadId);
|
|
80
49
|
}
|
|
50
|
+
await this.onEnd(queue, res);
|
|
81
51
|
}
|
|
82
|
-
async
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
if (attemptIndex < (this.attempts - 1)) {
|
|
88
|
-
return await this.redis.rpush(queueKey, dataProcessed);
|
|
89
|
-
}
|
|
52
|
+
async loop(queue, attempt) {
|
|
53
|
+
while (this.running) {
|
|
54
|
+
if (!this.redisManager.checkConnection(this.db)) {
|
|
55
|
+
await this.redisManager.wait(1000);
|
|
56
|
+
continue;
|
|
90
57
|
}
|
|
91
|
-
await this.
|
|
92
|
-
|
|
93
|
-
|
|
58
|
+
const tasks = await this.select(this.db, queue, attempt);
|
|
59
|
+
if (!(0, full_utils_1.isArrFilled)(tasks)) {
|
|
60
|
+
await this.redisManager.wait(1000);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
await Promise.all(tasks.map((task) => this.process(queue, task)));
|
|
94
64
|
}
|
|
95
|
-
return 0;
|
|
96
65
|
}
|
|
97
|
-
async
|
|
98
|
-
|
|
99
|
-
const data = await this.redis.lpop(queueKey);
|
|
100
|
-
const output = await this.selectAfter(queueName, data);
|
|
101
|
-
return output;
|
|
66
|
+
async retry(queue, task) {
|
|
67
|
+
await this.push(this.db, queue, { ...task, attempt: (task.attempt || 1) + 1 });
|
|
102
68
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
return parsed;
|
|
107
|
-
}
|
|
108
|
-
catch (err) {
|
|
109
|
-
}
|
|
110
|
-
return null;
|
|
69
|
+
setProcessors(processors) {
|
|
70
|
+
this.processors = [...processors];
|
|
71
|
+
return this;
|
|
111
72
|
}
|
|
112
|
-
|
|
113
|
-
return
|
|
73
|
+
getProcessors() {
|
|
74
|
+
return [...this.processors];
|
|
114
75
|
}
|
|
115
|
-
|
|
116
|
-
|
|
76
|
+
getProcessor(name) {
|
|
77
|
+
return this.getProcessors().find((processor) => processor.name === name) ?? null;
|
|
117
78
|
}
|
|
118
|
-
|
|
119
|
-
|
|
79
|
+
start(queue, attempt) {
|
|
80
|
+
if (this.running) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
this.running = true;
|
|
84
|
+
this.loop(queue, attempt).catch((err) => {
|
|
85
|
+
this.running = false;
|
|
86
|
+
});
|
|
120
87
|
}
|
|
121
|
-
|
|
122
|
-
|
|
88
|
+
stop() {
|
|
89
|
+
this.running = false;
|
|
123
90
|
}
|
|
124
|
-
|
|
125
|
-
return
|
|
91
|
+
key(name, attempt) {
|
|
92
|
+
return this.redisManager.key('queue', `${name}:${attempt}`);
|
|
126
93
|
}
|
|
127
|
-
async
|
|
128
|
-
|
|
129
|
-
|
|
94
|
+
async select(db, queue, attempt) {
|
|
95
|
+
const arr = await this.redisManager.connection(db).rpop(this.key(queue, attempt), this.portionSize);
|
|
96
|
+
if (!(0, full_utils_1.isArrFilled)(arr)) {
|
|
97
|
+
return null;
|
|
130
98
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
console.log(`\n-------------------------------------`);
|
|
138
|
-
console.error(`[ERR]`, this.timestamp());
|
|
139
|
-
console.error(` `, `Очередь:`, queueName);
|
|
140
|
-
console.error(` `, `Попытка:`, attemptIndex);
|
|
141
|
-
console.error(` `, `Результат:`, typeof data);
|
|
142
|
-
console.error(` `, `Сообщение:`, err.message);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
async error(queueName, attemptIndex, data, err) {
|
|
146
|
-
}
|
|
147
|
-
async dropKeys(pattern, opts) {
|
|
148
|
-
const count = opts?.count ?? 1000;
|
|
149
|
-
const batch = opts?.batch ?? 5000;
|
|
150
|
-
const pauseMs = opts?.pauseMs ?? 0;
|
|
151
|
-
const cmd = opts?.useUnlink ?? true ? `unlink` : `del`;
|
|
152
|
-
const stream = this.redis.scanStream({ match: pattern, count });
|
|
153
|
-
let buffer = [], deleted = 0;
|
|
154
|
-
for await (const keys of stream) {
|
|
155
|
-
buffer.push(...keys);
|
|
156
|
-
while (buffer.length >= batch) {
|
|
157
|
-
const chunk = buffer.splice(0, batch);
|
|
158
|
-
const n = await this.redis[cmd](...chunk);
|
|
159
|
-
deleted += Number(n) || 0;
|
|
160
|
-
if (pauseMs) {
|
|
161
|
-
await new Promise((r) => setTimeout(r, pauseMs));
|
|
99
|
+
return arr
|
|
100
|
+
.map((item) => {
|
|
101
|
+
try {
|
|
102
|
+
const v = (0, full_utils_1.fromJSON)(item);
|
|
103
|
+
if (!(0, full_utils_1.isObjFilled)(v)) {
|
|
104
|
+
return null;
|
|
162
105
|
}
|
|
106
|
+
return v;
|
|
163
107
|
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
}
|
|
110
|
+
return null;
|
|
111
|
+
})
|
|
112
|
+
.filter((item) => !!item);
|
|
113
|
+
}
|
|
114
|
+
async push(db, queue, task) {
|
|
115
|
+
const attempt = task.attempt || 1;
|
|
116
|
+
const payload = (0, full_utils_1.toJSON)({ ...task, attempt, enqueuedAt: Date.now(), });
|
|
117
|
+
if (!(0, full_utils_1.isStrFilled)(payload)) {
|
|
118
|
+
throw new Error('Empty payload.');
|
|
164
119
|
}
|
|
165
|
-
|
|
166
|
-
const n = await this.redis[cmd](...buffer);
|
|
167
|
-
deleted += Number(n) || 0;
|
|
168
|
-
}
|
|
169
|
-
return deleted;
|
|
120
|
+
await this.redisManager.connection(db).rpush(this.key(queue, attempt), payload);
|
|
170
121
|
}
|
|
171
122
|
}
|
|
172
123
|
exports.Queue = Queue;
|
package/dist/Queue.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Queue.js","sourceRoot":"","sources":["../src/Queue.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"Queue.js","sourceRoot":"","sources":["../src/Queue.ts"],"names":[],"mappings":";;;AAAA,2CAMoB;AAKpB,MAAa,KAAK;IAAlB;QAGiB,aAAQ,GAAW,CAAC,CAAC;QACrB,gBAAW,GAAW,CAAC,CAAC;QACjC,YAAO,GAAG,KAAK,CAAC;QACf,eAAU,GAAqB,EAAE,CAAC;IAyI3C,CAAC;IAvIU,KAAK,CAAC,OAAO,CAAC,KAAa,EAAE,IAAmB;QACzD,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAEpD,IAAI,CAAC,SAAS,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QACzC,CAAC;QACD,OAAO,MAAM,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACtD,CAAC;IAES,KAAK,CAAC,aAAa,CAAC,KAAa,EAAE,IAAmB;QAC/D,OAAO,IAAI,CAAC;IACb,CAAC;IAES,KAAK,CAAC,YAAY,CAAC,KAAa,EAAE,IAAmB,EAAE,MAAW;QAC3E,OA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;YACD,OAAO,GAAG,EAAE,CAAC;YACb,CAAC;YACD,OAAO,IAAI,CAAC;QACb,CAAC,CAAC;aACD,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;IAC5B,CAAC;IAED,KAAK,CAAC,IAAI,CAAC,EAAU,EAAE,KAAa,EAAE,IAAmB;QACxD,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,IAAI,CAAC,CAAC;QAClC,MAAM,OAAO,GAAG,IAAA,mBAAM,EAAC,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAEtE,IAAI,CAAC,IAAA,wBAAW,EAAC,OAAO,CAAC,EAAE,CAAC;YAC3B,MAAM,IAAI,KAAK,CAAC,gBAAgB,CAAC,CAAC;QACnC,CAAC;QACD,MAAM,IAAI,CAAC,YAAY,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,CAAC,CAAC;IACjF,CAAC;CACD;AA/ID,sBA+IC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RedisManager } from './RedisManager';
|
|
2
|
+
import { Queue } from './Queue';
|
|
3
|
+
import { TaskInterface } from './types';
|
|
4
|
+
export declare class QueueMethod extends Queue {
|
|
5
|
+
readonly redisManager: RedisManager;
|
|
6
|
+
readonly db: string;
|
|
7
|
+
protected execute(queue: string, task: TaskInterface): Promise<any>;
|
|
8
|
+
protected onSuccess(queue: string, task: TaskInterface, result: any): Promise<void>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueueMethod = void 0;
|
|
4
|
+
const full_utils_1 = require("full-utils");
|
|
5
|
+
const Queue_1 = require("./Queue");
|
|
6
|
+
class QueueMethod extends Queue_1.Queue {
|
|
7
|
+
async execute(queue, task) {
|
|
8
|
+
const processor = this.getProcessor(task.processor);
|
|
9
|
+
if (!processor) {
|
|
10
|
+
throw new Error('Неизвестный процессор.');
|
|
11
|
+
}
|
|
12
|
+
const method = processor.method(task.method);
|
|
13
|
+
if (!(0, full_utils_1.isFunc)(method)) {
|
|
14
|
+
throw new Error('Неизвестный метод.');
|
|
15
|
+
}
|
|
16
|
+
return await method.call(processor, task);
|
|
17
|
+
}
|
|
18
|
+
async onSuccess(queue, task, result) {
|
|
19
|
+
const processor = this.getProcessor(task.processor);
|
|
20
|
+
if (!processor) {
|
|
21
|
+
throw new Error('Неизвестный процессор.');
|
|
22
|
+
}
|
|
23
|
+
if ((processor.methods().length - 1) > task.method) {
|
|
24
|
+
await this.push(this.db, queue, { ...task, method: (task.method || 0) + 1 });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.QueueMethod = QueueMethod;
|
|
29
|
+
//# sourceMappingURL=QueueMethod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"QueueMethod.js","sourceRoot":"","sources":["../src/QueueMethod.ts"],"names":[],"mappings":";;;AAAA,2CAIoB;AAEpB,mCAAgC;AAGhC,MAAa,WAAY,SAAQ,aAAK;IAI3B,KAAK,CAAC,OAAO,CAAC,KAAa,EAAE,IAAmB;QACzD,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAEpD,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,MAAM,CAAC,CAAC;QAE7C,IAAI,CAAC,IAAA,mBAAM,EAAC,MAAM,CAAC,EAAE,CAAC;YACrB,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;QACvC,CAAC;QACD,OAAO,MAAM,MAAM,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IAC3C,CAAC;IAES,KAAK,CAAC,SAAS,CAAC,KAAa,EAAE,IAAmB,EAAE,MAAW;QACxE,MAAM,SAAS,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QAEpD,IAAI,CAAC,SAAS,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC;QAC3C,CAAC;QACD,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;YACpD,MAAM,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,IAAI,EAAE,MAAM,EAAE,CAAC,IAAI,CAAC,MAAM,IAAI,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAC9E,CAAC;IACF,CAAC;CACD;AA5BD,kCA4BC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import Redis from 'ioredis';
|
|
2
|
+
import { RedisService } from '@nestjs-labs/nestjs-ioredis';
|
|
3
|
+
import { DistLockInterface } from './types';
|
|
4
|
+
export declare class RedisManager {
|
|
5
|
+
private readonly redis;
|
|
6
|
+
private readonly logger;
|
|
7
|
+
private readonly connections;
|
|
8
|
+
constructor(redis: RedisService);
|
|
9
|
+
timestamp(value?: number): string;
|
|
10
|
+
wait(ms: number): Promise<unknown>;
|
|
11
|
+
key(path: string, name: string): string;
|
|
12
|
+
connection(db: string): Redis | undefined | null;
|
|
13
|
+
checkConnection(db: string): boolean;
|
|
14
|
+
dropKeys(db: string, pattern: string): Promise<number>;
|
|
15
|
+
acquireLock(db: string, logicalKey: string, ttlMs?: number, opts?: {
|
|
16
|
+
retries?: number;
|
|
17
|
+
minDelayMs?: number;
|
|
18
|
+
maxDelayMs?: number;
|
|
19
|
+
}): Promise<DistLockInterface | null>;
|
|
20
|
+
private readonly UNLOCK_LUA;
|
|
21
|
+
private readonly EXTEND_LUA;
|
|
22
|
+
}
|