@hotmeshio/hotmesh 0.0.1
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/LICENSE +214 -0
- package/README.md +241 -0
- package/build/index.d.ts +4 -0
- package/build/index.js +7 -0
- package/build/modules/errors.d.ts +28 -0
- package/build/modules/errors.js +50 -0
- package/build/modules/key.d.ts +75 -0
- package/build/modules/key.js +116 -0
- package/build/modules/utils.d.ts +34 -0
- package/build/modules/utils.js +173 -0
- package/build/package.json +73 -0
- package/build/services/activities/activity.d.ts +59 -0
- package/build/services/activities/activity.js +396 -0
- package/build/services/activities/await.d.ts +16 -0
- package/build/services/activities/await.js +143 -0
- package/build/services/activities/emit.d.ts +9 -0
- package/build/services/activities/emit.js +13 -0
- package/build/services/activities/index.d.ts +15 -0
- package/build/services/activities/index.js +16 -0
- package/build/services/activities/iterate.d.ts +9 -0
- package/build/services/activities/iterate.js +13 -0
- package/build/services/activities/trigger.d.ts +22 -0
- package/build/services/activities/trigger.js +161 -0
- package/build/services/activities/worker.d.ts +17 -0
- package/build/services/activities/worker.js +164 -0
- package/build/services/collator/index.d.ts +54 -0
- package/build/services/collator/index.js +171 -0
- package/build/services/compiler/deployer.d.ts +35 -0
- package/build/services/compiler/deployer.js +412 -0
- package/build/services/compiler/index.d.ts +30 -0
- package/build/services/compiler/index.js +111 -0
- package/build/services/compiler/validator.d.ts +32 -0
- package/build/services/compiler/validator.js +134 -0
- package/build/services/connector/clients/ioredis.d.ts +13 -0
- package/build/services/connector/clients/ioredis.js +50 -0
- package/build/services/connector/clients/redis.d.ts +13 -0
- package/build/services/connector/clients/redis.js +62 -0
- package/build/services/connector/index.d.ts +5 -0
- package/build/services/connector/index.js +31 -0
- package/build/services/dimension/index.d.ts +29 -0
- package/build/services/dimension/index.js +35 -0
- package/build/services/durable/asyncLocalStorage.d.ts +3 -0
- package/build/services/durable/asyncLocalStorage.js +5 -0
- package/build/services/durable/client.d.ts +15 -0
- package/build/services/durable/client.js +108 -0
- package/build/services/durable/connection.d.ts +4 -0
- package/build/services/durable/connection.js +51 -0
- package/build/services/durable/factory.d.ts +3 -0
- package/build/services/durable/factory.js +123 -0
- package/build/services/durable/handle.d.ts +8 -0
- package/build/services/durable/handle.js +38 -0
- package/build/services/durable/index.d.ts +57 -0
- package/build/services/durable/index.js +58 -0
- package/build/services/durable/native.d.ts +4 -0
- package/build/services/durable/native.js +47 -0
- package/build/services/durable/worker.d.ts +36 -0
- package/build/services/durable/worker.js +266 -0
- package/build/services/durable/workflow.d.ts +6 -0
- package/build/services/durable/workflow.js +135 -0
- package/build/services/engine/index.d.ts +82 -0
- package/build/services/engine/index.js +511 -0
- package/build/services/hotmesh/index.d.ts +45 -0
- package/build/services/hotmesh/index.js +134 -0
- package/build/services/logger/index.d.ts +17 -0
- package/build/services/logger/index.js +73 -0
- package/build/services/mapper/index.d.ts +24 -0
- package/build/services/mapper/index.js +72 -0
- package/build/services/pipe/functions/array.d.ts +24 -0
- package/build/services/pipe/functions/array.js +69 -0
- package/build/services/pipe/functions/bitwise.d.ts +9 -0
- package/build/services/pipe/functions/bitwise.js +24 -0
- package/build/services/pipe/functions/conditional.d.ts +10 -0
- package/build/services/pipe/functions/conditional.js +27 -0
- package/build/services/pipe/functions/date.d.ts +57 -0
- package/build/services/pipe/functions/date.js +167 -0
- package/build/services/pipe/functions/index.d.ts +25 -0
- package/build/services/pipe/functions/index.js +26 -0
- package/build/services/pipe/functions/json.d.ts +5 -0
- package/build/services/pipe/functions/json.js +12 -0
- package/build/services/pipe/functions/math.d.ts +38 -0
- package/build/services/pipe/functions/math.js +111 -0
- package/build/services/pipe/functions/number.d.ts +25 -0
- package/build/services/pipe/functions/number.js +133 -0
- package/build/services/pipe/functions/object.d.ts +22 -0
- package/build/services/pipe/functions/object.js +63 -0
- package/build/services/pipe/functions/string.d.ts +23 -0
- package/build/services/pipe/functions/string.js +69 -0
- package/build/services/pipe/functions/symbol.d.ts +12 -0
- package/build/services/pipe/functions/symbol.js +33 -0
- package/build/services/pipe/functions/unary.d.ts +7 -0
- package/build/services/pipe/functions/unary.js +18 -0
- package/build/services/pipe/index.d.ts +30 -0
- package/build/services/pipe/index.js +128 -0
- package/build/services/quorum/index.d.ts +34 -0
- package/build/services/quorum/index.js +147 -0
- package/build/services/reporter/index.d.ts +47 -0
- package/build/services/reporter/index.js +330 -0
- package/build/services/serializer/index.d.ts +36 -0
- package/build/services/serializer/index.js +222 -0
- package/build/services/signaler/store.d.ts +15 -0
- package/build/services/signaler/store.js +53 -0
- package/build/services/signaler/stream.d.ts +43 -0
- package/build/services/signaler/stream.js +317 -0
- package/build/services/store/cache.d.ts +66 -0
- package/build/services/store/cache.js +127 -0
- package/build/services/store/clients/ioredis.d.ts +27 -0
- package/build/services/store/clients/ioredis.js +96 -0
- package/build/services/store/clients/redis.d.ts +29 -0
- package/build/services/store/clients/redis.js +143 -0
- package/build/services/store/index.d.ts +88 -0
- package/build/services/store/index.js +657 -0
- package/build/services/stream/clients/ioredis.d.ts +23 -0
- package/build/services/stream/clients/ioredis.js +115 -0
- package/build/services/stream/clients/redis.d.ts +23 -0
- package/build/services/stream/clients/redis.js +119 -0
- package/build/services/stream/index.d.ts +21 -0
- package/build/services/stream/index.js +9 -0
- package/build/services/sub/clients/ioredis.d.ts +20 -0
- package/build/services/sub/clients/ioredis.js +72 -0
- package/build/services/sub/clients/redis.d.ts +20 -0
- package/build/services/sub/clients/redis.js +63 -0
- package/build/services/sub/index.d.ts +18 -0
- package/build/services/sub/index.js +9 -0
- package/build/services/task/index.d.ts +18 -0
- package/build/services/task/index.js +73 -0
- package/build/services/telemetry/index.d.ts +49 -0
- package/build/services/telemetry/index.js +223 -0
- package/build/services/worker/index.d.ts +30 -0
- package/build/services/worker/index.js +105 -0
- package/build/types/activity.d.ts +86 -0
- package/build/types/activity.js +2 -0
- package/build/types/app.d.ts +16 -0
- package/build/types/app.js +2 -0
- package/build/types/async.d.ts +5 -0
- package/build/types/async.js +2 -0
- package/build/types/cache.d.ts +1 -0
- package/build/types/cache.js +2 -0
- package/build/types/collator.d.ts +8 -0
- package/build/types/collator.js +11 -0
- package/build/types/durable.d.ts +59 -0
- package/build/types/durable.js +2 -0
- package/build/types/hook.d.ts +31 -0
- package/build/types/hook.js +9 -0
- package/build/types/hotmesh.d.ts +82 -0
- package/build/types/hotmesh.js +2 -0
- package/build/types/index.d.ts +20 -0
- package/build/types/index.js +21 -0
- package/build/types/ioredisclient.d.ts +5 -0
- package/build/types/ioredisclient.js +5 -0
- package/build/types/job.d.ts +50 -0
- package/build/types/job.js +2 -0
- package/build/types/logger.d.ts +6 -0
- package/build/types/logger.js +2 -0
- package/build/types/map.d.ts +4 -0
- package/build/types/map.js +2 -0
- package/build/types/pipe.d.ts +4 -0
- package/build/types/pipe.js +2 -0
- package/build/types/quorum.d.ts +46 -0
- package/build/types/quorum.js +2 -0
- package/build/types/redis.d.ts +8 -0
- package/build/types/redis.js +2 -0
- package/build/types/redisclient.d.ts +25 -0
- package/build/types/redisclient.js +2 -0
- package/build/types/serializer.d.ts +33 -0
- package/build/types/serializer.js +2 -0
- package/build/types/stats.d.ts +83 -0
- package/build/types/stats.js +2 -0
- package/build/types/stream.d.ts +67 -0
- package/build/types/stream.js +25 -0
- package/build/types/telemetry.d.ts +1 -0
- package/build/types/telemetry.js +11 -0
- package/build/types/transition.d.ts +17 -0
- package/build/types/transition.js +2 -0
- package/index.ts +5 -0
- package/modules/errors.ts +55 -0
- package/modules/key.ts +129 -0
- package/modules/utils.ts +170 -0
- package/package.json +73 -0
- package/services/activities/activity.ts +473 -0
- package/services/activities/await.ts +172 -0
- package/services/activities/emit.ts +25 -0
- package/services/activities/index.ts +15 -0
- package/services/activities/iterate.ts +26 -0
- package/services/activities/trigger.ts +196 -0
- package/services/activities/worker.ts +190 -0
- package/services/collator/README.md +102 -0
- package/services/collator/index.ts +182 -0
- package/services/compiler/deployer.ts +432 -0
- package/services/compiler/index.ts +98 -0
- package/services/compiler/validator.ts +154 -0
- package/services/connector/clients/ioredis.ts +57 -0
- package/services/connector/clients/redis.ts +72 -0
- package/services/connector/index.ts +44 -0
- package/services/dimension/README.md +73 -0
- package/services/dimension/index.ts +39 -0
- package/services/durable/asyncLocalStorage.ts +3 -0
- package/services/durable/client.ts +116 -0
- package/services/durable/connection.ts +50 -0
- package/services/durable/factory.ts +124 -0
- package/services/durable/handle.ts +43 -0
- package/services/durable/index.ts +60 -0
- package/services/durable/native.ts +46 -0
- package/services/durable/worker.ts +254 -0
- package/services/durable/workflow.ts +136 -0
- package/services/engine/index.ts +615 -0
- package/services/hotmesh/index.ts +182 -0
- package/services/logger/index.ts +79 -0
- package/services/mapper/index.ts +84 -0
- package/services/pipe/functions/array.ts +87 -0
- package/services/pipe/functions/bitwise.ts +27 -0
- package/services/pipe/functions/conditional.ts +31 -0
- package/services/pipe/functions/date.ts +214 -0
- package/services/pipe/functions/index.ts +25 -0
- package/services/pipe/functions/json.ts +11 -0
- package/services/pipe/functions/math.ts +143 -0
- package/services/pipe/functions/number.ts +150 -0
- package/services/pipe/functions/object.ts +79 -0
- package/services/pipe/functions/string.ts +86 -0
- package/services/pipe/functions/symbol.ts +39 -0
- package/services/pipe/functions/unary.ts +19 -0
- package/services/pipe/index.ts +138 -0
- package/services/quorum/index.ts +200 -0
- package/services/reporter/index.ts +379 -0
- package/services/serializer/README.md +10 -0
- package/services/serializer/index.ts +243 -0
- package/services/signaler/store.ts +61 -0
- package/services/signaler/stream.ts +354 -0
- package/services/store/cache.ts +172 -0
- package/services/store/clients/ioredis.ts +123 -0
- package/services/store/clients/redis.ts +169 -0
- package/services/store/index.ts +757 -0
- package/services/stream/clients/ioredis.ts +148 -0
- package/services/stream/clients/redis.ts +144 -0
- package/services/stream/index.ts +57 -0
- package/services/sub/clients/ioredis.ts +83 -0
- package/services/sub/clients/redis.ts +74 -0
- package/services/sub/index.ts +25 -0
- package/services/task/index.ts +86 -0
- package/services/telemetry/index.ts +267 -0
- package/services/worker/index.ts +165 -0
- package/types/activity.ts +115 -0
- package/types/app.ts +20 -0
- package/types/async.ts +7 -0
- package/types/cache.ts +1 -0
- package/types/collator.ts +9 -0
- package/types/durable.ts +81 -0
- package/types/hook.ts +32 -0
- package/types/hotmesh.ts +102 -0
- package/types/index.ts +138 -0
- package/types/ioredisclient.ts +10 -0
- package/types/job.ts +59 -0
- package/types/logger.ts +6 -0
- package/types/map.ts +5 -0
- package/types/ms.d.ts +7 -0
- package/types/pipe.ts +7 -0
- package/types/quorum.ts +59 -0
- package/types/redis.ts +27 -0
- package/types/redisclient.ts +29 -0
- package/types/serializer.ts +38 -0
- package/types/stats.ts +100 -0
- package/types/stream.ts +75 -0
- package/types/telemetry.ts +15 -0
- package/types/transition.ts +20 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { KeyService, KeyStoreParams, KeyType, PSNS } from '../../../modules/key';
|
|
2
|
+
import { ILogger } from '../../logger';
|
|
3
|
+
import { StreamService } from '../index';
|
|
4
|
+
import { RedisClientType, RedisMultiType } from '../../../types/ioredisclient';
|
|
5
|
+
import { ReclaimedMessageType } from '../../../types/stream';
|
|
6
|
+
|
|
7
|
+
class IORedisStreamService extends StreamService<RedisClientType, RedisMultiType> {
|
|
8
|
+
redisClient: RedisClientType;
|
|
9
|
+
namespace: string;
|
|
10
|
+
logger: ILogger;
|
|
11
|
+
appId: string;
|
|
12
|
+
|
|
13
|
+
constructor(redisClient: RedisClientType) {
|
|
14
|
+
super(redisClient);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async init(namespace = PSNS, appId: string, logger: ILogger): Promise<void> {
|
|
18
|
+
this.namespace = namespace;
|
|
19
|
+
this.logger = logger;
|
|
20
|
+
this.appId = appId;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getMulti(): RedisMultiType {
|
|
24
|
+
return this.redisClient.multi();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
mintKey(type: KeyType, params: KeyStoreParams): string {
|
|
28
|
+
if (!this.namespace) throw new Error('namespace not set');
|
|
29
|
+
return KeyService.mintKey(this.namespace, type, params);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async xgroup(command: 'CREATE', key: string, groupName: string, id: string, mkStream?: 'MKSTREAM'): Promise<boolean> {
|
|
33
|
+
if (mkStream === 'MKSTREAM') {
|
|
34
|
+
try {
|
|
35
|
+
return (await this.redisClient.xgroup(command, key, groupName, id, mkStream)) === 'OK';
|
|
36
|
+
} catch (err) {
|
|
37
|
+
this.logger.info(`Consumer group not created with MKSTREAM for key: ${key} and group: ${groupName}`);
|
|
38
|
+
throw err;
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
try {
|
|
42
|
+
return (await this.redisClient.xgroup(command, key, groupName, id)) === 'OK';
|
|
43
|
+
} catch (err) {
|
|
44
|
+
this.logger.info(`Consumer group not created for key: ${key} and group: ${groupName}`);
|
|
45
|
+
throw err;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async xadd(key: string, id: string, messageId: string, messageValue: string, multi?: RedisMultiType): Promise<string | RedisMultiType> {
|
|
51
|
+
try {
|
|
52
|
+
return await (multi || this.redisClient).xadd(key, id, messageId, messageValue);
|
|
53
|
+
} catch (err) {
|
|
54
|
+
this.logger.error(`Error publishing 'xadd'; key: ${key}`, err);
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async xreadgroup(
|
|
60
|
+
command: 'GROUP',
|
|
61
|
+
groupName: string,
|
|
62
|
+
consumerName: string,
|
|
63
|
+
blockOption: 'BLOCK'|'COUNT',
|
|
64
|
+
blockTime: number|string,
|
|
65
|
+
streamsOption: 'STREAMS',
|
|
66
|
+
streamName: string,
|
|
67
|
+
id: string
|
|
68
|
+
): Promise<string[][][] | null | unknown[]> {
|
|
69
|
+
try {
|
|
70
|
+
//@ts-ignore
|
|
71
|
+
return await this.redisClient.xreadgroup(
|
|
72
|
+
command,
|
|
73
|
+
groupName,
|
|
74
|
+
consumerName,
|
|
75
|
+
// @ts-ignore
|
|
76
|
+
blockOption,
|
|
77
|
+
blockTime,
|
|
78
|
+
streamsOption,
|
|
79
|
+
streamName,
|
|
80
|
+
id
|
|
81
|
+
);
|
|
82
|
+
} catch (err) {
|
|
83
|
+
this.logger.error(`Error reading stream data [Stream ${streamName}] [Group ${groupName}]`, err);
|
|
84
|
+
throw err;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async xpending(
|
|
89
|
+
key: string,
|
|
90
|
+
group: string,
|
|
91
|
+
start?: string,
|
|
92
|
+
end?: string,
|
|
93
|
+
count?: number,
|
|
94
|
+
consumer?: string
|
|
95
|
+
): Promise<[string, string, number, [string, number][]][] | [string, string, number, number] | unknown[]> {
|
|
96
|
+
try {
|
|
97
|
+
const args = [key, group];
|
|
98
|
+
if (start) args.push(start);
|
|
99
|
+
if (end) args.push(end);
|
|
100
|
+
if (count !== undefined) args.push(count.toString());
|
|
101
|
+
if (consumer) args.push(consumer);
|
|
102
|
+
try {
|
|
103
|
+
return await this.redisClient.call('XPENDING', ...args) as [string, string, number, number][];
|
|
104
|
+
} catch (err) {
|
|
105
|
+
this.logger.error('err, args', err, args);
|
|
106
|
+
}
|
|
107
|
+
} catch (err) {
|
|
108
|
+
this.logger.error(`Error in retrieving pending messages for [stream ${key}], [group ${group}]`, err);
|
|
109
|
+
throw err;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async xclaim(
|
|
114
|
+
key: string,
|
|
115
|
+
group: string,
|
|
116
|
+
consumer: string,
|
|
117
|
+
minIdleTime: number,
|
|
118
|
+
id: string,
|
|
119
|
+
...args: string[]
|
|
120
|
+
): Promise<ReclaimedMessageType> {
|
|
121
|
+
try {
|
|
122
|
+
return await this.redisClient.xclaim(key, group, consumer, minIdleTime, id, ...args) as unknown as ReclaimedMessageType;
|
|
123
|
+
} catch (err) {
|
|
124
|
+
this.logger.error(`Error in claiming message with id: ${id} in group: ${group} for key: ${key}`, err);
|
|
125
|
+
throw err;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async xack(key: string, group: string, id: string, multi? : RedisMultiType): Promise<number|RedisMultiType> {
|
|
130
|
+
try {
|
|
131
|
+
return await (multi || this.redisClient).xack(key, group, id);
|
|
132
|
+
} catch (err) {
|
|
133
|
+
this.logger.error(`Error in acknowledging messages in group: ${group} for key: ${key}`, err);
|
|
134
|
+
throw err;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async xdel(key: string, id: string, multi? : RedisMultiType): Promise<number|RedisMultiType> {
|
|
139
|
+
try {
|
|
140
|
+
return await (multi || this.redisClient).xdel(key, id);
|
|
141
|
+
} catch (err) {
|
|
142
|
+
this.logger.error(`Error in deleting messages with id: ${id} for key: ${key}`, err);
|
|
143
|
+
throw err;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export { IORedisStreamService };
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { KeyService, KeyStoreParams, KeyType, PSNS } from '../../../modules/key';
|
|
2
|
+
import { ILogger } from '../../logger';
|
|
3
|
+
import { StreamService } from '../index';
|
|
4
|
+
import { RedisClientType, RedisMultiType } from '../../../types/redisclient';
|
|
5
|
+
import { ReclaimedMessageType } from '../../../types/stream';
|
|
6
|
+
|
|
7
|
+
class RedisStreamService extends StreamService<RedisClientType, RedisMultiType> {
|
|
8
|
+
redisClient: RedisClientType;
|
|
9
|
+
namespace: string;
|
|
10
|
+
logger: ILogger;
|
|
11
|
+
appId: string;
|
|
12
|
+
|
|
13
|
+
constructor(redisClient: RedisClientType) {
|
|
14
|
+
super(redisClient);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async init(namespace = PSNS, appId: string, logger: ILogger): Promise<void> {
|
|
18
|
+
this.namespace = namespace;
|
|
19
|
+
this.logger = logger;
|
|
20
|
+
this.appId = appId;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getMulti(): RedisMultiType {
|
|
24
|
+
return this.redisClient.MULTI() as unknown as RedisMultiType;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
mintKey(type: KeyType, params: KeyStoreParams): string {
|
|
28
|
+
if (!this.namespace) throw new Error('namespace not set');
|
|
29
|
+
return KeyService.mintKey(this.namespace, type, params);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async xgroup(command: 'CREATE', key: string, groupName: string, id: string, mkStream?: 'MKSTREAM'): Promise<boolean> {
|
|
33
|
+
const args = mkStream === 'MKSTREAM' ? ['MKSTREAM'] : [];
|
|
34
|
+
try {
|
|
35
|
+
return (await this.redisClient.sendCommand(['XGROUP', 'CREATE', key, groupName, id, ...args])) === 1;
|
|
36
|
+
} catch (err) {
|
|
37
|
+
const streamType = mkStream === 'MKSTREAM' ? 'with MKSTREAM' : 'without MKSTREAM';
|
|
38
|
+
this.logger.error(`x-group-error ${streamType} for key: ${key} and group: ${groupName}`, err);
|
|
39
|
+
throw err;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async xadd(key: string, id: string, ...args: any[]): Promise<string | RedisMultiType> {
|
|
44
|
+
let multi: RedisMultiType;
|
|
45
|
+
if (typeof args[args.length - 1] !== 'string') {
|
|
46
|
+
multi = args.pop() as RedisMultiType;
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
return await (multi || this.redisClient).XADD(key, id, { [args[0]]: args[1] });
|
|
50
|
+
} catch (err) {
|
|
51
|
+
this.logger.error(`Error publishing 'xadd'; key: ${key}`, err);
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async xreadgroup(
|
|
57
|
+
command: 'GROUP',
|
|
58
|
+
groupName: string,
|
|
59
|
+
consumerName: string,
|
|
60
|
+
blockOption: 'BLOCK'|'COUNT',
|
|
61
|
+
blockTime: number|string,
|
|
62
|
+
streamsOption: 'STREAMS',
|
|
63
|
+
streamName: string,
|
|
64
|
+
id: string
|
|
65
|
+
): Promise<string[][][] | null> {
|
|
66
|
+
try {
|
|
67
|
+
return await this.redisClient.sendCommand(['XREADGROUP', command, groupName, consumerName, blockOption, blockTime.toString(), streamsOption, streamName, id]);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
this.logger.error(`Error in reading data from group: ${groupName} in stream: ${streamName}`, err);
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async xpending(
|
|
75
|
+
key: string,
|
|
76
|
+
group: string,
|
|
77
|
+
start?: string,
|
|
78
|
+
end?: string,
|
|
79
|
+
count?: number,
|
|
80
|
+
consumer?: string
|
|
81
|
+
): Promise<[string, string, number, [string, number][]][] | [string, string, number, number]> {
|
|
82
|
+
try {
|
|
83
|
+
const args = [key, group];
|
|
84
|
+
if (start) args.push(start);
|
|
85
|
+
if (end) args.push(end);
|
|
86
|
+
if (count !== undefined) args.push(count.toString());
|
|
87
|
+
if (consumer) args.push(consumer);
|
|
88
|
+
try {
|
|
89
|
+
return await this.redisClient.sendCommand(['XPENDING', ...args]);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
this.logger.error('err, args', err, args);
|
|
92
|
+
}
|
|
93
|
+
} catch (err) {
|
|
94
|
+
this.logger.error(`Error in retrieving pending messages for group: ${group} in key: ${key}`, err);
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async xclaim(
|
|
100
|
+
key: string,
|
|
101
|
+
group: string,
|
|
102
|
+
consumer: string,
|
|
103
|
+
minIdleTime: number,
|
|
104
|
+
id: string,
|
|
105
|
+
...args: string[]
|
|
106
|
+
): Promise<ReclaimedMessageType> {
|
|
107
|
+
try {
|
|
108
|
+
return await this.redisClient.sendCommand(['XCLAIM', key, group, consumer, minIdleTime.toString(), id, ...args]) as unknown as ReclaimedMessageType;
|
|
109
|
+
} catch (err) {
|
|
110
|
+
this.logger.error(`Error in claiming message with id: ${id} in group: ${group} for key: ${key}`, err);
|
|
111
|
+
throw err;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async xack(key: string, group: string, id: string, multi? : RedisMultiType): Promise<number|RedisMultiType> {
|
|
116
|
+
try {
|
|
117
|
+
if (multi) {
|
|
118
|
+
multi.XACK(key, group, id);
|
|
119
|
+
return multi;
|
|
120
|
+
} else {
|
|
121
|
+
return await this.redisClient.XACK(key, group, id);
|
|
122
|
+
}
|
|
123
|
+
} catch (err) {
|
|
124
|
+
this.logger.error(`Error in acknowledging messages in group: ${group} for key: ${key}`, err);
|
|
125
|
+
throw err;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async xdel(key: string, id: string, multi? : RedisMultiType): Promise<number|RedisMultiType> {
|
|
130
|
+
try {
|
|
131
|
+
if (multi) {
|
|
132
|
+
multi.XDEL(key, id);
|
|
133
|
+
return multi;
|
|
134
|
+
} else {
|
|
135
|
+
return await this.redisClient.XDEL(key, id);
|
|
136
|
+
}
|
|
137
|
+
} catch (err) {
|
|
138
|
+
this.logger.error(`Error in deleting messages with ids: ${id} for key: ${key}`, err);
|
|
139
|
+
throw err;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export { RedisStreamService };
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { KeyStoreParams, KeyType } from '../../modules/key';
|
|
2
|
+
import { ReclaimedMessageType } from '../../types/stream';
|
|
3
|
+
import { ILogger } from '../logger';
|
|
4
|
+
|
|
5
|
+
abstract class StreamService<T, U> {
|
|
6
|
+
redisClient: T;
|
|
7
|
+
namespace: string;
|
|
8
|
+
logger: ILogger;
|
|
9
|
+
appId: string;
|
|
10
|
+
|
|
11
|
+
constructor(redisClient: T) {
|
|
12
|
+
this.redisClient = redisClient;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
abstract init(namespace: string, appId: string, logger: ILogger): Promise<void>;
|
|
16
|
+
abstract getMulti(): U;
|
|
17
|
+
abstract mintKey(type: KeyType, params: KeyStoreParams): string;
|
|
18
|
+
abstract xgroup(
|
|
19
|
+
command: 'CREATE',
|
|
20
|
+
key: string,
|
|
21
|
+
groupName: string,
|
|
22
|
+
id: string,
|
|
23
|
+
mkStream?: 'MKSTREAM'): Promise<boolean>;
|
|
24
|
+
abstract xadd(
|
|
25
|
+
key: string,
|
|
26
|
+
id: string,
|
|
27
|
+
messageId: string,
|
|
28
|
+
messageValue: string,
|
|
29
|
+
multi?: U): Promise<string | U>;
|
|
30
|
+
abstract xreadgroup(
|
|
31
|
+
command: 'GROUP',
|
|
32
|
+
groupName: string,
|
|
33
|
+
consumerName: string,
|
|
34
|
+
blockOption: 'BLOCK'|'COUNT',
|
|
35
|
+
blockTime: number|string,
|
|
36
|
+
streamsOption: 'STREAMS',
|
|
37
|
+
streamName: string,
|
|
38
|
+
id: string): Promise<string[][][] | null | unknown[]>;
|
|
39
|
+
abstract xpending(
|
|
40
|
+
key: string,
|
|
41
|
+
group: string,
|
|
42
|
+
start?: string,
|
|
43
|
+
end?: string,
|
|
44
|
+
count?: number,
|
|
45
|
+
consumer?: string): Promise<[string, string, number, [string, number][]][] | [string, string, number, number] | unknown[]>;
|
|
46
|
+
abstract xclaim(
|
|
47
|
+
key: string,
|
|
48
|
+
group: string,
|
|
49
|
+
consumer: string,
|
|
50
|
+
minIdleTime: number,
|
|
51
|
+
id: string,
|
|
52
|
+
...args: string[]): Promise<ReclaimedMessageType>;
|
|
53
|
+
abstract xack(key: string, group: string, id: string, multi?: U): Promise<number|U>;
|
|
54
|
+
abstract xdel(key: string, id: string, multi?: U): Promise<number|U>;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export { StreamService };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { KeyService, KeyStoreParams, KeyType, PSNS } from '../../../modules/key';
|
|
2
|
+
import { ILogger } from '../../logger';
|
|
3
|
+
import { SubService } from '../index';
|
|
4
|
+
import { RedisClientType, RedisMultiType } from '../../../types/ioredisclient';
|
|
5
|
+
import { SubscriptionCallback } from '../../../types/quorum';
|
|
6
|
+
|
|
7
|
+
class IORedisSubService extends SubService<RedisClientType, RedisMultiType> {
|
|
8
|
+
redisClient: RedisClientType;
|
|
9
|
+
namespace: string;
|
|
10
|
+
logger: ILogger;
|
|
11
|
+
appId: string
|
|
12
|
+
|
|
13
|
+
constructor(redisClient: RedisClientType) {
|
|
14
|
+
super(redisClient);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async init(namespace = PSNS, appId: string, engineId: string, logger: ILogger): Promise<void> {
|
|
18
|
+
this.namespace = namespace;
|
|
19
|
+
this.logger = logger;
|
|
20
|
+
this.appId = appId;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getMulti(): RedisMultiType {
|
|
24
|
+
return this.redisClient.multi();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
mintKey(type: KeyType, params: KeyStoreParams): string {
|
|
28
|
+
if (!this.namespace) throw new Error('namespace not set');
|
|
29
|
+
return KeyService.mintKey(this.namespace, type, params);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async subscribe(keyType: KeyType.QUORUM, callback: SubscriptionCallback, appId: string, engineId?: string): Promise<void> {
|
|
33
|
+
const self = this;
|
|
34
|
+
const topic = this.mintKey(keyType, { appId, engineId });
|
|
35
|
+
await this.redisClient.subscribe(topic, (err) => {
|
|
36
|
+
if (err) {
|
|
37
|
+
self.logger.error(`Error subscribing to: ${topic}`, err);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
this.redisClient.on('message', (channel, message) => {
|
|
41
|
+
if (channel === topic) {
|
|
42
|
+
try {
|
|
43
|
+
const payload = JSON.parse(message);
|
|
44
|
+
callback(topic, payload);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
self.logger.error(`Error parsing message: ${message}`, e);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async unsubscribe(keyType: KeyType.QUORUM, appId: string, engineId?: string): Promise<void> {
|
|
53
|
+
const topic = this.mintKey(keyType, { appId, engineId });
|
|
54
|
+
await this.redisClient.unsubscribe(topic);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async psubscribe(keyType: KeyType.QUORUM, callback: SubscriptionCallback, appId: string, engineId?: string): Promise<void> {
|
|
58
|
+
const self = this;
|
|
59
|
+
const topic = this.mintKey(keyType, { appId, engineId });
|
|
60
|
+
await this.redisClient.psubscribe(topic, (err) => {
|
|
61
|
+
if (err) {
|
|
62
|
+
self.logger.error(`Error subscribing to: ${topic}`, err);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
this.redisClient.on('pmessage', (pattern, channel, message) => {
|
|
66
|
+
if (pattern === topic) {
|
|
67
|
+
try {
|
|
68
|
+
const payload = JSON.parse(message);
|
|
69
|
+
callback(channel, payload);
|
|
70
|
+
} catch (e) {
|
|
71
|
+
self.logger.error(`Error parsing message: ${message}`, e);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async punsubscribe(keyType: KeyType.QUORUM, appId: string, engineId?: string): Promise<void> {
|
|
78
|
+
const topic = this.mintKey(keyType, { appId, engineId });
|
|
79
|
+
await this.redisClient.punsubscribe(topic);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { IORedisSubService };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { KeyService, KeyStoreParams, KeyType, PSNS } from '../../../modules/key';
|
|
2
|
+
import { ILogger } from '../../logger';
|
|
3
|
+
import { SubService } from '../index';
|
|
4
|
+
import { RedisClientType, RedisMultiType } from '../../../types/redisclient';
|
|
5
|
+
import { SubscriptionCallback } from '../../../types/quorum';
|
|
6
|
+
|
|
7
|
+
class RedisSubService extends SubService<RedisClientType, RedisMultiType> {
|
|
8
|
+
redisClient: RedisClientType;
|
|
9
|
+
namespace: string;
|
|
10
|
+
logger: ILogger;
|
|
11
|
+
appId: string;
|
|
12
|
+
|
|
13
|
+
constructor(redisClient: RedisClientType) {
|
|
14
|
+
super(redisClient);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async init(namespace = PSNS, appId: string, engineId: string, logger: ILogger): Promise<void> {
|
|
18
|
+
this.namespace = namespace;
|
|
19
|
+
this.logger = logger;
|
|
20
|
+
this.appId = appId;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
getMulti(): RedisMultiType {
|
|
24
|
+
const multi = this.redisClient.MULTI();
|
|
25
|
+
return multi as unknown as RedisMultiType;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
mintKey(type: KeyType, params: KeyStoreParams): string {
|
|
29
|
+
if (!this.namespace) throw new Error('namespace not set');
|
|
30
|
+
return KeyService.mintKey(this.namespace, type, params);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async subscribe(keyType: KeyType.QUORUM, callback: SubscriptionCallback, appId: string, engineId?: string): Promise<void> {
|
|
34
|
+
if (this.redisClient) {
|
|
35
|
+
const self = this;
|
|
36
|
+
const topic = this.mintKey(keyType, { appId, engineId });
|
|
37
|
+
await this.redisClient.subscribe(topic, (message) => {
|
|
38
|
+
try {
|
|
39
|
+
const payload = JSON.parse(message);
|
|
40
|
+
callback(topic, payload);
|
|
41
|
+
} catch (e) {
|
|
42
|
+
self.logger.error(`Error parsing message: ${message}`, e);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async unsubscribe(keyType: KeyType.QUORUM, appId: string, engineId?: string): Promise<void> {
|
|
49
|
+
const topic = this.mintKey(keyType, { appId, engineId });
|
|
50
|
+
await this.redisClient.unsubscribe(topic);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async psubscribe(keyType: KeyType.QUORUM, callback: SubscriptionCallback, appId: string, engineId?: string): Promise<void> {
|
|
54
|
+
if (this.redisClient) {
|
|
55
|
+
const self = this;
|
|
56
|
+
const topic = this.mintKey(keyType, { appId, engineId });
|
|
57
|
+
await this.redisClient.pSubscribe(topic, (message, channel) => {
|
|
58
|
+
try {
|
|
59
|
+
const payload = JSON.parse(message);
|
|
60
|
+
callback(channel, payload);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
self.logger.error(`Error parsing message: ${message}`, e);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async punsubscribe(keyType: KeyType.QUORUM, appId: string, engineId?: string): Promise<void> {
|
|
69
|
+
const topic = this.mintKey(keyType, { appId, engineId });
|
|
70
|
+
await this.redisClient.pUnsubscribe(topic);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export { RedisSubService };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { KeyStoreParams, KeyType } from '../../modules/key';
|
|
2
|
+
import { ILogger } from '../logger';
|
|
3
|
+
import { SubscriptionCallback } from '../../types/quorum';
|
|
4
|
+
|
|
5
|
+
abstract class SubService<T, U> {
|
|
6
|
+
redisClient: T;
|
|
7
|
+
namespace: string;
|
|
8
|
+
logger: ILogger;
|
|
9
|
+
appId: string;
|
|
10
|
+
|
|
11
|
+
constructor(redisClient: T) {
|
|
12
|
+
this.redisClient = redisClient;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
abstract init(namespace: string, appId: string, engineId: string, logger: ILogger): Promise<void>;
|
|
16
|
+
abstract getMulti(): U;
|
|
17
|
+
abstract mintKey(type: KeyType, params: KeyStoreParams): string;
|
|
18
|
+
abstract subscribe(keyType: KeyType.QUORUM, callback: SubscriptionCallback, appId: string, engineId?: string): Promise<void>;
|
|
19
|
+
abstract unsubscribe(keyType: KeyType.QUORUM, appId: string, engineId?: string): Promise<void>;
|
|
20
|
+
abstract psubscribe(keyType: KeyType.QUORUM, callback: SubscriptionCallback, appId: string, engineId?: string): Promise<void>;
|
|
21
|
+
abstract punsubscribe(keyType: KeyType.QUORUM, appId: string, engineId?: string): Promise<void>;
|
|
22
|
+
//NOTE: `publish` happens in the 'StoreService' as Redis subscription clients must be read-only
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { SubService };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { ILogger } from '../logger';
|
|
2
|
+
import { StoreService } from '../store';
|
|
3
|
+
import { RedisClient, RedisMulti } from '../../types/redis';
|
|
4
|
+
import { HookInterface } from '../../types/hook';
|
|
5
|
+
import { XSleepFor, sleepFor } from '../../modules/utils';
|
|
6
|
+
|
|
7
|
+
//system timer granularity limit (task queues organize)
|
|
8
|
+
const FIDELITY_SECONDS = 15; //note: this can be reduced using 'watch' or scout role
|
|
9
|
+
//default resolution/fidelity when expiring
|
|
10
|
+
const EXPIRATION_FIDELITY_SECONDS = 60;
|
|
11
|
+
|
|
12
|
+
class TaskService {
|
|
13
|
+
store: StoreService<RedisClient, RedisMulti>;
|
|
14
|
+
logger: ILogger;
|
|
15
|
+
cleanupTimeout: NodeJS.Timeout | null = null;
|
|
16
|
+
|
|
17
|
+
constructor(
|
|
18
|
+
store: StoreService<RedisClient, RedisMulti>,
|
|
19
|
+
logger: ILogger
|
|
20
|
+
) {
|
|
21
|
+
this.logger = logger;
|
|
22
|
+
this.store = store;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async processWebHooks(hookEventCallback: HookInterface): Promise<void> {
|
|
26
|
+
const workItemKey = await this.store.getActiveTaskQueue();
|
|
27
|
+
if (workItemKey) {
|
|
28
|
+
const [topic, sourceKey, ...sdata] = workItemKey.split('::');
|
|
29
|
+
const data = JSON.parse(sdata.join('::'));
|
|
30
|
+
const destinationKey = `${sourceKey}:processed`;
|
|
31
|
+
const jobId = await this.store.processTaskQueue(sourceKey, destinationKey);
|
|
32
|
+
if (jobId) {
|
|
33
|
+
await hookEventCallback(topic, { ...data, id: jobId });
|
|
34
|
+
//todo: do final checksum count (values are tracked in the stats hash)
|
|
35
|
+
} else {
|
|
36
|
+
await this.store.deleteProcessedTaskQueue(workItemKey, sourceKey, destinationKey);
|
|
37
|
+
}
|
|
38
|
+
setImmediate(() => this.processWebHooks(hookEventCallback));
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async enqueueWorkItems(keys: string[]): Promise<void> {
|
|
43
|
+
await this.store.addTaskQueues(keys);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async registerJobForCleanup(jobId: string, inSeconds = EXPIRATION_FIDELITY_SECONDS): Promise<void> {
|
|
47
|
+
if (inSeconds > -1) {
|
|
48
|
+
await this.store.expireJob(jobId, inSeconds);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async registerTimeHook(jobId: string, activityId: string, type: 'sleep'|'expire'|'cron', inSeconds = FIDELITY_SECONDS, multi?: RedisMulti): Promise<void> {
|
|
53
|
+
const awakenTimeSlot = Math.floor((Date.now() + inSeconds * 1000) / (FIDELITY_SECONDS * 1000)) * (FIDELITY_SECONDS * 1000); //n second awaken groups
|
|
54
|
+
await this.store.registerTimeHook(jobId, activityId, type, awakenTimeSlot, multi);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
//todo: need 'scout' role in quorum to check for this and then alert the quorum to get to work
|
|
58
|
+
async processTimeHooks(timeEventCallback: (jobId: string, activityId: string) => Promise<void>, listKey?: string): Promise<void> {
|
|
59
|
+
try {
|
|
60
|
+
const job = await this.store.getNextTimeJob(listKey);
|
|
61
|
+
if (job) {
|
|
62
|
+
const [listKey, jobId, activityId] = job;
|
|
63
|
+
await timeEventCallback(jobId, activityId);
|
|
64
|
+
await sleepFor(0);
|
|
65
|
+
this.processTimeHooks(timeEventCallback, listKey);
|
|
66
|
+
} else {
|
|
67
|
+
let sleep = XSleepFor(FIDELITY_SECONDS * 1000);
|
|
68
|
+
this.cleanupTimeout = sleep.timerId;
|
|
69
|
+
await sleep.promise;
|
|
70
|
+
this.processTimeHooks(timeEventCallback)
|
|
71
|
+
}
|
|
72
|
+
} catch (err) {
|
|
73
|
+
//todo: retry connect to redis
|
|
74
|
+
this.logger.error('task-process-timehooks-error', err);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
cancelCleanup() {
|
|
79
|
+
if (this.cleanupTimeout !== undefined) {
|
|
80
|
+
clearTimeout(this.cleanupTimeout);
|
|
81
|
+
this.cleanupTimeout = undefined;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export { TaskService };
|