@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.
Files changed (263) hide show
  1. package/LICENSE +214 -0
  2. package/README.md +241 -0
  3. package/build/index.d.ts +4 -0
  4. package/build/index.js +7 -0
  5. package/build/modules/errors.d.ts +28 -0
  6. package/build/modules/errors.js +50 -0
  7. package/build/modules/key.d.ts +75 -0
  8. package/build/modules/key.js +116 -0
  9. package/build/modules/utils.d.ts +34 -0
  10. package/build/modules/utils.js +173 -0
  11. package/build/package.json +73 -0
  12. package/build/services/activities/activity.d.ts +59 -0
  13. package/build/services/activities/activity.js +396 -0
  14. package/build/services/activities/await.d.ts +16 -0
  15. package/build/services/activities/await.js +143 -0
  16. package/build/services/activities/emit.d.ts +9 -0
  17. package/build/services/activities/emit.js +13 -0
  18. package/build/services/activities/index.d.ts +15 -0
  19. package/build/services/activities/index.js +16 -0
  20. package/build/services/activities/iterate.d.ts +9 -0
  21. package/build/services/activities/iterate.js +13 -0
  22. package/build/services/activities/trigger.d.ts +22 -0
  23. package/build/services/activities/trigger.js +161 -0
  24. package/build/services/activities/worker.d.ts +17 -0
  25. package/build/services/activities/worker.js +164 -0
  26. package/build/services/collator/index.d.ts +54 -0
  27. package/build/services/collator/index.js +171 -0
  28. package/build/services/compiler/deployer.d.ts +35 -0
  29. package/build/services/compiler/deployer.js +412 -0
  30. package/build/services/compiler/index.d.ts +30 -0
  31. package/build/services/compiler/index.js +111 -0
  32. package/build/services/compiler/validator.d.ts +32 -0
  33. package/build/services/compiler/validator.js +134 -0
  34. package/build/services/connector/clients/ioredis.d.ts +13 -0
  35. package/build/services/connector/clients/ioredis.js +50 -0
  36. package/build/services/connector/clients/redis.d.ts +13 -0
  37. package/build/services/connector/clients/redis.js +62 -0
  38. package/build/services/connector/index.d.ts +5 -0
  39. package/build/services/connector/index.js +31 -0
  40. package/build/services/dimension/index.d.ts +29 -0
  41. package/build/services/dimension/index.js +35 -0
  42. package/build/services/durable/asyncLocalStorage.d.ts +3 -0
  43. package/build/services/durable/asyncLocalStorage.js +5 -0
  44. package/build/services/durable/client.d.ts +15 -0
  45. package/build/services/durable/client.js +108 -0
  46. package/build/services/durable/connection.d.ts +4 -0
  47. package/build/services/durable/connection.js +51 -0
  48. package/build/services/durable/factory.d.ts +3 -0
  49. package/build/services/durable/factory.js +123 -0
  50. package/build/services/durable/handle.d.ts +8 -0
  51. package/build/services/durable/handle.js +38 -0
  52. package/build/services/durable/index.d.ts +57 -0
  53. package/build/services/durable/index.js +58 -0
  54. package/build/services/durable/native.d.ts +4 -0
  55. package/build/services/durable/native.js +47 -0
  56. package/build/services/durable/worker.d.ts +36 -0
  57. package/build/services/durable/worker.js +266 -0
  58. package/build/services/durable/workflow.d.ts +6 -0
  59. package/build/services/durable/workflow.js +135 -0
  60. package/build/services/engine/index.d.ts +82 -0
  61. package/build/services/engine/index.js +511 -0
  62. package/build/services/hotmesh/index.d.ts +45 -0
  63. package/build/services/hotmesh/index.js +134 -0
  64. package/build/services/logger/index.d.ts +17 -0
  65. package/build/services/logger/index.js +73 -0
  66. package/build/services/mapper/index.d.ts +24 -0
  67. package/build/services/mapper/index.js +72 -0
  68. package/build/services/pipe/functions/array.d.ts +24 -0
  69. package/build/services/pipe/functions/array.js +69 -0
  70. package/build/services/pipe/functions/bitwise.d.ts +9 -0
  71. package/build/services/pipe/functions/bitwise.js +24 -0
  72. package/build/services/pipe/functions/conditional.d.ts +10 -0
  73. package/build/services/pipe/functions/conditional.js +27 -0
  74. package/build/services/pipe/functions/date.d.ts +57 -0
  75. package/build/services/pipe/functions/date.js +167 -0
  76. package/build/services/pipe/functions/index.d.ts +25 -0
  77. package/build/services/pipe/functions/index.js +26 -0
  78. package/build/services/pipe/functions/json.d.ts +5 -0
  79. package/build/services/pipe/functions/json.js +12 -0
  80. package/build/services/pipe/functions/math.d.ts +38 -0
  81. package/build/services/pipe/functions/math.js +111 -0
  82. package/build/services/pipe/functions/number.d.ts +25 -0
  83. package/build/services/pipe/functions/number.js +133 -0
  84. package/build/services/pipe/functions/object.d.ts +22 -0
  85. package/build/services/pipe/functions/object.js +63 -0
  86. package/build/services/pipe/functions/string.d.ts +23 -0
  87. package/build/services/pipe/functions/string.js +69 -0
  88. package/build/services/pipe/functions/symbol.d.ts +12 -0
  89. package/build/services/pipe/functions/symbol.js +33 -0
  90. package/build/services/pipe/functions/unary.d.ts +7 -0
  91. package/build/services/pipe/functions/unary.js +18 -0
  92. package/build/services/pipe/index.d.ts +30 -0
  93. package/build/services/pipe/index.js +128 -0
  94. package/build/services/quorum/index.d.ts +34 -0
  95. package/build/services/quorum/index.js +147 -0
  96. package/build/services/reporter/index.d.ts +47 -0
  97. package/build/services/reporter/index.js +330 -0
  98. package/build/services/serializer/index.d.ts +36 -0
  99. package/build/services/serializer/index.js +222 -0
  100. package/build/services/signaler/store.d.ts +15 -0
  101. package/build/services/signaler/store.js +53 -0
  102. package/build/services/signaler/stream.d.ts +43 -0
  103. package/build/services/signaler/stream.js +317 -0
  104. package/build/services/store/cache.d.ts +66 -0
  105. package/build/services/store/cache.js +127 -0
  106. package/build/services/store/clients/ioredis.d.ts +27 -0
  107. package/build/services/store/clients/ioredis.js +96 -0
  108. package/build/services/store/clients/redis.d.ts +29 -0
  109. package/build/services/store/clients/redis.js +143 -0
  110. package/build/services/store/index.d.ts +88 -0
  111. package/build/services/store/index.js +657 -0
  112. package/build/services/stream/clients/ioredis.d.ts +23 -0
  113. package/build/services/stream/clients/ioredis.js +115 -0
  114. package/build/services/stream/clients/redis.d.ts +23 -0
  115. package/build/services/stream/clients/redis.js +119 -0
  116. package/build/services/stream/index.d.ts +21 -0
  117. package/build/services/stream/index.js +9 -0
  118. package/build/services/sub/clients/ioredis.d.ts +20 -0
  119. package/build/services/sub/clients/ioredis.js +72 -0
  120. package/build/services/sub/clients/redis.d.ts +20 -0
  121. package/build/services/sub/clients/redis.js +63 -0
  122. package/build/services/sub/index.d.ts +18 -0
  123. package/build/services/sub/index.js +9 -0
  124. package/build/services/task/index.d.ts +18 -0
  125. package/build/services/task/index.js +73 -0
  126. package/build/services/telemetry/index.d.ts +49 -0
  127. package/build/services/telemetry/index.js +223 -0
  128. package/build/services/worker/index.d.ts +30 -0
  129. package/build/services/worker/index.js +105 -0
  130. package/build/types/activity.d.ts +86 -0
  131. package/build/types/activity.js +2 -0
  132. package/build/types/app.d.ts +16 -0
  133. package/build/types/app.js +2 -0
  134. package/build/types/async.d.ts +5 -0
  135. package/build/types/async.js +2 -0
  136. package/build/types/cache.d.ts +1 -0
  137. package/build/types/cache.js +2 -0
  138. package/build/types/collator.d.ts +8 -0
  139. package/build/types/collator.js +11 -0
  140. package/build/types/durable.d.ts +59 -0
  141. package/build/types/durable.js +2 -0
  142. package/build/types/hook.d.ts +31 -0
  143. package/build/types/hook.js +9 -0
  144. package/build/types/hotmesh.d.ts +82 -0
  145. package/build/types/hotmesh.js +2 -0
  146. package/build/types/index.d.ts +20 -0
  147. package/build/types/index.js +21 -0
  148. package/build/types/ioredisclient.d.ts +5 -0
  149. package/build/types/ioredisclient.js +5 -0
  150. package/build/types/job.d.ts +50 -0
  151. package/build/types/job.js +2 -0
  152. package/build/types/logger.d.ts +6 -0
  153. package/build/types/logger.js +2 -0
  154. package/build/types/map.d.ts +4 -0
  155. package/build/types/map.js +2 -0
  156. package/build/types/pipe.d.ts +4 -0
  157. package/build/types/pipe.js +2 -0
  158. package/build/types/quorum.d.ts +46 -0
  159. package/build/types/quorum.js +2 -0
  160. package/build/types/redis.d.ts +8 -0
  161. package/build/types/redis.js +2 -0
  162. package/build/types/redisclient.d.ts +25 -0
  163. package/build/types/redisclient.js +2 -0
  164. package/build/types/serializer.d.ts +33 -0
  165. package/build/types/serializer.js +2 -0
  166. package/build/types/stats.d.ts +83 -0
  167. package/build/types/stats.js +2 -0
  168. package/build/types/stream.d.ts +67 -0
  169. package/build/types/stream.js +25 -0
  170. package/build/types/telemetry.d.ts +1 -0
  171. package/build/types/telemetry.js +11 -0
  172. package/build/types/transition.d.ts +17 -0
  173. package/build/types/transition.js +2 -0
  174. package/index.ts +5 -0
  175. package/modules/errors.ts +55 -0
  176. package/modules/key.ts +129 -0
  177. package/modules/utils.ts +170 -0
  178. package/package.json +73 -0
  179. package/services/activities/activity.ts +473 -0
  180. package/services/activities/await.ts +172 -0
  181. package/services/activities/emit.ts +25 -0
  182. package/services/activities/index.ts +15 -0
  183. package/services/activities/iterate.ts +26 -0
  184. package/services/activities/trigger.ts +196 -0
  185. package/services/activities/worker.ts +190 -0
  186. package/services/collator/README.md +102 -0
  187. package/services/collator/index.ts +182 -0
  188. package/services/compiler/deployer.ts +432 -0
  189. package/services/compiler/index.ts +98 -0
  190. package/services/compiler/validator.ts +154 -0
  191. package/services/connector/clients/ioredis.ts +57 -0
  192. package/services/connector/clients/redis.ts +72 -0
  193. package/services/connector/index.ts +44 -0
  194. package/services/dimension/README.md +73 -0
  195. package/services/dimension/index.ts +39 -0
  196. package/services/durable/asyncLocalStorage.ts +3 -0
  197. package/services/durable/client.ts +116 -0
  198. package/services/durable/connection.ts +50 -0
  199. package/services/durable/factory.ts +124 -0
  200. package/services/durable/handle.ts +43 -0
  201. package/services/durable/index.ts +60 -0
  202. package/services/durable/native.ts +46 -0
  203. package/services/durable/worker.ts +254 -0
  204. package/services/durable/workflow.ts +136 -0
  205. package/services/engine/index.ts +615 -0
  206. package/services/hotmesh/index.ts +182 -0
  207. package/services/logger/index.ts +79 -0
  208. package/services/mapper/index.ts +84 -0
  209. package/services/pipe/functions/array.ts +87 -0
  210. package/services/pipe/functions/bitwise.ts +27 -0
  211. package/services/pipe/functions/conditional.ts +31 -0
  212. package/services/pipe/functions/date.ts +214 -0
  213. package/services/pipe/functions/index.ts +25 -0
  214. package/services/pipe/functions/json.ts +11 -0
  215. package/services/pipe/functions/math.ts +143 -0
  216. package/services/pipe/functions/number.ts +150 -0
  217. package/services/pipe/functions/object.ts +79 -0
  218. package/services/pipe/functions/string.ts +86 -0
  219. package/services/pipe/functions/symbol.ts +39 -0
  220. package/services/pipe/functions/unary.ts +19 -0
  221. package/services/pipe/index.ts +138 -0
  222. package/services/quorum/index.ts +200 -0
  223. package/services/reporter/index.ts +379 -0
  224. package/services/serializer/README.md +10 -0
  225. package/services/serializer/index.ts +243 -0
  226. package/services/signaler/store.ts +61 -0
  227. package/services/signaler/stream.ts +354 -0
  228. package/services/store/cache.ts +172 -0
  229. package/services/store/clients/ioredis.ts +123 -0
  230. package/services/store/clients/redis.ts +169 -0
  231. package/services/store/index.ts +757 -0
  232. package/services/stream/clients/ioredis.ts +148 -0
  233. package/services/stream/clients/redis.ts +144 -0
  234. package/services/stream/index.ts +57 -0
  235. package/services/sub/clients/ioredis.ts +83 -0
  236. package/services/sub/clients/redis.ts +74 -0
  237. package/services/sub/index.ts +25 -0
  238. package/services/task/index.ts +86 -0
  239. package/services/telemetry/index.ts +267 -0
  240. package/services/worker/index.ts +165 -0
  241. package/types/activity.ts +115 -0
  242. package/types/app.ts +20 -0
  243. package/types/async.ts +7 -0
  244. package/types/cache.ts +1 -0
  245. package/types/collator.ts +9 -0
  246. package/types/durable.ts +81 -0
  247. package/types/hook.ts +32 -0
  248. package/types/hotmesh.ts +102 -0
  249. package/types/index.ts +138 -0
  250. package/types/ioredisclient.ts +10 -0
  251. package/types/job.ts +59 -0
  252. package/types/logger.ts +6 -0
  253. package/types/map.ts +5 -0
  254. package/types/ms.d.ts +7 -0
  255. package/types/pipe.ts +7 -0
  256. package/types/quorum.ts +59 -0
  257. package/types/redis.ts +27 -0
  258. package/types/redisclient.ts +29 -0
  259. package/types/serializer.ts +38 -0
  260. package/types/stats.ts +100 -0
  261. package/types/stream.ts +75 -0
  262. package/types/telemetry.ts +15 -0
  263. 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 };