@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,223 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TelemetryService = void 0;
7
+ const package_json_1 = __importDefault(require("../../package.json"));
8
+ const mapper_1 = require("../mapper");
9
+ const stream_1 = require("../../types/stream");
10
+ const telemetry_1 = require("../../types/telemetry");
11
+ class TelemetryService {
12
+ constructor(appId, config, metadata, context) {
13
+ this.leg = 1;
14
+ this.appId = appId;
15
+ //these are REQUIRED for job and activity spans
16
+ this.config = config;
17
+ this.metadata = metadata;
18
+ this.context = context;
19
+ }
20
+ getJobParentSpanId() {
21
+ return this.context.metadata.spn;
22
+ }
23
+ getActivityParentSpanId(leg) {
24
+ if (leg === 1) {
25
+ return this.context[this.config.parent].output?.metadata?.l2s;
26
+ }
27
+ else {
28
+ return this.context['$self'].output?.metadata?.l1s;
29
+ }
30
+ }
31
+ getTraceId() {
32
+ return this.context.metadata.trc;
33
+ }
34
+ startJobSpan() {
35
+ const spanName = `JOB/${this.appId}/${this.config.subscribes}/1`;
36
+ const traceId = this.getTraceId();
37
+ const spanId = this.getJobParentSpanId();
38
+ const attributes = this.getSpanAttrs(1);
39
+ const span = this.startSpan(traceId, spanId, spanName, attributes);
40
+ this.jobSpan = span;
41
+ this.setTelemetryContext(span, 1);
42
+ return this;
43
+ }
44
+ startActivitySpan(leg = this.leg) {
45
+ const spanName = `${this.config.type.toUpperCase()}/${this.appId}/${this.metadata.aid}/${leg}`;
46
+ const traceId = this.getTraceId();
47
+ const spanId = this.getActivityParentSpanId(leg);
48
+ const attributes = this.getSpanAttrs(leg);
49
+ const span = this.startSpan(traceId, spanId, spanName, attributes);
50
+ this.setTelemetryContext(span, leg);
51
+ this.span = span;
52
+ return this;
53
+ }
54
+ startStreamSpan(data, role) {
55
+ let type;
56
+ if (role === stream_1.StreamRole.SYSTEM) {
57
+ type = 'SYSTEM';
58
+ }
59
+ else if (role === stream_1.StreamRole.WORKER) {
60
+ type = 'EXECUTE';
61
+ }
62
+ else if (data.type === stream_1.StreamDataType.RESULT || data.type === stream_1.StreamDataType.RESPONSE) {
63
+ type = 'FANIN';
64
+ }
65
+ else {
66
+ type = 'FANOUT';
67
+ }
68
+ const topic = data.metadata.topic ? `/${data.metadata.topic}` : '';
69
+ const spanName = `${type}/${this.appId}/${data.metadata.aid}${topic}`;
70
+ const attributes = this.getStreamSpanAttrs(data);
71
+ const span = this.startSpan(data.metadata.trc, data.metadata.spn, spanName, attributes);
72
+ this.span = span;
73
+ return this;
74
+ }
75
+ startSpan(traceId, spanId, spanName, attributes) {
76
+ this.traceId = traceId;
77
+ this.spanId = spanId;
78
+ const tracer = telemetry_1.trace.getTracer(package_json_1.default.name, package_json_1.default.version);
79
+ let parentContext = this.getParentSpanContext();
80
+ const span = tracer.startSpan(spanName, { kind: telemetry_1.SpanKind.CLIENT, attributes, root: !parentContext }, parentContext);
81
+ return span;
82
+ }
83
+ mapActivityAttributes() {
84
+ //export user-defined span attributes (app.activity.data.*)
85
+ if (this.config.telemetry) {
86
+ const telemetryAtts = new mapper_1.MapperService(this.config.telemetry, this.context).mapRules();
87
+ const namespacedAtts = {
88
+ ...Object.keys(telemetryAtts).reduce((result, key) => {
89
+ if (['string', 'boolean', 'number'].includes(typeof telemetryAtts[key])) {
90
+ result[`app.activity.data.${key}`] = telemetryAtts[key];
91
+ }
92
+ return result;
93
+ }, {})
94
+ };
95
+ this.span.setAttributes(namespacedAtts);
96
+ }
97
+ }
98
+ setActivityAttributes(attributes) {
99
+ this.span.setAttributes(attributes);
100
+ }
101
+ setStreamAttributes(attributes) {
102
+ this.span.setAttributes(attributes);
103
+ }
104
+ setJobAttributes(attributes) {
105
+ this.jobSpan.setAttributes(attributes);
106
+ }
107
+ endJobSpan() {
108
+ this.endSpan(this.jobSpan);
109
+ }
110
+ endActivitySpan() {
111
+ this.endSpan(this.span);
112
+ }
113
+ endStreamSpan() {
114
+ this.endSpan(this.span);
115
+ }
116
+ endSpan(span) {
117
+ span && span.end();
118
+ }
119
+ getParentSpanContext() {
120
+ if (this.traceId && this.spanId) {
121
+ const restoredSpanContext = {
122
+ traceId: this.traceId,
123
+ spanId: this.spanId,
124
+ isRemote: true,
125
+ traceFlags: 1, // (todo: revisit sampling strategy/config)
126
+ };
127
+ const parentContext = telemetry_1.trace.setSpanContext(telemetry_1.context.active(), restoredSpanContext);
128
+ return parentContext;
129
+ }
130
+ }
131
+ getSpanAttrs(leg) {
132
+ return {
133
+ ...Object.keys(this.context.metadata).reduce((result, key) => {
134
+ if (key !== 'trc') {
135
+ result[`app.job.${key}`] = this.context.metadata[key];
136
+ }
137
+ return result;
138
+ }, {}),
139
+ ...Object.keys(this.metadata).reduce((result, key) => {
140
+ result[`app.activity.${key}`] = this.metadata[key];
141
+ return result;
142
+ }, {}),
143
+ 'app.activity.leg': leg,
144
+ };
145
+ }
146
+ ;
147
+ getStreamSpanAttrs(input) {
148
+ return {
149
+ ...Object.keys(input.metadata).reduce((result, key) => {
150
+ if (key !== 'trc' && key !== 'spn') {
151
+ result[`app.stream.${key}`] = input.metadata[key];
152
+ }
153
+ return result;
154
+ }, {})
155
+ };
156
+ }
157
+ ;
158
+ setTelemetryContext(span, leg) {
159
+ if (!this.context.metadata.trc) {
160
+ this.context.metadata.trc = span.spanContext().traceId;
161
+ }
162
+ if (leg === 1) {
163
+ if (!this.context['$self'].output.metadata) {
164
+ this.context['$self'].output.metadata = {};
165
+ }
166
+ this.context['$self'].output.metadata.l1s = span.spanContext().spanId;
167
+ }
168
+ else {
169
+ if (!this.context['$self'].output.metadata) {
170
+ this.context['$self'].output.metadata = {};
171
+ }
172
+ this.context['$self'].output.metadata.l2s = span.spanContext().spanId;
173
+ }
174
+ }
175
+ setActivityError(message) {
176
+ this.span.setStatus({ code: telemetry_1.SpanStatusCode.ERROR, message });
177
+ }
178
+ setStreamError(message) {
179
+ this.span.setStatus({ code: telemetry_1.SpanStatusCode.ERROR, message });
180
+ }
181
+ /**
182
+ * Adds the paths (HGET) necessary to restore telemetry state for an activity
183
+ * @param consumes
184
+ * @param config
185
+ * @param metadata
186
+ * @param leg
187
+ */
188
+ static addTargetTelemetryPaths(consumes, config, metadata, leg) {
189
+ if (leg === 1) {
190
+ if (!(config.parent in consumes)) {
191
+ consumes[config.parent] = [];
192
+ }
193
+ consumes[config.parent].push(`${config.parent}/output/metadata/l2s`);
194
+ }
195
+ else {
196
+ if (!(metadata.aid in consumes)) {
197
+ consumes[metadata.aid] = [];
198
+ }
199
+ consumes[metadata.aid].push(`${metadata.aid}/output/metadata/l1s`);
200
+ }
201
+ }
202
+ static bindJobTelemetryToState(state, config, context) {
203
+ if (config.type === 'trigger') {
204
+ state['metadata/trc'] = context.metadata.trc;
205
+ }
206
+ }
207
+ static bindActivityTelemetryToState(state, config, metadata, context, leg) {
208
+ if (config.type === 'trigger') {
209
+ state[`${metadata.aid}/output/metadata/l1s`] = context['$self'].output.metadata.l1s;
210
+ state[`${metadata.aid}/output/metadata/l2s`] = context['$self'].output.metadata.l2s;
211
+ }
212
+ else if (config.type === 'activity' && leg === 1) {
213
+ //activities run non-duplexed and only have a single leg
214
+ state[`${metadata.aid}/output/metadata/l1s`] = context['$self'].output.metadata.l1s;
215
+ state[`${metadata.aid}/output/metadata/l2s`] = context['$self'].output.metadata.l1s;
216
+ }
217
+ else {
218
+ const target = `l${leg}s`;
219
+ state[`${metadata.aid}/output/metadata/${target}`] = context['$self'].output.metadata[target];
220
+ }
221
+ }
222
+ }
223
+ exports.TelemetryService = TelemetryService;
@@ -0,0 +1,30 @@
1
+ import { ILogger } from "../logger";
2
+ import { StreamSignaler } from "../signaler/stream";
3
+ import { StoreService } from '../store';
4
+ import { StreamService } from '../stream';
5
+ import { SubService } from '../sub';
6
+ import { HotMeshConfig, HotMeshWorker } from "../../types/hotmesh";
7
+ import { SubscriptionCallback } from "../../types/quorum";
8
+ import { RedisClient, RedisMulti } from "../../types/redis";
9
+ declare class WorkerService {
10
+ namespace: string;
11
+ appId: string;
12
+ guid: string;
13
+ topic: string;
14
+ config: HotMeshConfig;
15
+ store: StoreService<RedisClient, RedisMulti> | null;
16
+ stream: StreamService<RedisClient, RedisMulti> | null;
17
+ subscribe: SubService<RedisClient, RedisMulti> | null;
18
+ streamSignaler: StreamSignaler | null;
19
+ logger: ILogger;
20
+ reporting: boolean;
21
+ static init(namespace: string, appId: string, guid: string, config: HotMeshConfig, logger: ILogger): Promise<WorkerService[]>;
22
+ verifyWorkerFields(worker: HotMeshWorker): void;
23
+ initStoreChannel(service: WorkerService, store: RedisClient): Promise<void>;
24
+ initSubChannel(service: WorkerService, sub: RedisClient): Promise<void>;
25
+ initStreamChannel(service: WorkerService, stream: RedisClient): Promise<void>;
26
+ initStreamSignaler(worker: HotMeshWorker, logger: ILogger): StreamSignaler;
27
+ subscriptionHandler(): SubscriptionCallback;
28
+ throttle(delayInMillis: number): Promise<void>;
29
+ }
30
+ export { WorkerService };
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WorkerService = void 0;
4
+ const key_1 = require("../../modules/key");
5
+ const stream_1 = require("../signaler/stream");
6
+ const redis_1 = require("../store/clients/redis");
7
+ const ioredis_1 = require("../store/clients/ioredis");
8
+ const redis_2 = require("../stream/clients/redis");
9
+ const ioredis_2 = require("../stream/clients/ioredis");
10
+ const ioredis_3 = require("../sub/clients/ioredis");
11
+ const redis_3 = require("../sub/clients/redis");
12
+ const stream_2 = require("../../types/stream");
13
+ const utils_1 = require("../../modules/utils");
14
+ const connector_1 = require("../connector");
15
+ class WorkerService {
16
+ constructor() {
17
+ this.reporting = false;
18
+ }
19
+ static async init(namespace, appId, guid, config, logger) {
20
+ const services = [];
21
+ if (Array.isArray(config.workers)) {
22
+ for (const worker of config.workers) {
23
+ await connector_1.ConnectorService.initRedisClients(worker.redis?.class, worker.redis?.options, worker);
24
+ const service = new WorkerService();
25
+ service.verifyWorkerFields(worker);
26
+ service.namespace = namespace;
27
+ service.appId = appId;
28
+ service.guid = guid;
29
+ service.topic = worker.topic;
30
+ service.config = config;
31
+ service.logger = logger;
32
+ await service.initStoreChannel(service, worker.store);
33
+ await service.initSubChannel(service, worker.sub);
34
+ await service.subscribe.subscribe(key_1.KeyType.QUORUM, service.subscriptionHandler(), appId);
35
+ await service.subscribe.subscribe(key_1.KeyType.QUORUM, service.subscriptionHandler(), appId, service.topic);
36
+ await service.subscribe.subscribe(key_1.KeyType.QUORUM, service.subscriptionHandler(), appId, service.guid);
37
+ await service.initStreamChannel(service, worker.stream);
38
+ service.streamSignaler = service.initStreamSignaler(worker, logger);
39
+ const key = service.stream.mintKey(key_1.KeyType.STREAMS, { appId: service.appId, topic: worker.topic });
40
+ await service.streamSignaler.consumeMessages(key, 'WORKER', service.guid, worker.callback);
41
+ services.push(service);
42
+ }
43
+ }
44
+ return services;
45
+ }
46
+ verifyWorkerFields(worker) {
47
+ if ((!(0, utils_1.identifyRedisType)(worker.store) ||
48
+ !(0, utils_1.identifyRedisType)(worker.stream) ||
49
+ !(0, utils_1.identifyRedisType)(worker.sub)) ||
50
+ !(worker.topic && worker.callback)) {
51
+ throw new Error('worker must include `store`, `stream`, and `sub` fields along with a callback function and topic.');
52
+ }
53
+ }
54
+ async initStoreChannel(service, store) {
55
+ if ((0, utils_1.identifyRedisType)(store) === 'redis') {
56
+ service.store = new redis_1.RedisStoreService(store);
57
+ }
58
+ else {
59
+ service.store = new ioredis_1.IORedisStoreService(store);
60
+ }
61
+ await service.store.init(service.namespace, service.appId, service.logger);
62
+ }
63
+ async initSubChannel(service, sub) {
64
+ if ((0, utils_1.identifyRedisType)(sub) === 'redis') {
65
+ service.subscribe = new redis_3.RedisSubService(sub);
66
+ }
67
+ else {
68
+ service.subscribe = new ioredis_3.IORedisSubService(sub);
69
+ }
70
+ await service.subscribe.init(service.namespace, service.appId, service.guid, service.logger);
71
+ }
72
+ async initStreamChannel(service, stream) {
73
+ if ((0, utils_1.identifyRedisType)(stream) === 'redis') {
74
+ service.stream = new redis_2.RedisStreamService(stream);
75
+ }
76
+ else {
77
+ service.stream = new ioredis_2.IORedisStreamService(stream);
78
+ }
79
+ await service.stream.init(service.namespace, service.appId, service.logger);
80
+ }
81
+ initStreamSignaler(worker, logger) {
82
+ return new stream_1.StreamSignaler({
83
+ namespace: this.namespace,
84
+ appId: this.appId,
85
+ guid: this.guid,
86
+ role: stream_2.StreamRole.WORKER,
87
+ topic: worker.topic,
88
+ reclaimDelay: worker.reclaimDelay,
89
+ reclaimCount: worker.reclaimCount,
90
+ }, this.stream, this.store, logger);
91
+ }
92
+ subscriptionHandler() {
93
+ const self = this;
94
+ return async (topic, message) => {
95
+ self.logger.debug('worker-event-received', { topic, type: message.type });
96
+ if (message.type === 'throttle') {
97
+ self.throttle(message.throttle);
98
+ }
99
+ };
100
+ }
101
+ async throttle(delayInMillis) {
102
+ this.streamSignaler.setThrottle(delayInMillis);
103
+ }
104
+ }
105
+ exports.WorkerService = WorkerService;
@@ -0,0 +1,86 @@
1
+ import { MetricTypes } from "./stats";
2
+ import { StreamRetryPolicy } from "./stream";
3
+ type ActivityExecutionType = 'trigger' | 'await' | 'worker' | 'activity' | 'emit' | 'iterate';
4
+ type Consumes = Record<string, string[]>;
5
+ interface BaseActivity {
6
+ title?: string;
7
+ type?: ActivityExecutionType;
8
+ subtype?: string;
9
+ input?: Record<string, any>;
10
+ output?: Record<string, any>;
11
+ settings?: Record<string, any>;
12
+ job?: Record<string, any>;
13
+ hook?: Record<string, any>;
14
+ telemetry?: Record<string, any>;
15
+ sleep?: number;
16
+ expire?: number;
17
+ retry?: StreamRetryPolicy;
18
+ collationInt?: number;
19
+ consumes?: Consumes;
20
+ PRODUCES?: string[];
21
+ produces?: string[];
22
+ publishes?: string;
23
+ subscribes?: string;
24
+ trigger?: string;
25
+ parent?: string;
26
+ }
27
+ interface Measure {
28
+ measure: MetricTypes;
29
+ target: string;
30
+ }
31
+ interface TriggerActivityStats {
32
+ id?: {
33
+ [key: string]: unknown;
34
+ } | string;
35
+ key?: {
36
+ [key: string]: unknown;
37
+ } | string;
38
+ measures?: Measure[];
39
+ }
40
+ interface TriggerActivity extends BaseActivity {
41
+ type: 'trigger';
42
+ stats?: TriggerActivityStats;
43
+ }
44
+ interface AwaitActivity extends BaseActivity {
45
+ type: 'await';
46
+ eventName: string;
47
+ timeout: number;
48
+ }
49
+ interface WorkerActivity extends BaseActivity {
50
+ type: 'worker';
51
+ topic: string;
52
+ timeout: number;
53
+ }
54
+ interface EmitActivity extends BaseActivity {
55
+ type: 'emit';
56
+ }
57
+ interface IterateActivity extends BaseActivity {
58
+ type: 'iterate';
59
+ }
60
+ type ActivityType = BaseActivity | TriggerActivity | AwaitActivity | WorkerActivity | EmitActivity | IterateActivity;
61
+ type ActivityData = Record<string, any>;
62
+ type ActivityMetadata = {
63
+ aid: string;
64
+ atp: string;
65
+ stp: string;
66
+ ac: string;
67
+ au: string;
68
+ err?: string;
69
+ l1s?: string;
70
+ l2s?: string;
71
+ dad?: string;
72
+ as?: string;
73
+ };
74
+ type ActivityContext = {
75
+ data?: ActivityData | null;
76
+ metadata: ActivityMetadata;
77
+ hook?: ActivityData;
78
+ };
79
+ type ActivityDuplex = 1 | 2;
80
+ type ActivityDataType = {
81
+ data?: Record<string, unknown>;
82
+ metadata?: Record<string, unknown>;
83
+ hook?: Record<string, unknown>;
84
+ };
85
+ type ActivityLeg = 1 | 2;
86
+ export { ActivityContext, ActivityData, ActivityDataType, ActivityDuplex, ActivityLeg, ActivityMetadata, ActivityType, Consumes, TriggerActivityStats, AwaitActivity, BaseActivity, EmitActivity, IterateActivity, TriggerActivity, WorkerActivity };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,16 @@
1
+ interface App {
2
+ name: string;
3
+ title: string;
4
+ description: string;
5
+ }
6
+ type AppVID = {
7
+ version: string;
8
+ id: string;
9
+ };
10
+ type AppTransitions = {
11
+ [key: string]: Record<string, unknown>;
12
+ };
13
+ type AppSubscriptions = {
14
+ [key: string]: string;
15
+ };
16
+ export { App, AppVID, AppTransitions, AppSubscriptions };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,5 @@
1
+ type AsyncSignal = {
2
+ topic: string;
3
+ jobId: string;
4
+ };
5
+ export { AsyncSignal };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1 @@
1
+ export type CacheMode = 'nocache' | 'cache';
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ export type CollationStage = 'enter' | 'exit' | 'confirm';
2
+ export declare enum CollationFaultType {
3
+ MISSING = "missing",
4
+ DUPLICATE = "duplicate",
5
+ INACTIVE = "inactive",
6
+ INVALID = "invalid",
7
+ FORBIDDEN = "forbidden"
8
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CollationFaultType = void 0;
4
+ var CollationFaultType;
5
+ (function (CollationFaultType) {
6
+ CollationFaultType["MISSING"] = "missing";
7
+ CollationFaultType["DUPLICATE"] = "duplicate";
8
+ CollationFaultType["INACTIVE"] = "inactive";
9
+ CollationFaultType["INVALID"] = "invalid";
10
+ CollationFaultType["FORBIDDEN"] = "forbidden";
11
+ })(CollationFaultType = exports.CollationFaultType || (exports.CollationFaultType = {}));
@@ -0,0 +1,59 @@
1
+ import { RedisClass, RedisOptions } from './redis';
2
+ type WorkflowOptions = {
3
+ taskQueue: string;
4
+ args: any[];
5
+ workflowId: string;
6
+ workflowName?: string;
7
+ workflowTrace?: string;
8
+ workflowSpan?: string;
9
+ };
10
+ type ActivityDataType = {
11
+ activityName: string;
12
+ arguments: any[];
13
+ workflowId: string;
14
+ workflowTopic: string;
15
+ };
16
+ type WorkflowDataType = {
17
+ arguments: any[];
18
+ workflowId: string;
19
+ workflowTopic: string;
20
+ };
21
+ type ConnectionConfig = {
22
+ class: RedisClass;
23
+ options: RedisOptions;
24
+ };
25
+ type Connection = ConnectionConfig;
26
+ type NativeConnection = ConnectionConfig;
27
+ type ClientConfig = {
28
+ connection: Connection;
29
+ };
30
+ type Registry = {
31
+ [key: string]: Function;
32
+ };
33
+ type WorkerConfig = {
34
+ connection: Connection;
35
+ namespace: string;
36
+ taskQueue: string;
37
+ workflowsPath: string;
38
+ activities: {
39
+ [key: string]: Function;
40
+ };
41
+ };
42
+ type ContextType = {
43
+ workflowId: string;
44
+ workflowTopic: string;
45
+ };
46
+ type FunctionSignature<T> = T extends (...args: infer A) => infer R ? (...args: A) => R : never;
47
+ type ProxyType<ACT> = {
48
+ [K in keyof ACT]: FunctionSignature<ACT[K]>;
49
+ };
50
+ type ActivityConfig = {
51
+ startToCloseTimeout: string;
52
+ retryPolicy: {
53
+ initialInterval: string;
54
+ maximumAttempts: number;
55
+ backoffCoefficient: number;
56
+ maximumInterval: string;
57
+ };
58
+ };
59
+ export { ActivityConfig, ActivityDataType, ClientConfig, ContextType, ConnectionConfig, Connection, NativeConnection, ProxyType, Registry, WorkerConfig, WorkflowDataType, WorkflowOptions, };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,31 @@
1
+ interface HookCondition {
2
+ expected: string;
3
+ actual: string;
4
+ }
5
+ declare enum HookGate {
6
+ AND = "and",
7
+ OR = "or"
8
+ }
9
+ interface HookConditions {
10
+ gate?: HookGate;
11
+ match: HookCondition[];
12
+ }
13
+ interface HookRule {
14
+ to: string;
15
+ conditions: HookConditions;
16
+ }
17
+ interface HookRules {
18
+ [eventName: string]: HookRule[];
19
+ }
20
+ type HookSignal = {
21
+ topic: string;
22
+ resolved: string;
23
+ jobId: string;
24
+ };
25
+ interface HookInterface {
26
+ (topic: string, data: {
27
+ [key: string]: any;
28
+ id: string;
29
+ }): Promise<void>;
30
+ }
31
+ export { HookCondition, HookConditions, HookGate, HookInterface, HookRule, HookRules, HookSignal };
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.HookGate = void 0;
4
+ var HookGate;
5
+ (function (HookGate) {
6
+ HookGate["AND"] = "and";
7
+ HookGate["OR"] = "or";
8
+ })(HookGate || (HookGate = {}));
9
+ exports.HookGate = HookGate;