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
|
@@ -0,0 +1,120 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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,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AE/E,OAAO,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;gBAC1B,CAAC,CAAC;gBACF,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;YACvD,CAAC;YACD,OAAO,EAAE,CAAC;YAEV,IAAI,OAAO,GAAG,OAAO,EAAE,CAAC;gBACvB,MAAM,KAAK,GAAG,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,QAAQ,GAAG,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/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { QueuePortionProcessor } from './QueuePortionProcessor';
|
|
3
|
-
import { QueuePortion } from './QueuePortion';
|
|
4
|
-
import { QueueProcessor } from './QueueProcessor';
|
|
5
|
-
import { Queue } from './Queue';
|
|
1
|
+
import { TaskInterface } from './types';
|
|
6
2
|
import { Processor } from './Processor';
|
|
7
|
-
|
|
3
|
+
import { Queue } from './Queue';
|
|
4
|
+
import { QueueMethod } from './QueueMethod';
|
|
5
|
+
import { RedisManager } from './RedisManager';
|
|
6
|
+
export { TaskInterface, Processor, Queue, QueueMethod, RedisManager, };
|
package/dist/index.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const QueuePortionProcessorMethod_1 = require("./QueuePortionProcessorMethod");
|
|
5
|
-
Object.defineProperty(exports, "QueuePortionProcessorMethod", { enumerable: true, get: function () { return QueuePortionProcessorMethod_1.QueuePortionProcessorMethod; } });
|
|
6
|
-
const QueuePortionProcessor_1 = require("./QueuePortionProcessor");
|
|
7
|
-
Object.defineProperty(exports, "QueuePortionProcessor", { enumerable: true, get: function () { return QueuePortionProcessor_1.QueuePortionProcessor; } });
|
|
8
|
-
const QueuePortion_1 = require("./QueuePortion");
|
|
9
|
-
Object.defineProperty(exports, "QueuePortion", { enumerable: true, get: function () { return QueuePortion_1.QueuePortion; } });
|
|
10
|
-
const QueueProcessor_1 = require("./QueueProcessor");
|
|
11
|
-
Object.defineProperty(exports, "QueueProcessor", { enumerable: true, get: function () { return QueueProcessor_1.QueueProcessor; } });
|
|
12
|
-
const Queue_1 = require("./Queue");
|
|
13
|
-
Object.defineProperty(exports, "Queue", { enumerable: true, get: function () { return Queue_1.Queue; } });
|
|
3
|
+
exports.RedisManager = exports.QueueMethod = exports.Queue = exports.Processor = void 0;
|
|
14
4
|
const Processor_1 = require("./Processor");
|
|
15
5
|
Object.defineProperty(exports, "Processor", { enumerable: true, get: function () { return Processor_1.Processor; } });
|
|
6
|
+
const Queue_1 = require("./Queue");
|
|
7
|
+
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; } });
|
|
16
12
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AACA,2CAAwC;AAOvC,0FAPQ,qBAAS,OAOR;AANV,mCAAgC;AAO/B,sFAPQ,aAAK,OAOR;AANN,+CAA4C;AAO3C,4FAPQ,yBAAW,OAOR;AANZ,iDAA8C;AAO7C,6FAPQ,2BAAY,OAOR"}
|