@hotmeshio/hotmesh 0.0.55 → 0.0.57

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 (182) hide show
  1. package/README.md +1 -1
  2. package/build/modules/enums.js +1 -10
  3. package/build/modules/key.d.ts +0 -38
  4. package/build/modules/key.js +4 -46
  5. package/build/modules/utils.d.ts +0 -8
  6. package/build/modules/utils.js +0 -14
  7. package/build/package.json +11 -4
  8. package/build/services/activities/activity.d.ts +0 -28
  9. package/build/services/activities/activity.js +1 -46
  10. package/build/services/activities/await.js +0 -4
  11. package/build/services/activities/cycle.d.ts +0 -7
  12. package/build/services/activities/cycle.js +1 -16
  13. package/build/services/activities/hook.d.ts +0 -6
  14. package/build/services/activities/hook.js +2 -12
  15. package/build/services/activities/interrupt.js +0 -8
  16. package/build/services/activities/signal.d.ts +0 -6
  17. package/build/services/activities/signal.js +0 -15
  18. package/build/services/activities/trigger.d.ts +0 -4
  19. package/build/services/activities/trigger.js +1 -7
  20. package/build/services/activities/worker.js +0 -4
  21. package/build/services/collator/index.d.ts +0 -70
  22. package/build/services/collator/index.js +1 -91
  23. package/build/services/compiler/deployer.js +6 -38
  24. package/build/services/compiler/index.d.ts +0 -15
  25. package/build/services/compiler/index.js +0 -20
  26. package/build/services/compiler/validator.d.ts +0 -3
  27. package/build/services/compiler/validator.js +0 -25
  28. package/build/services/connector/clients/ioredis.d.ts +2 -2
  29. package/build/services/connector/clients/ioredis.js +0 -2
  30. package/build/services/connector/clients/redis.d.ts +4 -4
  31. package/build/services/connector/clients/redis.js +1 -3
  32. package/build/services/connector/index.d.ts +1 -1
  33. package/build/services/connector/index.js +0 -2
  34. package/build/services/durable/client.d.ts +1 -26
  35. package/build/services/durable/client.js +0 -56
  36. package/build/services/durable/exporter.d.ts +0 -22
  37. package/build/services/durable/exporter.js +1 -30
  38. package/build/services/durable/handle.d.ts +0 -36
  39. package/build/services/durable/handle.js +0 -46
  40. package/build/services/durable/index.d.ts +0 -4
  41. package/build/services/durable/index.js +0 -4
  42. package/build/services/durable/schemas/factory.d.ts +0 -29
  43. package/build/services/durable/schemas/factory.js +0 -29
  44. package/build/services/durable/search.d.ts +1 -36
  45. package/build/services/durable/search.js +56 -56
  46. package/build/services/durable/worker.js +2 -22
  47. package/build/services/durable/workflow.d.ts +0 -114
  48. package/build/services/durable/workflow.js +1 -141
  49. package/build/services/engine/index.d.ts +1 -6
  50. package/build/services/engine/index.js +1 -43
  51. package/build/services/exporter/index.d.ts +0 -27
  52. package/build/services/exporter/index.js +0 -33
  53. package/build/services/hotmesh/index.d.ts +2 -2
  54. package/build/services/hotmesh/index.js +1 -9
  55. package/build/services/logger/index.js +0 -2
  56. package/build/services/mapper/index.d.ts +0 -14
  57. package/build/services/mapper/index.js +0 -14
  58. package/build/services/pipe/functions/date.d.ts +0 -7
  59. package/build/services/pipe/functions/date.js +0 -7
  60. package/build/services/pipe/functions/math.js +0 -2
  61. package/build/services/pipe/index.d.ts +0 -15
  62. package/build/services/pipe/index.js +2 -23
  63. package/build/services/quorum/index.d.ts +0 -7
  64. package/build/services/quorum/index.js +0 -21
  65. package/build/services/reporter/index.d.ts +0 -5
  66. package/build/services/reporter/index.js +0 -9
  67. package/build/services/router/index.d.ts +0 -9
  68. package/build/services/router/index.js +2 -38
  69. package/build/services/serializer/index.js +7 -26
  70. package/build/services/store/cache.d.ts +0 -18
  71. package/build/services/store/cache.js +0 -18
  72. package/build/services/store/clients/ioredis.d.ts +1 -1
  73. package/build/services/store/clients/ioredis.js +0 -1
  74. package/build/services/store/clients/redis.d.ts +1 -1
  75. package/build/services/store/index.d.ts +0 -55
  76. package/build/services/store/index.js +5 -81
  77. package/build/services/stream/clients/ioredis.d.ts +1 -1
  78. package/build/services/stream/clients/ioredis.js +1 -4
  79. package/build/services/stream/clients/redis.d.ts +1 -1
  80. package/build/services/sub/clients/ioredis.d.ts +1 -1
  81. package/build/services/sub/clients/redis.d.ts +1 -1
  82. package/build/services/task/index.d.ts +0 -9
  83. package/build/services/task/index.js +0 -31
  84. package/build/services/telemetry/index.d.ts +0 -7
  85. package/build/services/telemetry/index.js +1 -13
  86. package/build/services/worker/index.d.ts +0 -4
  87. package/build/services/worker/index.js +2 -6
  88. package/build/types/activity.d.ts +0 -81
  89. package/build/types/durable.d.ts +26 -177
  90. package/build/types/exporter.d.ts +0 -13
  91. package/build/types/hotmesh.d.ts +4 -16
  92. package/build/types/hotmesh.js +0 -3
  93. package/build/types/index.d.ts +4 -6
  94. package/build/types/index.js +4 -3
  95. package/build/types/job.d.ts +1 -86
  96. package/build/types/pipe.d.ts +0 -65
  97. package/build/types/quorum.d.ts +15 -10
  98. package/build/types/redis.d.ts +225 -7
  99. package/build/types/redis.js +9 -0
  100. package/build/types/stream.d.ts +0 -58
  101. package/build/types/stream.js +0 -4
  102. package/package.json +11 -4
  103. package/types/durable.ts +131 -4
  104. package/types/hotmesh.ts +3 -6
  105. package/types/index.ts +23 -10
  106. package/types/job.ts +1 -1
  107. package/types/quorum.ts +22 -0
  108. package/types/redis.ts +267 -18
  109. package/build/types/ioredisclient.d.ts +0 -5
  110. package/build/types/ioredisclient.js +0 -5
  111. package/build/types/redisclient.d.ts +0 -26
  112. package/build/types/redisclient.js +0 -2
  113. package/modules/enums.ts +0 -62
  114. package/modules/errors.ts +0 -280
  115. package/modules/key.ts +0 -101
  116. package/modules/storage.ts +0 -3
  117. package/modules/utils.ts +0 -242
  118. package/services/activities/activity.ts +0 -589
  119. package/services/activities/await.ts +0 -113
  120. package/services/activities/cycle.ts +0 -115
  121. package/services/activities/hook.ts +0 -197
  122. package/services/activities/index.ts +0 -19
  123. package/services/activities/interrupt.ts +0 -172
  124. package/services/activities/signal.ts +0 -148
  125. package/services/activities/trigger.ts +0 -295
  126. package/services/activities/worker.ts +0 -107
  127. package/services/collator/README.md +0 -102
  128. package/services/collator/index.ts +0 -291
  129. package/services/compiler/deployer.ts +0 -504
  130. package/services/compiler/index.ts +0 -98
  131. package/services/compiler/validator.ts +0 -158
  132. package/services/connector/clients/ioredis.ts +0 -57
  133. package/services/connector/clients/redis.ts +0 -72
  134. package/services/connector/index.ts +0 -42
  135. package/services/durable/client.ts +0 -266
  136. package/services/durable/connection.ts +0 -10
  137. package/services/durable/exporter.ts +0 -232
  138. package/services/durable/handle.ts +0 -160
  139. package/services/durable/index.ts +0 -27
  140. package/services/durable/schemas/factory.ts +0 -2358
  141. package/services/durable/search.ts +0 -196
  142. package/services/durable/worker.ts +0 -401
  143. package/services/durable/workflow.ts +0 -557
  144. package/services/engine/index.ts +0 -761
  145. package/services/exporter/index.ts +0 -146
  146. package/services/hotmesh/index.ts +0 -237
  147. package/services/logger/index.ts +0 -79
  148. package/services/mapper/index.ts +0 -89
  149. package/services/pipe/functions/array.ts +0 -78
  150. package/services/pipe/functions/bitwise.ts +0 -27
  151. package/services/pipe/functions/conditional.ts +0 -35
  152. package/services/pipe/functions/date.ts +0 -220
  153. package/services/pipe/functions/index.ts +0 -27
  154. package/services/pipe/functions/json.ts +0 -11
  155. package/services/pipe/functions/logical.ts +0 -11
  156. package/services/pipe/functions/math.ts +0 -217
  157. package/services/pipe/functions/number.ts +0 -75
  158. package/services/pipe/functions/object.ts +0 -98
  159. package/services/pipe/functions/string.ts +0 -86
  160. package/services/pipe/functions/symbol.ts +0 -39
  161. package/services/pipe/functions/unary.ts +0 -19
  162. package/services/pipe/index.ts +0 -216
  163. package/services/quorum/index.ts +0 -319
  164. package/services/reporter/index.ts +0 -387
  165. package/services/router/index.ts +0 -426
  166. package/services/serializer/README.md +0 -10
  167. package/services/serializer/index.ts +0 -285
  168. package/services/store/cache.ts +0 -172
  169. package/services/store/clients/ioredis.ts +0 -145
  170. package/services/store/clients/redis.ts +0 -191
  171. package/services/store/index.ts +0 -1091
  172. package/services/stream/clients/ioredis.ts +0 -157
  173. package/services/stream/clients/redis.ts +0 -158
  174. package/services/stream/index.ts +0 -58
  175. package/services/sub/clients/ioredis.ts +0 -83
  176. package/services/sub/clients/redis.ts +0 -74
  177. package/services/sub/index.ts +0 -25
  178. package/services/task/index.ts +0 -250
  179. package/services/telemetry/index.ts +0 -273
  180. package/services/worker/index.ts +0 -248
  181. package/types/ioredisclient.ts +0 -10
  182. package/types/redisclient.ts +0 -30
