redis-smq-common 1.0.0-rc.2 → 1.0.0-rc.3
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/CHANGELOG.md +5 -0
- package/README.md +6 -5
- package/dist/src/worker/worker-runner/worker-runner.d.ts +5 -8
- package/dist/src/worker/worker-runner/worker-runner.js +6 -56
- package/dist/src/worker/worker.d.ts +3 -6
- package/dist/src/worker/worker.js +1 -4
- package/dist/types/index.d.ts +0 -8
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
<div align="center" style="text-align: center">
|
|
2
2
|
<p><a href="https://github.com/weyoss/redis-smq-common"><img alt="RedisSMQ" src="./logo.png" /></a></p>
|
|
3
3
|
<p>A simple high-performance Redis message queue for Node.js.</p>
|
|
4
|
-
<p>
|
|
5
|
-
<a href="https://github.com/weyoss/redis-smq-common/actions/workflows/tests.yml"><img src="https://github.com/weyoss/redis-smq-common/actions/workflows/tests.yml/badge.svg" alt="Tests" style="max-width:100%;" /></a>
|
|
6
|
-
<a href="https://codecov.io/github/weyoss/redis-smq-common?branch=master" rel="nofollow"><img src="https://img.shields.io/codecov/c/github/weyoss/redis-smq-common" alt="Coverage Status" /></a>
|
|
7
|
-
<a href="https://lgtm.com/projects/g/weyoss/redis-smq-common/context:javascript" rel="nofollow"><img src="https://img.shields.io/lgtm/grade/javascript/github/weyoss/redis-smq-common.svg?logo=lgtm&logoWidth=18" alt="Code quality" /></a>
|
|
8
|
-
</p>
|
|
9
4
|
</div>
|
|
10
5
|
|
|
11
6
|
## RedisSMQ Common Library
|
|
12
7
|
|
|
8
|
+
<p>
|
|
9
|
+
<a href="https://github.com/weyoss/redis-smq-common/actions/workflows/tests.yml"><img src="https://github.com/weyoss/redis-smq-common/actions/workflows/tests.yml/badge.svg" alt="Tests" style="max-width:100%;" /></a>
|
|
10
|
+
<a href="https://codecov.io/github/weyoss/redis-smq-common?branch=master" rel="nofollow"><img src="https://img.shields.io/codecov/c/github/weyoss/redis-smq-common" alt="Coverage Status" /></a>
|
|
11
|
+
<a href="https://lgtm.com/projects/g/weyoss/redis-smq-common/context:javascript" rel="nofollow"><img src="https://img.shields.io/lgtm/grade/javascript/github/weyoss/redis-smq-common.svg?logo=lgtm&logoWidth=18" alt="Code quality" /></a>
|
|
12
|
+
</p>
|
|
13
|
+
|
|
13
14
|
[RedisSMQ](https://github.com/weyoss/redis-smq) shared components that may be used by integrated applications and extensions.
|
|
14
15
|
|
|
15
16
|
## License
|
|
@@ -1,25 +1,22 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { ICallback, ICompatibleLogger
|
|
2
|
+
import { ICallback, ICompatibleLogger } from '../../../types';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
4
|
import { RedisClient } from '../../redis-client/redis-client';
|
|
5
5
|
import { WorkerPool } from './worker-pool';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
private readonly workerParameters;
|
|
6
|
+
import { Worker } from '../worker';
|
|
7
|
+
export declare class WorkerRunner extends EventEmitter {
|
|
9
8
|
private readonly powerManager;
|
|
10
9
|
private readonly ticker;
|
|
11
10
|
private readonly lockManager;
|
|
12
11
|
private readonly redisClient;
|
|
13
12
|
private readonly logger;
|
|
14
13
|
private readonly workerPool;
|
|
15
|
-
constructor(redisClient: RedisClient,
|
|
14
|
+
constructor(redisClient: RedisClient, keyLock: string, workerPool: WorkerPool, logger: ICompatibleLogger);
|
|
16
15
|
private onTick;
|
|
17
|
-
private init;
|
|
18
|
-
private addToWorkerPool;
|
|
19
|
-
private createWorkerInstance;
|
|
20
16
|
private clearWorkerPool;
|
|
21
17
|
private stopTicker;
|
|
22
18
|
private releaseLock;
|
|
19
|
+
addWorker(instance: Worker): void;
|
|
23
20
|
run: () => void;
|
|
24
21
|
quit: (cb: ICallback<void>) => void;
|
|
25
22
|
}
|
|
@@ -1,19 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.WorkerRunner = void 0;
|
|
4
|
-
const path_1 = require("path");
|
|
5
|
-
const fs_1 = require("fs");
|
|
6
4
|
const power_manager_1 = require("../../power-manager/power-manager");
|
|
7
5
|
const events_1 = require("events");
|
|
8
6
|
const ticker_1 = require("../../ticker/ticker");
|
|
9
7
|
const lock_manager_1 = require("../../lock-manager/lock-manager");
|
|
10
8
|
const events_2 = require("../../events/events");
|
|
11
|
-
const empty_callback_reply_error_1 = require("../../errors/empty-callback-reply.error");
|
|
12
|
-
const panic_error_1 = require("../../errors/panic.error");
|
|
13
9
|
const async_1 = require("../../async/async");
|
|
14
10
|
const lock_manager_acquire_error_1 = require("../../lock-manager/errors/lock-manager-acquire.error");
|
|
15
11
|
class WorkerRunner extends events_1.EventEmitter {
|
|
16
|
-
constructor(redisClient,
|
|
12
|
+
constructor(redisClient, keyLock, workerPool, logger) {
|
|
17
13
|
super();
|
|
18
14
|
this.onTick = () => {
|
|
19
15
|
async_1.async.waterfall([
|
|
@@ -39,43 +35,6 @@ class WorkerRunner extends events_1.EventEmitter {
|
|
|
39
35
|
this.emit(events_2.events.ERROR, err);
|
|
40
36
|
});
|
|
41
37
|
};
|
|
42
|
-
this.init = (cb) => {
|
|
43
|
-
(0, fs_1.readdir)(this.workersDir, undefined, (err, reply) => {
|
|
44
|
-
if (err)
|
|
45
|
-
cb(err);
|
|
46
|
-
else {
|
|
47
|
-
async_1.async.each(reply !== null && reply !== void 0 ? reply : [], (filename, _, done) => {
|
|
48
|
-
if (filename.match(/\.worker\.js$/)) {
|
|
49
|
-
this.addToWorkerPool(filename, done);
|
|
50
|
-
}
|
|
51
|
-
else
|
|
52
|
-
done();
|
|
53
|
-
}, cb);
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
};
|
|
57
|
-
this.addToWorkerPool = (filename, cb) => {
|
|
58
|
-
this.createWorkerInstance(filename, (err, instance) => {
|
|
59
|
-
if (err)
|
|
60
|
-
cb(err);
|
|
61
|
-
else if (!instance)
|
|
62
|
-
cb(new empty_callback_reply_error_1.EmptyCallbackReplyError());
|
|
63
|
-
else if (!this.workerPool)
|
|
64
|
-
cb(new panic_error_1.PanicError(`Expected an instance of WorkerPool`));
|
|
65
|
-
else {
|
|
66
|
-
this.workerPool.add(instance);
|
|
67
|
-
cb();
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
};
|
|
71
|
-
this.createWorkerInstance = (filename, cb) => {
|
|
72
|
-
const filepath = (0, path_1.join)(this.workersDir, filename);
|
|
73
|
-
Promise.resolve().then(() => require(filepath)).then((module) => {
|
|
74
|
-
const worker = new module.default(this.redisClient, this.workerParameters, true);
|
|
75
|
-
cb(null, worker);
|
|
76
|
-
})
|
|
77
|
-
.catch(cb);
|
|
78
|
-
};
|
|
79
38
|
this.clearWorkerPool = (cb) => {
|
|
80
39
|
this.workerPool.clear(cb);
|
|
81
40
|
};
|
|
@@ -87,34 +46,25 @@ class WorkerRunner extends events_1.EventEmitter {
|
|
|
87
46
|
this.lockManager.releaseLock(cb);
|
|
88
47
|
};
|
|
89
48
|
this.run = () => {
|
|
90
|
-
this.
|
|
91
|
-
this.
|
|
92
|
-
if (err)
|
|
93
|
-
this.emit(events_2.events.ERROR, err);
|
|
94
|
-
else {
|
|
95
|
-
this.powerManager.commit();
|
|
96
|
-
this.emit(events_2.events.UP);
|
|
97
|
-
this.ticker.nextTick();
|
|
98
|
-
}
|
|
99
|
-
});
|
|
49
|
+
this.emit(events_2.events.UP);
|
|
50
|
+
this.ticker.nextTick();
|
|
100
51
|
};
|
|
101
52
|
this.quit = (cb) => {
|
|
102
|
-
this.powerManager.goingDown();
|
|
103
53
|
async_1.async.waterfall([this.stopTicker, this.clearWorkerPool, this.releaseLock], () => {
|
|
104
|
-
this.powerManager.commit();
|
|
105
54
|
this.emit(events_2.events.DOWN);
|
|
106
55
|
cb();
|
|
107
56
|
});
|
|
108
57
|
};
|
|
109
58
|
this.powerManager = new power_manager_1.PowerManager();
|
|
110
59
|
this.redisClient = redisClient;
|
|
111
|
-
this.workersDir = workersDir;
|
|
112
|
-
this.workerParameters = workerParameters;
|
|
113
60
|
this.logger = logger;
|
|
114
61
|
this.lockManager = new lock_manager_1.LockManager(redisClient, keyLock, 10000, false, true);
|
|
115
62
|
this.ticker = new ticker_1.Ticker(this.onTick);
|
|
116
63
|
this.workerPool = workerPool;
|
|
117
64
|
}
|
|
65
|
+
addWorker(instance) {
|
|
66
|
+
this.workerPool.add(instance);
|
|
67
|
+
}
|
|
118
68
|
}
|
|
119
69
|
exports.WorkerRunner = WorkerRunner;
|
|
120
70
|
//# sourceMappingURL=worker-runner.js.map
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
export declare abstract class Worker<T extends TWorkerParameters = TWorkerParameters> {
|
|
1
|
+
import { ICallback } from '../../types';
|
|
2
|
+
export declare abstract class Worker {
|
|
4
3
|
private readonly ticker;
|
|
5
4
|
private readonly powerManager;
|
|
6
5
|
private readonly managed;
|
|
7
|
-
|
|
8
|
-
protected readonly params: T;
|
|
9
|
-
constructor(redisClient: RedisClient, params: T, managed: boolean);
|
|
6
|
+
constructor(managed: boolean, timeout?: number);
|
|
10
7
|
private getTicker;
|
|
11
8
|
private getPowerManager;
|
|
12
9
|
private onTick;
|
|
@@ -6,7 +6,7 @@ const events_1 = require("../events/events");
|
|
|
6
6
|
const power_manager_1 = require("../power-manager/power-manager");
|
|
7
7
|
const panic_error_1 = require("../errors/panic.error");
|
|
8
8
|
class Worker {
|
|
9
|
-
constructor(
|
|
9
|
+
constructor(managed, timeout = 1000) {
|
|
10
10
|
this.ticker = null;
|
|
11
11
|
this.powerManager = null;
|
|
12
12
|
this.getTicker = () => {
|
|
@@ -46,11 +46,8 @@ class Worker {
|
|
|
46
46
|
else
|
|
47
47
|
cb();
|
|
48
48
|
};
|
|
49
|
-
this.redisClient = redisClient;
|
|
50
49
|
this.managed = managed;
|
|
51
|
-
this.params = params;
|
|
52
50
|
if (!managed) {
|
|
53
|
-
const { timeout = 1000 } = params;
|
|
54
51
|
this.ticker = new ticker_1.Ticker(this.onTick, timeout);
|
|
55
52
|
this.powerManager = new power_manager_1.PowerManager();
|
|
56
53
|
}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { KeyType, Pipeline, Redis, RedisOptions } from 'ioredis';
|
|
2
2
|
import { ClientOpts, Multi, RedisClient as NodeRedis } from 'redis';
|
|
3
|
-
import { Worker } from '../src/worker/worker';
|
|
4
|
-
import { RedisClient } from '../src/redis-client/redis-client';
|
|
5
3
|
import * as Logger from 'bunyan';
|
|
6
4
|
export interface IORedisConfig {
|
|
7
5
|
client: RedisClientName.IOREDIS;
|
|
@@ -66,12 +64,6 @@ export declare type TCompatibleRedisClient = (NodeRedis | Redis) & {
|
|
|
66
64
|
export declare type TRedisClientMulti = (Multi | Pipeline) & {
|
|
67
65
|
hmset(key: string, args: (string | number)[]): void;
|
|
68
66
|
};
|
|
69
|
-
export declare type TWorkerParameters = {
|
|
70
|
-
timeout?: number;
|
|
71
|
-
};
|
|
72
|
-
export declare type TWorkerClassConstructor<T extends TWorkerParameters> = {
|
|
73
|
-
new (redisClient: RedisClient, params: T, managed: boolean): Worker<T>;
|
|
74
|
-
};
|
|
75
67
|
export interface ICompatibleLogger {
|
|
76
68
|
info(message: unknown, ...params: unknown[]): void;
|
|
77
69
|
warn(message: unknown, ...params: unknown[]): void;
|
package/package.json
CHANGED