power-queues 1.0.5 → 1.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "power-queues",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Base classes for implementing custom queues in redis under high load conditions based on nestjs.",
5
5
  "author": "ihor-bielchenko",
6
6
  "license": "MIT",
@@ -27,11 +27,7 @@
27
27
  "order"
28
28
  ],
29
29
  "dependencies": {
30
- "@nestjs-labs/nestjs-ioredis": "^11.0.4",
31
- "@nestjs/common": "^11.1.6",
32
30
  "crypto": "^1.0.1",
33
- "date-fns": "^4.1.0",
34
- "full-utils": "^1.0.1",
35
- "ioredis": "^5.7.0"
31
+ "full-utils": "^1.0.1"
36
32
  }
37
33
  }
@@ -1 +0,0 @@
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 DELETED
@@ -1,30 +0,0 @@
1
- import { RedisManager } from './RedisManager';
2
- import { Processor } from './Processor';
3
- import { TaskInterface } from './types';
4
- export declare class Queue {
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>;
30
- }
package/dist/Queue.js DELETED
@@ -1,124 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Queue = void 0;
4
- const full_utils_1 = require("full-utils");
5
- class Queue {
6
- constructor() {
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.');
16
- }
17
- return await processor.execute.call(processor, task);
18
- }
19
- async beforeExecute(queue, task) {
20
- return task;
21
- }
22
- async afterExecute(queue, task, result) {
23
- return result;
24
- }
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;
37
- try {
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))));
40
- }
41
- catch (err) {
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);
48
- }
49
- }
50
- await this.onEnd(queue, res);
51
- }
52
- async loop(queue, attempt) {
53
- while (this.running) {
54
- if (!this.redisManager.checkConnection(this.db)) {
55
- await this.redisManager.wait(1000);
56
- continue;
57
- }
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)));
64
- }
65
- }
66
- async retry(queue, task) {
67
- await this.push(this.db, queue, { ...task, attempt: (task.attempt || 1) + 1 });
68
- }
69
- setProcessors(processors) {
70
- this.processors = [...processors];
71
- return this;
72
- }
73
- getProcessors() {
74
- return [...this.processors];
75
- }
76
- getProcessor(name) {
77
- return this.getProcessors().find((processor) => processor.name === name) ?? null;
78
- }
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
- });
87
- }
88
- stop() {
89
- this.running = false;
90
- }
91
- key(name, attempt) {
92
- return this.redisManager.key('queue', `${name}:${attempt}`);
93
- }
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;
98
- }
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;
105
- }
106
- return v;
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.');
119
- }
120
- await this.redisManager.connection(db).rpush(this.key(queue, attempt), payload);
121
- }
122
- }
123
- exports.Queue = Queue;
124
- //# sourceMappingUR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AAC,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"}
@@ -1,9 +0,0 @@
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
- }
@@ -1,29 +0,0 @@
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
@@ -1 +0,0 @@
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"}
@@ -1,22 +0,0 @@
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
- }
@@ -1,120 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __metadata = (this && this.__metadata) || function (k, v) {
9
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.RedisManager = void 0;
13
- const crypto = require("crypto");
14
- const nestjs_ioredis_1 = require("@nestjs-labs/nestjs-ioredis");
15
- const common_1 = require("@nestjs/common");
16
- const date_fns_1 = require("date-fns");
17
- let RedisManager = class RedisManager {
18
- constructor(redis) {
19
- this.redis = redis;
20
- this.logger = new common_1.Logger('RedisManager');
21
- this.connections = {};
22
- this.UNLOCK_LUA = `
23
- if redis.call("GET", KEYS[1]) == ARGV[1] then
24
- return redis.call("DEL", KEYS[1])
25
- else
26
- return 0
27
- end
28
- `;
29
- this.EXTEND_LUA = `
30
- if redis.call("GET", KEYS[1]) == ARGV[1] then
31
- return redis.call("PEXPIRE", KEYS[1], ARGV[2])
32
- else
33
- return 0
34
- end
35
- `;
36
- }
37
- timestamp(value) {
38
- return (0, date_fns_1.format)(new Date(value ?? Date.now()), 'yyyy-MM-dd HH');
39
- }
40
- wait(ms) {
41
- return new Promise((r) => setTimeout(r, ms));
42
- }
43
- key(path, name) {
44
- return `${path}:${name}`;
45
- }
46
- connection(db) {
47
- return this.redis['clients'].get(db);
48
- }
49
- checkConnection(db) {
50
- return !!this.connection(db)
51
- && (this.connection(db).status === 'ready'
52
- || this.connection(db).status === 'connecting');
53
- }
54
- async dropKeys(db, pattern) {
55
- if (!this.checkConnection(db)) {
56
- throw new Error(`Redis connection error "${db}".`);
57
- }
58
- try {
59
- let cursor = '0', total = 0;
60
- do {
61
- const [next, keys] = await this.connection(db).scan(cursor, 'MATCH', pattern, 'COUNT', 500);
62
- cursor = next;
63
- if (keys.length) {
64
- total += keys.length;
65
- for (let i = 0; i < keys.length; i += 500) {
66
- const chunk = keys.slice(i, i + 500);
67
- if (typeof this.connection(db).unlink === 'function') {
68
- await this.connection(db).unlink(...chunk);
69
- }
70
- else {
71
- await this.connection(db).del(...chunk);
72
- }
73
- }
74
- }
75
- } while (cursor !== '0');
76
- return total;
77
- }
78
- catch (err) {
79
- }
80
- throw new Error(`Redis clear error "${db}".`);
81
- }
82
- async acquireLock(db, logicalKey, ttlMs = 3000, opts) {
83
- if (!this.checkConnection(db)) {
84
- throw new Error(`Redis connection error "${db}".`);
85
- }
86
- const lockKey = `lock:${logicalKey}`;
87
- const token = crypto.randomBytes(16).toString('hex');
88
- const retries = Math.max(0, opts?.retries ?? 5);
89
- const minDelay = Math.max(5, opts?.minDelayMs ?? 20);
90
- const maxDelay = Math.max(minDelay, opts?.maxDelayMs ?? 60);
91
- let attempt = 0;
92
- while (attempt < retries) {
93
- const ok = await this.connection(db).set(lockKey, token, 'PX', ttlMs, 'NX');
94
- if (ok === 'OK') {
95
- const extend = async (newTtlMs) => {
96
- const ms = Math.max(1, newTtlMs ?? ttlMs);
97
- const res = await this.connection(db).eval(this.EXTEND_LUA, 1, lockKey, token, ms);
98
- return Number(res) === 1;
99
- };
100
- const unlock = async () => {
101
- const res = await this.connection(db).eval(this.UNLOCK_LUA, 1, lockKey, token);
102
- return Number(res) === 1;
103
- };
104
- return { key: lockKey, token, ttlMs, extend, unlock };
105
- }
106
- attempt++;
107
- if (attempt < retries) {
108
- const sleep = minDelay + Math.floor(Math.random() * (maxDelay - minDelay + 1));
109
- await new Promise((retry) => setTimeout(retry, sleep));
110
- }
111
- }
112
- return null;
113
- }
114
- };
115
- exports.RedisManager = RedisManager;
116
- exports.RedisManager = RedisManager = __decorate([
117
- (0, common_1.Injectable)(),
118
- __metadata("design:paramtypes", [nestjs_ioredis_1.RedisService])
119
- ], RedisManager);
120
- //# sourceMappingURL=RedisManager.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"RedisManager.js","sourceRoot":"","sources":["../src/RedisManager.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,iCAAiC;AAEjC,gEAA2D;AAC3D,2CAGwB;AACxB,uCAAkC;AAQ3B,IAAM,YAAY,GAAlB,MAAM,YAAY;IAIxB,YACkB,KAAmB;QAAnB,UAAK,GAAL,KAAK,CAAc;QAJpB,WAAM,GAAW,IAAI,eAAM,CAAC,cAAc,CAAC,CAAC;QAC5C,gBAAW,GAAQ,EAAE,CAAC;QAwGtB,eAAU,GAAG;;;;;;EAM7B,CAAC;QAEe,eAAU,GAAG;;;;;;EAM7B,CAAC;IAjHF,CAAC;IAED,SAAS,CAAC,KAAc;QACvB,OAAO,IAAA,iBAAM,EAAC,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,eAAe,CAAC,CAAC;IAC/D,CAAC;IAED,IAAI,CAAC,EAAU;QACd,OAAO,IAAI,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CA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QAAQ,GAAG,CAAC,CAAC,CAAC,CAAC;gBAE/E,MAAM,IAAI,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC;YACxD,CAAC;QACF,CAAC;QACD,OAAO,IAAI,CAAC;IACb,CAAC;CAiBD,CAAA;AAzHY,oCAAY;uBAAZ,YAAY;IADxB,IAAA,mBAAU,GAAE;qCAMa,6BAAY;GALzB,YAAY,CAyHxB"}
package/dist/types.d.ts DELETED
@@ -1,14 +0,0 @@
1
- export interface DistLockInterface {
2
- key: string;
3
- token: string;
4
- ttlMs: number;
5
- extend: (newTtlMs?: number) => Promise<boolean>;
6
- unlock: () => Promise<boolean>;
7
- }
8
- export interface TaskInterface {
9
- payload: object;
10
- attempt?: number;
11
- processor?: string;
12
- method?: number;
13
- enqueuedAt?: number;
14
- }
package/dist/types.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
File without changes