@@ -1,504 +0,0 @@
1
- import { KeyStoreParams, KeyType } from '../../modules/key';
2
- import { getSymKey } from '../../modules/utils';
3
- import { CollatorService } from '../collator';
4
- import { SerializerService } from '../serializer';
5
- import { StoreService } from '../store';
6
- import { ActivityType } from '../../types/activity';
7
- import { HookRule } from '../../types/hook';
8
- import { HotMeshGraph, HotMeshManifest } from '../../types/hotmesh';
9
- import { RedisClient, RedisMulti } from '../../types/redis';
10
- import { StringAnyType, Symbols } from '../../types/serializer';
11
- import { Pipe } from '../pipe';
12
- import { Validator } from './validator';
13
-
14
- const DEFAULT_METADATA_RANGE_SIZE = 26; //metadata is 26 slots ([a-z] * 1)
15
- const DEFAULT_DATA_RANGE_SIZE = 260; //data is 260 slots ([a-zA-Z] * 5)
16
- const DEFAULT_RANGE_SIZE = DEFAULT_METADATA_RANGE_SIZE + DEFAULT_DATA_RANGE_SIZE;
17
-
18
- class Deployer {
19
- manifest: HotMeshManifest | null = null;
20
- store: StoreService<RedisClient, RedisMulti> | null;
21
-
22
- constructor(manifest: HotMeshManifest) {
23
- this.manifest = manifest;
24
- }
25
-
26
- async deploy(store: StoreService<RedisClient, RedisMulti>) {
27
- this.store = store;
28
- CollatorService.compile(this.manifest.app.graphs);
29
- this.convertActivitiesToHooks();
30
- this.convertTopicsToTypes();
31
- this.copyJobSchemas();
32
- this.bindBackRefs();
33
- this.bindParents();
34
- this.bindCycleTarget();
35
- this.resolveMappingDependencies();
36
- this.resolveJobMapsPaths();
37
- await this.generateSymKeys();
38
- await this.generateSymVals();
39
- await this.deployHookPatterns();
40
- await this.deployActivitySchemas();
41
- await this.deploySubscriptions();
42
- await this.deployTransitions();
43
- await this.deployConsumerGroups();
44
- }
45
-
46
- getVID() {
47
- return {
48
- id: this.manifest.app.id,
49
- version: this.manifest.app.version,
50
- }
51
- }
52
-
53
- async generateSymKeys() {
54
- //note: symbol ranges are additive (per version); path assignments are immutable
55
- const appId = this.manifest.app.id;
56
- for (const graph of this.manifest.app.graphs) {
57
- //generate JOB symbols
58
- const [,trigger] = this.findTrigger(graph);
59
- const topic = trigger.subscribes;
60
- const [lower, upper, symbols] = await this.store.reserveSymbolRange(`$${topic}`, DEFAULT_RANGE_SIZE, 'JOB');
61
- const prefix = ''; //job meta/data is NOT namespaced
62
- const newSymbols = this.bindSymbols(lower, upper, symbols, prefix, trigger.PRODUCES);
63
- if (Object.keys(newSymbols).length) {
64
- await this.store.addSymbols(`$${topic}`, newSymbols);
65
- }
66
- //generate ACTIVITY symbols
67
- for (const [activityId, activity] of Object.entries(graph.activities)) {
68
- const [lower, upper, symbols] = await this.store.reserveSymbolRange(activityId, DEFAULT_RANGE_SIZE, 'ACTIVITY');
69
- const prefix = `${activityId}/`; //activity meta/data is namespaced
70
- this.bindSelf(activity.consumes, activity.produces, activityId);
71
- const newSymbols = this.bindSymbols(lower, upper, symbols, prefix, activity.produces);
72
- if (Object.keys(newSymbols).length) {
73
- await this.store.addSymbols(activityId, newSymbols);
74
- }
75
- }
76
- }
77
- }
78
-
79
- bindSelf(consumes: Record<string, string[]>, produces: string[], activityId: string) {
80
- //bind self-referential mappings
81
- for (const selfId of [activityId, '$self']) {
82
- const selfConsumes = consumes[selfId];
83
- if (selfConsumes) {
84
- for (const path of selfConsumes) {
85
- if (!produces.includes(path)) {
86
- produces.push(path);
87
- }
88
- }
89
- }
90
- }
91
- }
92
-
93
- bindSymbols(startIndex: number, maxIndex: number, existingSymbols: Symbols, prefix: string, produces: string[]): Symbols {
94
- let newSymbols: Symbols = {};
95
- let currentSymbols: Symbols = {...existingSymbols};
96
- for (let path of produces) {
97
- const fullPath = `${prefix}${path}`;
98
- if (!currentSymbols[fullPath]) {
99
- if (startIndex > maxIndex) {
100
- throw new Error('Symbol index out of bounds');
101
- }
102
- const symbol = getSymKey(startIndex);
103
- startIndex++
104
- newSymbols[fullPath] = symbol;
105
- currentSymbols[fullPath] = symbol; // update the currentSymbols to include this new symbol
106
- }
107
- }
108
- return newSymbols;
109
- }
110
-
111
- copyJobSchemas() {
112
- const graphs = this.manifest!.app.graphs;
113
- for (const graph of graphs) {
114
- const jobSchema = graph.output?.schema;
115
- const outputSchema = graph.input?.schema;
116
- if (!jobSchema && !outputSchema) continue;
117
- const activities = graph.activities;
118
- // Find the trigger activity and bind the job schema to it
119
- // at execution time, the trigger is a standin for the job
120
- for (const activityKey in activities) {
121
- if (activities[activityKey].type === 'trigger') {
122
- const trigger = activities[activityKey];
123
- if (jobSchema) {
124
- //possible for trigger to have job mappings
125
- if (!trigger.job) { trigger.job = {}; }
126
- trigger.job.schema = jobSchema;
127
- }
128
- if (outputSchema) {
129
- //impossible for trigger to have output mappings.
130
- trigger.output = { schema: outputSchema };
131
- }
132
- }
133
- }
134
- }
135
- }
136
-
137
- bindBackRefs() {
138
- for (const graph of this.manifest!.app.graphs) {
139
- const activities = graph.activities;
140
- const triggerId = this.findTrigger(graph)[0];
141
- for (const activityKey in activities) {
142
- activities[activityKey].trigger = triggerId;
143
- activities[activityKey].subscribes = graph.subscribes;
144
- if (graph.publishes) {
145
- activities[activityKey].publishes = graph.publishes;
146
- }
147
- activities[activityKey].expire = graph.expire ?? undefined;
148
- }
149
- }
150
- }
151
-
152
- //the cycle/goto activity includes and ancestor target;
153
- //update with the cycle flag, so it can be rerun
154
- bindCycleTarget() {
155
- for (const graph of this.manifest!.app.graphs) {
156
- const activities = graph.activities;
157
- for (const activityKey in activities) {
158
- const activity = activities[activityKey];
159
- if (activity.type === 'cycle') {
160
- activities[activity.ancestor].cycle = true;
161
- }
162
- }
163
- }
164
- }
165
-
166
- //it's more intuitive for SDK users to use 'topic',
167
- //but the compiler is desiged to be generic and uses the attribute, 'subtypes'
168
- convertTopicsToTypes() {
169
- for (const graph of this.manifest!.app.graphs) {
170
- const activities = graph.activities;
171
- for (const activityKey in activities) {
172
- const activity = activities[activityKey];
173
- if (['worker', 'await'].includes(activity.type) && activity.topic && !activity.subtype) {
174
- activity.subtype = activity.topic;
175
- }
176
- }
177
- }
178
- }
179
-
180
- //legacy; remove at beta (assume no legacy refs to 'activity' at that point)
181
- convertActivitiesToHooks() {
182
- for (const graph of this.manifest!.app.graphs) {
183
- const activities = graph.activities;
184
- for (const activityKey in activities) {
185
- const activity = activities[activityKey];
186
- if (['activity'].includes(activity.type)) {
187
- activity.type = 'hook';
188
- }
189
- }
190
- }
191
- }
192
-
193
-
194
- async bindParents() {
195
- const graphs = this.manifest.app.graphs;
196
- for (const graph of graphs) {
197
- if (graph.transitions) {
198
- for (const fromActivity in graph.transitions) {
199
- const toTransitions = graph.transitions[fromActivity];
200
- for (const transition of toTransitions) {
201
- const to = transition.to;
202
- //DAGs have one parent; easy to optimize for
203
- graph.activities[to].parent = fromActivity;
204
- }
205
- //temporarily bind the transitions to the parent activity,
206
- // so the consumer/producer registrar picks up the bindings
207
- graph.activities[fromActivity].transitions = toTransitions;
208
- }
209
- }
210
- }
211
- }
212
-
213
- collectValues(schema: Record<string, any>, values: Set<string>) {
214
- for (const [key, value] of Object.entries(schema)) {
215
- if (key === 'enum' || key === 'examples' || key === 'default') {
216
- if (Array.isArray(value)) {
217
- for (const v of value) {
218
- if (typeof v === 'string' && v.length > 5) {
219
- values.add(v);
220
- }
221
- }
222
- } else if (typeof value === 'string' && value.length > 5) {
223
- values.add(value);
224
- }
225
- } else if (typeof value === 'object') {
226
- this.collectValues(value, values);
227
- }
228
- }
229
- }
230
-
231
- traverse(obj: any, values: Set<string>) {
232
- for (const value of Object.values(obj)) {
233
- if (typeof value === 'object') {
234
- if ('schema' in value) {
235
- this.collectValues(value.schema, values);
236
- } else {
237
- this.traverse(value, values);
238
- }
239
- }
240
- }
241
- }
242
-
243
- async generateSymVals() {
244
- const uniqueStrings = new Set<string>();
245
- for (const graph of this.manifest!.app.graphs) {
246
- this.traverse(graph, uniqueStrings);
247
- }
248
- const existingSymbols = await this.store.getSymbolValues();
249
- const startIndex = Object.keys(existingSymbols).length;
250
- const maxIndex = Math.pow(52, 2) - 1;
251
- const newSymbols = SerializerService.filterSymVals(startIndex, maxIndex, existingSymbols, uniqueStrings);
252
- await this.store.addSymbolValues(newSymbols);
253
- }
254
-
255
- resolveJobMapsPaths() {
256
- function parsePaths(obj: StringAnyType): string[] {
257
- let result = [];
258
- function traverse(obj: StringAnyType, path = []) {
259
- for (let key in obj) {
260
- if (typeof obj[key] === 'object' && obj[key] !== null && !('@pipe' in obj[key])) {
261
- let newPath = [...path, key];
262
- traverse(obj[key], newPath);
263
- } else {
264
- //wildcard mapping (e.g., 'friends[25]')
265
- //when this is resolved, it will be expanded to
266
- //`'friends/0', ..., 'friends/24'`, providing 25 dynamic
267
- //slots in the flow's output data
268
- const pathName = [...path, key].join('/');
269
- if (!pathName.includes('[')) {
270
- const finalPath = `data/${pathName}`;
271
- if (!result.includes(finalPath)) {
272
- result.push(finalPath);
273
- }
274
- } else {
275
- const [left, right] = pathName.split('[');
276
- //check if this variable isLiteralKeyType (#, -, or _)
277
- const [amount, _] = right.split(']');
278
- if (!isNaN(parseInt(amount))) {
279
- //loop to create all possible paths (0 to amount)
280
- for (let i = 0; i < parseInt(amount); i++) {
281
- const finalPath = `data/${left}/${i}`;
282
- if (!result.includes(finalPath)) {
283
- result.push(finalPath);
284
- }
285
- }
286
- } //else ignore (amount might be '-' or '_') `-` is marker data; `_` is job data;
287
- }
288
- }
289
- }
290
- }
291
- if (obj) {
292
- traverse(obj);
293
- }
294
- return result;
295
- }
296
-
297
- for (const graph of this.manifest.app.graphs) {
298
- let results: string[] = [];
299
- const [, trigger] = this.findTrigger(graph);
300
- for (const activityKey in graph.activities) {
301
- const activity = graph.activities[activityKey];
302
- results = results.concat(parsePaths(activity.job?.maps));
303
- }
304
- trigger.PRODUCES = results;
305
- }
306
- }
307
-
308
- resolveMappingDependencies() {
309
- const dynamicMappingRules: string[] = [];
310
- //recursive function to descend into the object and find all dynamic mapping rules
311
- function traverse(obj: StringAnyType, consumes: string[]): void {
312
- for (const key in obj) {
313
- if (typeof obj[key] === 'string') {
314
- const stringValue = obj[key] as string;
315
- const dynamicMappingRuleMatch = stringValue.match(/^\{[^@].*}$/);
316
- if (dynamicMappingRuleMatch && !Validator.CONTEXT_VARS.includes(stringValue)) {
317
- if (stringValue.split('.')[1] !== 'input') {
318
- dynamicMappingRules.push(stringValue);
319
- consumes.push(stringValue);
320
- }
321
- }
322
- } else if (typeof obj[key] === 'object' && obj[key] !== null) {
323
- traverse(obj[key], consumes);
324
- }
325
- }
326
- }
327
- const graphs = this.manifest.app.graphs;
328
- for (const graph of graphs) {
329
- const activities = graph.activities;
330
- for (const activityId in activities) {
331
- const activity = activities[activityId];
332
- activity.consumes = [];
333
- traverse(activity, activity.consumes);
334
- activity.consumes = this.groupMappingRules(activity.consumes);
335
- }
336
- }
337
- const groupedRules = this.groupMappingRules(dynamicMappingRules);
338
- // Iterate through the graph and add 'produces' field to each activity
339
- for (const graph of graphs) {
340
- const activities = graph.activities;
341
- for (const activityId in activities) {
342
- const activity = activities[activityId];
343
- activity.produces = groupedRules[`${activityId}`] || [];
344
- }
345
- }
346
- }
347
-
348
- groupMappingRules(rules: string[]): Record<string, string[]> {
349
- rules = Array.from(new Set(rules)).sort();
350
- // Group by the first symbol before the period (this is the activity name)
351
- const groupedRules: { [key: string]: string[] } = {};
352
- for (const rule of rules) {
353
- const [group, resolved] = this.resolveMappableValue(rule);
354
- if (!groupedRules[group]) {
355
- groupedRules[group] = [];
356
- }
357
- groupedRules[group].push(resolved);
358
- }
359
- return groupedRules;
360
- }
361
-
362
- resolveMappableValue(mappable: string): [string, string] {
363
- mappable = mappable.substring(1, mappable.length - 1);
364
- const parts = mappable.split('.');
365
- if (parts[0] === '$job') {
366
- const [group, ...path] = parts;
367
- return [group, path.join('/')];
368
- } else {
369
- //normalize paths to be relative to the activity
370
- const [group, type, subtype, ...path] = parts;
371
- const prefix = {
372
- hook: 'hook/data',
373
- input: 'input/data',
374
- output: subtype === 'data' ? 'output/data': 'output/metadata'
375
- }[type];
376
- return [group, `${prefix}/${path.join('/')}`];
377
- }
378
- }
379
-
380
- async deployActivitySchemas() {
381
- const graphs = this.manifest!.app.graphs;
382
- const activitySchemas: Record<string, ActivityType> = {};
383
- for (const graph of graphs) {
384
- const activities = graph.activities;
385
- for (const activityKey in activities) {
386
- const target = activities[activityKey];
387
- //remove transitions; no longer necessary for runtime
388
- delete target.transitions;
389
- activitySchemas[activityKey] = target;
390
- }
391
- }
392
- await this.store.setSchemas(activitySchemas, this.getVID());
393
- }
394
-
395
- async deploySubscriptions() {
396
- const graphs = this.manifest!.app.graphs;
397
- const publicSubscriptions: { [key: string]: string } = {};
398
- for (const graph of graphs) {
399
- const activities = graph.activities;
400
- const subscribesTopic = graph.subscribes;
401
- // Find the activity ID associated with the subscribes topic
402
- for (const activityKey in activities) {
403
- if (activities[activityKey].type === 'trigger') {
404
- publicSubscriptions[subscribesTopic] = activityKey;
405
- break;
406
- }
407
- }
408
- }
409
- await this.store.setSubscriptions(publicSubscriptions, this.getVID());
410
- }
411
-
412
- findTrigger(graph: HotMeshGraph): [string, Record<string, any>] | null {
413
- for (const activityKey in graph.activities) {
414
- const activity = graph.activities[activityKey];
415
- if (activity.type === 'trigger') {
416
- return [activityKey, activity];
417
- }
418
- }
419
- return null;
420
- }
421
-
422
- async deployTransitions() {
423
- const graphs = this.manifest!.app.graphs;
424
- const privateSubscriptions: { [key: string]: any } = {};
425
- for (const graph of graphs) {
426
- if (graph.subscribes && graph.subscribes.startsWith('.')) {
427
- const [triggerId] = this.findTrigger(graph);
428
- if (triggerId) {
429
- privateSubscriptions[graph.subscribes] = { [triggerId]: true };
430
- }
431
- }
432
- if (graph.transitions) {
433
- for (const fromActivity in graph.transitions) {
434
- const toTransitions = graph.transitions[fromActivity];
435
- const toValues: { [key: string]: any } = {};
436
- for (const transition of toTransitions) {
437
- const to = transition.to;
438
- if (transition.conditions) {
439
- toValues[to] = transition.conditions;
440
- } else {
441
- toValues[to] = true;
442
- }
443
- }
444
- if (Object.keys(toValues).length > 0) {
445
- privateSubscriptions['.' + fromActivity] = toValues;
446
- }
447
- }
448
- }
449
- }
450
- await this.store.setTransitions(privateSubscriptions, this.getVID());
451
- }
452
-
453
- async deployHookPatterns() {
454
- const graphs = this.manifest!.app.graphs;
455
- const hookRules: Record<string, HookRule[]> = {};
456
- for (const graph of graphs) {
457
- if (graph.hooks) {
458
- for (const topic in graph.hooks) {
459
- hookRules[topic] = graph.hooks[topic];
460
- const activityId = graph.hooks[topic][0].to;
461
- const targetActivity = graph.activities[activityId];
462
- if (targetActivity) {
463
- if (!targetActivity.hook) {
464
- targetActivity.hook = {};
465
- }
466
- //create back-reference to the hook topic
467
- targetActivity.hook.topic = topic;
468
- }
469
- }
470
- }
471
- }
472
- await this.store.setHookRules(hookRules);
473
- }
474
-
475
- async deployConsumerGroups() {
476
- //create one engine group
477
- const params: KeyStoreParams = { appId: this.manifest.app.id }
478
- const key = this.store.mintKey(KeyType.STREAMS, params);
479
- await this.deployConsumerGroup(key, 'ENGINE');
480
- for (const graph of this.manifest.app.graphs) {
481
- const activities = graph.activities;
482
- for (const activityKey in activities) {
483
- const activity = activities[activityKey];
484
- //only precreate if the topic is concrete and not `mappable`
485
- if (activity.type === 'worker' && Pipe.resolve(activity.subtype, {}) === activity.subtype) {
486
- params.topic = activity.subtype;
487
- const key = this.store.mintKey(KeyType.STREAMS, params);
488
- //create one worker group per unique activity subtype (the topic)
489
- await this.deployConsumerGroup(key, 'WORKER');
490
- }
491
- }
492
- }
493
- }
494
-
495
- async deployConsumerGroup(stream: string, group: string) {
496
- try {
497
- await this.store.xgroup('CREATE', stream, group, '$', 'MKSTREAM');
498
- } catch (err) {
499
- this.store.logger.info('consumer-group-exists', { stream, group });
500
- }
501
- }
502
- }
503
-
504
- export { Deployer };
@@ -1,98 +0,0 @@
1
- import $RefParser from '@apidevtools/json-schema-ref-parser';
2
- import yaml from 'js-yaml';
3
- import * as fs from 'fs/promises';
4
- import * as path from 'path';
5
-
6
- import { ILogger } from '../logger';
7
- import { StoreService } from '../store';
8
- import { Deployer } from './deployer';
9
- import { Validator } from './validator';
10
- import { HotMeshManifest } from '../../types/hotmesh';
11
- import { RedisClient, RedisMulti } from '../../types/redis';
12
-
13
- /**
14
- * The compiler service converts a graph into a executable program.
15
- */
16
- class CompilerService {
17
- store: StoreService<RedisClient, RedisMulti> | null;
18
- logger: ILogger;
19
-
20
- constructor(store: StoreService<RedisClient, RedisMulti>, logger: ILogger) {
21
- this.store = store;
22
- this.logger = logger;
23
- }
24
-
25
- /**
26
- * verifies and plans the deployment of an app to Redis; the app is not deployed yet
27
- * @param path
28
- */
29
- async plan(mySchemaOrPath: string): Promise<HotMeshManifest> {
30
- try {
31
- let schema: HotMeshManifest;
32
- if (this.isPath(mySchemaOrPath)) {
33
- schema = await $RefParser.dereference(mySchemaOrPath) as HotMeshManifest;
34
- } else {
35
- schema = yaml.load(mySchemaOrPath) as HotMeshManifest;
36
- }
37
-
38
- // 1) validate the manifest file
39
- const validator = new Validator(schema);
40
- validator.validate(this.store);
41
-
42
- // 2) todo: add a PlannerService module that will plan the deployment (what might break, drift, etc)
43
- return schema as HotMeshManifest
44
- } catch(err) {
45
- this.logger.error('compiler-plan-error', err);
46
- }
47
- }
48
-
49
- isPath(input: string): boolean {
50
- return !input.trim().startsWith('app:');
51
- }
52
-
53
- /**
54
- * deploys an app to Redis but does NOT activate it.
55
- */
56
- async deploy(mySchemaOrPath: string): Promise<HotMeshManifest> {
57
- try {
58
- let schema: HotMeshManifest;
59
- if (this.isPath(mySchemaOrPath)) {
60
- schema = await $RefParser.dereference(mySchemaOrPath) as HotMeshManifest;
61
- await this.saveAsJSON(mySchemaOrPath, schema);
62
- } else {
63
- schema = yaml.load(mySchemaOrPath) as HotMeshManifest;
64
- }
65
-
66
- // 2) validate the manifest file (synchronous operation...no callbacks)
67
- const validator = new Validator(schema);
68
- validator.validate(this.store);
69
-
70
- // 3) deploy the schema (segment, optimize, etc; save to Redis)
71
- const deployer = new Deployer(schema);
72
- await deployer.deploy(this.store);
73
-
74
- // 4) save the app version to Redis (so it can be activated later)
75
- await this.store.setApp(schema.app.id, schema.app.version);
76
- return schema;
77
- } catch(err) {
78
- this.logger.error('compiler-deploy-error', err);
79
- }
80
- }
81
-
82
- /**
83
- * activates a deployed version of an app;
84
- * @param appId
85
- * @param appVersion
86
- */
87
- async activate(appId: string, appVersion: string): Promise<boolean> {
88
- return await this.store.activateAppVersion(appId, appVersion);
89
- }
90
-
91
- async saveAsJSON(originalPath: string, schema: HotMeshManifest): Promise<void> {
92
- const json = JSON.stringify(schema, null, 2);
93
- const newPath = path.join( path.dirname(originalPath), `.hotmesh.${schema.app.id}.${schema.app.version}.json` );
94
- await fs.writeFile(newPath, json, 'utf8');
95
- }
96
- }
97
-
98
- export { CompilerService };