@hotmeshio/hotmesh 0.3.29 → 0.3.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.d.ts +8 -2
- package/build/index.js +13 -7
- package/build/modules/enums.d.ts +65 -1
- package/build/modules/enums.js +79 -5
- package/build/modules/key.d.ts +52 -0
- package/build/modules/key.js +1 -1
- package/build/modules/utils.d.ts +73 -0
- package/build/modules/utils.js +1 -1
- package/build/package.json +6 -4
- package/build/services/activities/activity.d.ts +36 -0
- package/build/services/activities/activity.js +1 -1
- package/build/services/activities/await.js +1 -1
- package/build/services/activities/cycle.d.ts +7 -0
- package/build/services/activities/cycle.js +1 -1
- package/build/services/activities/hook.d.ts +6 -0
- package/build/services/activities/hook.js +1 -1
- package/build/services/activities/index.js +1 -1
- package/build/services/activities/interrupt.js +1 -1
- package/build/services/activities/signal.d.ts +6 -0
- package/build/services/activities/signal.js +1 -1
- package/build/services/activities/trigger.d.ts +7 -0
- package/build/services/activities/trigger.js +1 -1
- package/build/services/activities/worker.js +1 -1
- package/build/services/collator/index.d.ts +76 -0
- package/build/services/collator/index.js +1 -1
- package/build/services/compiler/deployer.js +1 -1
- package/build/services/compiler/index.d.ts +15 -0
- package/build/services/compiler/index.js +1 -1
- package/build/services/compiler/validator.d.ts +3 -0
- package/build/services/compiler/validator.js +1 -1
- package/build/services/connector/factory.d.ts +14 -0
- package/build/services/connector/factory.js +22 -2
- package/build/services/connector/index.d.ts +13 -1
- package/build/services/connector/index.js +13 -1
- package/build/services/connector/providers/postgres.js +7 -0
- package/build/services/engine/index.d.ts +162 -0
- package/build/services/engine/index.js +1 -1
- package/build/services/exporter/index.d.ts +27 -0
- package/build/services/exporter/index.js +1 -1
- package/build/services/hotmesh/index.d.ts +280 -0
- package/build/services/hotmesh/index.js +286 -0
- package/build/services/logger/index.js +1 -0
- package/build/services/mapper/index.d.ts +14 -0
- package/build/services/mapper/index.js +1 -1
- package/build/services/meshcall/index.d.ts +171 -0
- package/build/services/meshcall/index.js +190 -0
- package/build/services/meshcall/schemas/factory.d.ts +7 -0
- package/build/services/meshcall/schemas/factory.js +7 -0
- package/build/services/meshdata/index.d.ts +725 -3
- package/build/services/meshdata/index.js +689 -3
- package/build/services/meshflow/client.d.ts +89 -0
- package/build/services/meshflow/client.js +173 -4
- package/build/services/meshflow/connection.d.ts +17 -0
- package/build/services/meshflow/connection.js +17 -0
- package/build/services/meshflow/exporter.d.ts +22 -0
- package/build/services/meshflow/exporter.js +1 -1
- package/build/services/meshflow/handle.d.ts +68 -0
- package/build/services/meshflow/handle.js +75 -0
- package/build/services/meshflow/index.d.ts +92 -0
- package/build/services/meshflow/index.js +92 -0
- package/build/services/meshflow/schemas/factory.d.ts +25 -0
- package/build/services/meshflow/schemas/factory.js +26 -1
- package/build/services/meshflow/search.d.ts +120 -0
- package/build/services/meshflow/search.js +108 -0
- package/build/services/meshflow/worker.d.ts +97 -0
- package/build/services/meshflow/worker.js +116 -2
- package/build/services/meshflow/workflow/all.d.ts +6 -0
- package/build/services/meshflow/workflow/all.js +7 -0
- package/build/services/meshflow/workflow/context.d.ts +4 -0
- package/build/services/meshflow/workflow/context.js +4 -0
- package/build/services/meshflow/workflow/didRun.d.ts +6 -0
- package/build/services/meshflow/workflow/didRun.js +6 -0
- package/build/services/meshflow/workflow/emit.d.ts +7 -0
- package/build/services/meshflow/workflow/emit.js +7 -0
- package/build/services/meshflow/workflow/enrich.d.ts +7 -0
- package/build/services/meshflow/workflow/enrich.js +7 -0
- package/build/services/meshflow/workflow/execChild.d.ts +14 -0
- package/build/services/meshflow/workflow/execChild.js +18 -0
- package/build/services/meshflow/workflow/hook.d.ts +7 -0
- package/build/services/meshflow/workflow/hook.js +7 -0
- package/build/services/meshflow/workflow/index.d.ts +31 -0
- package/build/services/meshflow/workflow/index.js +31 -0
- package/build/services/meshflow/workflow/interrupt.d.ts +7 -0
- package/build/services/meshflow/workflow/interrupt.js +7 -0
- package/build/services/meshflow/workflow/isSideEffectAllowed.d.ts +8 -0
- package/build/services/meshflow/workflow/isSideEffectAllowed.js +8 -0
- package/build/services/meshflow/workflow/proxyActivities.d.ts +14 -0
- package/build/services/meshflow/workflow/proxyActivities.js +18 -3
- package/build/services/meshflow/workflow/random.d.ts +5 -0
- package/build/services/meshflow/workflow/random.js +5 -0
- package/build/services/meshflow/workflow/searchMethods.d.ts +4 -0
- package/build/services/meshflow/workflow/searchMethods.js +4 -0
- package/build/services/meshflow/workflow/signal.d.ts +6 -0
- package/build/services/meshflow/workflow/signal.js +6 -0
- package/build/services/meshflow/workflow/sleepFor.d.ts +7 -0
- package/build/services/meshflow/workflow/sleepFor.js +7 -0
- package/build/services/meshflow/workflow/trace.d.ts +10 -0
- package/build/services/meshflow/workflow/trace.js +10 -0
- package/build/services/meshflow/workflow/waitFor.d.ts +7 -0
- package/build/services/meshflow/workflow/waitFor.js +7 -0
- package/build/services/meshos/index.d.ts +213 -2
- package/build/services/meshos/index.js +221 -1
- package/build/services/pipe/functions/array.js +1 -1
- package/build/services/pipe/functions/bitwise.js +1 -1
- package/build/services/pipe/functions/conditional.js +1 -1
- package/build/services/pipe/functions/cron.d.ts +6 -0
- package/build/services/pipe/functions/cron.js +1 -1
- package/build/services/pipe/functions/date.d.ts +7 -0
- package/build/services/pipe/functions/date.js +1 -1
- package/build/services/pipe/functions/index.js +1 -1
- package/build/services/pipe/functions/json.js +1 -1
- package/build/services/pipe/functions/logical.js +1 -1
- package/build/services/pipe/functions/math.js +1 -1
- package/build/services/pipe/functions/number.js +1 -1
- package/build/services/pipe/functions/object.js +1 -1
- package/build/services/pipe/functions/string.js +1 -1
- package/build/services/pipe/functions/symbol.js +1 -1
- package/build/services/pipe/functions/unary.js +1 -1
- package/build/services/pipe/index.d.ts +15 -0
- package/build/services/pipe/index.js +1 -1
- package/build/services/quorum/index.d.ts +49 -0
- package/build/services/quorum/index.js +1 -1
- package/build/services/reporter/index.d.ts +5 -0
- package/build/services/reporter/index.js +1 -1
- package/build/services/router/index.d.ts +10 -0
- package/build/services/router/index.js +1 -1
- package/build/services/search/providers/postgres/postgres.d.ts +3 -0
- package/build/services/search/providers/postgres/postgres.js +1 -1
- package/build/services/search/providers/redis/ioredis.js +1 -1
- package/build/services/search/providers/redis/redis.js +1 -1
- package/build/services/serializer/index.js +1 -1
- package/build/services/store/cache.d.ts +19 -0
- package/build/services/store/cache.js +19 -0
- package/build/services/store/factory.js +1 -1
- package/build/services/store/providers/postgres/kvsql.d.ts +7 -0
- package/build/services/store/providers/postgres/kvsql.js +1 -1
- package/build/services/store/providers/postgres/kvtables.d.ts +4 -0
- package/build/services/store/providers/postgres/kvtables.js +1 -1
- package/build/services/store/providers/postgres/kvtransaction.js +1 -1
- package/build/services/store/providers/postgres/kvtypes/hash.d.ts +4 -0
- package/build/services/store/providers/postgres/kvtypes/hash.js +1 -1
- package/build/services/store/providers/postgres/kvtypes/list.js +1 -1
- package/build/services/store/providers/postgres/kvtypes/string.js +1 -1
- package/build/services/store/providers/postgres/kvtypes/zset.js +1 -1
- package/build/services/store/providers/postgres/postgres.d.ts +48 -1
- package/build/services/store/providers/postgres/postgres.js +1 -1
- package/build/services/store/providers/redis/_base.d.ts +42 -0
- package/build/services/store/providers/redis/_base.js +1 -1
- package/build/services/store/providers/redis/ioredis.d.ts +8 -0
- package/build/services/store/providers/redis/ioredis.js +1 -1
- package/build/services/store/providers/redis/redis.d.ts +6 -0
- package/build/services/store/providers/redis/redis.js +1 -1
- package/build/services/store/providers/store-initializable.js +1 -1
- package/build/services/stream/factory.js +2 -1
- package/build/services/stream/index.d.ts +4 -0
- package/build/services/stream/providers/nats/nats.js +1 -1
- package/build/services/stream/providers/postgres/kvtables.js +1 -1
- package/build/services/stream/providers/postgres/postgres.d.ts +21 -0
- package/build/services/stream/providers/postgres/postgres.js +1 -1
- package/build/services/stream/providers/redis/ioredis.js +1 -1
- package/build/services/stream/providers/redis/redis.js +1 -1
- package/build/services/stream/providers/stream-initializable.js +1 -1
- package/build/services/sub/providers/nats/nats.js +1 -1
- package/build/services/sub/providers/postgres/postgres.js +1 -1
- package/build/services/sub/providers/redis/ioredis.js +1 -1
- package/build/services/sub/providers/redis/redis.js +1 -1
- package/build/services/task/index.d.ts +9 -0
- package/build/services/task/index.js +1 -1
- package/build/services/telemetry/index.d.ts +7 -0
- package/build/services/telemetry/index.js +1 -1
- package/build/services/worker/index.d.ts +37 -0
- package/build/services/worker/index.js +1 -1
- package/build/types/activity.d.ts +81 -0
- package/build/types/exporter.d.ts +13 -0
- package/build/types/hotmesh.d.ts +144 -0
- package/build/types/hotmesh.js +3 -0
- package/build/types/job.d.ts +101 -0
- package/build/types/manifest.d.ts +8 -0
- package/build/types/meshcall.d.ts +148 -0
- package/build/types/meshdata.d.ts +193 -0
- package/build/types/meshflow.d.ts +297 -0
- package/build/types/nats.d.ts +55 -0
- package/build/types/pipe.d.ts +65 -0
- package/build/types/provider.d.ts +43 -0
- package/build/types/quorum.d.ts +12 -0
- package/build/types/redis.d.ts +6 -0
- package/build/types/stream.d.ts +65 -0
- package/build/types/stream.js +4 -0
- package/index.ts +8 -1
- package/package.json +6 -4
- package/types/manifest.ts +2 -2
- package/types/meshcall.ts +1 -2
- package/types/meshdata.ts +1 -1
|
@@ -8,8 +8,42 @@ const utils_1 = require("../../modules/utils");
|
|
|
8
8
|
const key_1 = require("../../modules/key");
|
|
9
9
|
const cron_1 = require("../pipe/functions/cron");
|
|
10
10
|
const factory_1 = require("./schemas/factory");
|
|
11
|
+
/**
|
|
12
|
+
* MeshCall connects any function as an idempotent endpoint.
|
|
13
|
+
* Call functions from anywhere on the network connected to the
|
|
14
|
+
* target backend (Postgres, Redis/ValKey, NATS, etc). Function
|
|
15
|
+
* responses are cacheable and invocations can be scheduled to
|
|
16
|
+
* run as idempotent cron jobs (this one runs nightly at midnight
|
|
17
|
+
* and uses Postgres as the backend provider).
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { Client as Postgres } from 'pg';
|
|
22
|
+
* import { MeshCall } from '@hotmesh/meshcall';
|
|
23
|
+
*
|
|
24
|
+
* MeshCall.cron({
|
|
25
|
+
* topic: 'my.cron.function',
|
|
26
|
+
* connection: {
|
|
27
|
+
* class: Postgres,
|
|
28
|
+
* options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' }
|
|
29
|
+
* },
|
|
30
|
+
* callback: async () => {
|
|
31
|
+
* //your code here...anything goes
|
|
32
|
+
* },
|
|
33
|
+
* options: { id: 'myDailyCron123', interval: '0 0 * * *' }
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
11
37
|
class MeshCall {
|
|
38
|
+
/**
|
|
39
|
+
* @private
|
|
40
|
+
*/
|
|
12
41
|
constructor() { }
|
|
42
|
+
/**
|
|
43
|
+
* iterates cached worker/engine instances to locate the first match
|
|
44
|
+
* with the provided namespace and connection options
|
|
45
|
+
* @private
|
|
46
|
+
*/
|
|
13
47
|
static async findFirstMatching(targets, namespace = key_1.HMNS, config, options = {}) {
|
|
14
48
|
for (const [id, hotMeshInstance] of targets) {
|
|
15
49
|
const hotMesh = await hotMeshInstance;
|
|
@@ -23,6 +57,9 @@ class MeshCall {
|
|
|
23
57
|
}
|
|
24
58
|
}
|
|
25
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
26
63
|
static async verifyWorkflowActive(hotMesh, appId = key_1.HMNS, count = 0) {
|
|
27
64
|
const app = await hotMesh.engine.store.getApp(appId);
|
|
28
65
|
const appVersion = app?.version;
|
|
@@ -35,6 +72,9 @@ class MeshCall {
|
|
|
35
72
|
}
|
|
36
73
|
return true;
|
|
37
74
|
}
|
|
75
|
+
/**
|
|
76
|
+
* @private
|
|
77
|
+
*/
|
|
38
78
|
static async activateWorkflow(hotMesh, appId = key_1.HMNS, version = factory_1.VERSION) {
|
|
39
79
|
const app = await hotMesh.engine.store.getApp(appId);
|
|
40
80
|
const appVersion = app?.version;
|
|
@@ -62,6 +102,10 @@ class MeshCall {
|
|
|
62
102
|
}
|
|
63
103
|
}
|
|
64
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Returns a cached worker instance or creates a new one
|
|
107
|
+
* @private
|
|
108
|
+
*/
|
|
65
109
|
static async getInstance(namespace, providerConfig, options = {}) {
|
|
66
110
|
let hotMeshInstance;
|
|
67
111
|
if (!options.readonly) {
|
|
@@ -75,11 +119,18 @@ class MeshCall {
|
|
|
75
119
|
}
|
|
76
120
|
return hotMeshInstance;
|
|
77
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* connection re-use is important when making repeated calls, but
|
|
124
|
+
* only if the connection options are an exact match. this method
|
|
125
|
+
* hashes the connection options to ensure that the same connection
|
|
126
|
+
*/
|
|
78
127
|
static hashOptions(connection) {
|
|
79
128
|
if ('options' in connection) {
|
|
129
|
+
//shorthand format
|
|
80
130
|
return (0, utils_1.hashOptions)(connection.options);
|
|
81
131
|
}
|
|
82
132
|
else {
|
|
133
|
+
//longhand format (sub, store, stream, pub, search)
|
|
83
134
|
const response = [];
|
|
84
135
|
for (const p in connection) {
|
|
85
136
|
if (connection[p].options) {
|
|
@@ -89,6 +140,25 @@ class MeshCall {
|
|
|
89
140
|
return response.join('');
|
|
90
141
|
}
|
|
91
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Connects and links a worker function to the mesh
|
|
145
|
+
* @example
|
|
146
|
+
* ```typescript
|
|
147
|
+
* import { Client as Postgres } from 'pg';
|
|
148
|
+
* import { MeshCall } from '@hotmesh/meshcall';
|
|
149
|
+
*
|
|
150
|
+
* MeshCall.connect({
|
|
151
|
+
* topic: 'my.function',
|
|
152
|
+
* connection: {
|
|
153
|
+
* class: Postgres,
|
|
154
|
+
* options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' }
|
|
155
|
+
* },
|
|
156
|
+
* callback: async (arg1: any) => {
|
|
157
|
+
* //your code here...
|
|
158
|
+
* }
|
|
159
|
+
* });
|
|
160
|
+
* ```
|
|
161
|
+
*/
|
|
92
162
|
static async connect(params) {
|
|
93
163
|
const targetNamespace = params.namespace ?? key_1.HMNS;
|
|
94
164
|
const optionsHash = MeshCall.hashOptions(utils_1.polyfill.providerConfig(params));
|
|
@@ -117,6 +187,23 @@ class MeshCall {
|
|
|
117
187
|
await MeshCall.activateWorkflow(hotMeshWorker, targetNamespace);
|
|
118
188
|
return hotMeshWorker;
|
|
119
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* Calls a function and returns the response.
|
|
192
|
+
*
|
|
193
|
+
* @template U - the return type of the linked worker function
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```typescript
|
|
197
|
+
* const response = await MeshCall.exec({
|
|
198
|
+
* topic: 'my.function',
|
|
199
|
+
* args: [{ my: 'args' }],
|
|
200
|
+
* connection: {
|
|
201
|
+
* class: Postgres,
|
|
202
|
+
* options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' }
|
|
203
|
+
* }
|
|
204
|
+
* });
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
120
207
|
static async exec(params) {
|
|
121
208
|
const TOPIC = `${params.namespace ?? key_1.HMNS}.call`;
|
|
122
209
|
const hotMeshInstance = await MeshCall.getInstance(params.namespace, utils_1.polyfill.providerConfig(params));
|
|
@@ -126,13 +213,16 @@ class MeshCall {
|
|
|
126
213
|
await hotMeshInstance.scrub(id);
|
|
127
214
|
}
|
|
128
215
|
else if (params.options?.ttl) {
|
|
216
|
+
//check cache
|
|
129
217
|
try {
|
|
130
218
|
const cached = await hotMeshInstance.getState(TOPIC, id);
|
|
131
219
|
if (cached) {
|
|
220
|
+
//todo: check if present; await if not (subscribe)
|
|
132
221
|
return cached.data.response;
|
|
133
222
|
}
|
|
134
223
|
}
|
|
135
224
|
catch (error) {
|
|
225
|
+
//just swallow error; it means the cache is empty (no doc by that id)
|
|
136
226
|
}
|
|
137
227
|
}
|
|
138
228
|
}
|
|
@@ -146,14 +236,59 @@ class MeshCall {
|
|
|
146
236
|
const jobOutput = await hotMeshInstance.pubsub(TOPIC, { id, expire, topic: params.topic, args: params.args }, null, 30000);
|
|
147
237
|
return jobOutput?.data?.response;
|
|
148
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Clears a cached function response.
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* import { Client as Postgres } from 'pg';
|
|
245
|
+
* import { MeshCall } from '@hotmesh/meshcall';
|
|
246
|
+
*
|
|
247
|
+
* MeshCall.flush({
|
|
248
|
+
* topic: 'my.function',
|
|
249
|
+
* connection: {
|
|
250
|
+
* class: Postgres,
|
|
251
|
+
* options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' }
|
|
252
|
+
* },
|
|
253
|
+
* options: { id: 'myCachedExecFunctionId' }
|
|
254
|
+
* });
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
149
257
|
static async flush(params) {
|
|
150
258
|
const hotMeshInstance = await MeshCall.getInstance(params.namespace, utils_1.polyfill.providerConfig(params));
|
|
151
259
|
await hotMeshInstance.scrub(params.id ?? params?.options?.id);
|
|
152
260
|
}
|
|
261
|
+
/**
|
|
262
|
+
* Schedules a cron job to run at a specified interval
|
|
263
|
+
* with optional args. Provided arguments are passed to the
|
|
264
|
+
* callback function each time the cron job runs. The `id`
|
|
265
|
+
* option is used to uniquely identify the cron job, allowing
|
|
266
|
+
* it to be interrupted at any time.
|
|
267
|
+
*
|
|
268
|
+
* @example
|
|
269
|
+
* ```typescript
|
|
270
|
+
* import { Client as Postgres } from 'pg';
|
|
271
|
+
* import { MeshCall } from '@hotmesh/meshcall';
|
|
272
|
+
*
|
|
273
|
+
* MeshCall.cron({
|
|
274
|
+
* topic: 'my.cron.function',
|
|
275
|
+
* args: ['arg1', 'arg2'], //optionally pass args
|
|
276
|
+
* connection: {
|
|
277
|
+
* class: Postgres,
|
|
278
|
+
* options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' }
|
|
279
|
+
* },
|
|
280
|
+
* callback: async (arg1: any, arg2: any) => {
|
|
281
|
+
* //your code here...
|
|
282
|
+
* },
|
|
283
|
+
* options: { id: 'myDailyCron123', interval: '0 0 * * *' }
|
|
284
|
+
* });
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
153
287
|
static async cron(params) {
|
|
154
288
|
let hotMeshInstance;
|
|
155
289
|
let readonly = true;
|
|
156
290
|
if (params.callback) {
|
|
291
|
+
//always connect cron worker if provided
|
|
157
292
|
hotMeshInstance = await MeshCall.connect({
|
|
158
293
|
logLevel: params.logLevel,
|
|
159
294
|
guid: params.guid,
|
|
@@ -165,13 +300,19 @@ class MeshCall {
|
|
|
165
300
|
readonly = false;
|
|
166
301
|
}
|
|
167
302
|
else {
|
|
303
|
+
//this is a readonly cron connection which means
|
|
304
|
+
//it is only being created to connect as a readonly member
|
|
305
|
+
//of the mesh network that the cron is running on. it
|
|
306
|
+
//can start a job, but it cannot run the job itself in RO mode.
|
|
168
307
|
}
|
|
308
|
+
//configure job inputs
|
|
169
309
|
const TOPIC = `${params.namespace ?? key_1.HMNS}.cron`;
|
|
170
310
|
const maxCycles = params.options.maxCycles ?? 100000;
|
|
171
311
|
let interval = enums_1.HMSH_FIDELITY_SECONDS;
|
|
172
312
|
let delay;
|
|
173
313
|
let cron;
|
|
174
314
|
if ((0, utils_1.isValidCron)(params.options.interval)) {
|
|
315
|
+
//cron syntax
|
|
175
316
|
cron = params.options.interval;
|
|
176
317
|
const nextDelay = new cron_1.CronHandler().nextDelay(cron);
|
|
177
318
|
delay = nextDelay > 0 ? nextDelay : undefined;
|
|
@@ -183,9 +324,11 @@ class MeshCall {
|
|
|
183
324
|
}
|
|
184
325
|
try {
|
|
185
326
|
if (!hotMeshInstance) {
|
|
327
|
+
//get or create a read-only engine instance to start the cron
|
|
186
328
|
hotMeshInstance = await MeshCall.getInstance(params.namespace, utils_1.polyfill.providerConfig(params), { readonly, guid: params.guid });
|
|
187
329
|
await MeshCall.createStream(hotMeshInstance, params.topic, params.namespace);
|
|
188
330
|
}
|
|
331
|
+
//spawn the job (ok if it's a duplicate)
|
|
189
332
|
await hotMeshInstance.pub(TOPIC, {
|
|
190
333
|
id: params.options.id,
|
|
191
334
|
topic: params.topic,
|
|
@@ -204,16 +347,41 @@ class MeshCall {
|
|
|
204
347
|
throw error;
|
|
205
348
|
}
|
|
206
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* Interrupts a running cron job. Returns `true` if the job
|
|
352
|
+
* was successfully interrupted, or `false` if the job was not
|
|
353
|
+
* found.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```typescript
|
|
357
|
+
* import { Client as Postgres } from 'pg';
|
|
358
|
+
* import { MeshCall } from '@hotmesh/meshcall';
|
|
359
|
+
*
|
|
360
|
+
* MeshCall.interrupt({
|
|
361
|
+
* topic: 'my.cron.function',
|
|
362
|
+
* connection: {
|
|
363
|
+
* class: Postgres,
|
|
364
|
+
* options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' }
|
|
365
|
+
* },
|
|
366
|
+
* options: { id: 'myDailyCron123' }
|
|
367
|
+
* });
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
207
370
|
static async interrupt(params) {
|
|
208
371
|
const hotMeshInstance = await MeshCall.getInstance(params.namespace, utils_1.polyfill.providerConfig(params));
|
|
209
372
|
try {
|
|
210
373
|
await hotMeshInstance.interrupt(`${params.namespace ?? key_1.HMNS}.cron`, params.options.id, { throw: false, expire: 1 });
|
|
211
374
|
}
|
|
212
375
|
catch (error) {
|
|
376
|
+
//job doesn't exist; is already stopped
|
|
213
377
|
return false;
|
|
214
378
|
}
|
|
215
379
|
return true;
|
|
216
380
|
}
|
|
381
|
+
/**
|
|
382
|
+
* Shuts down all meshcall instances. Call this method
|
|
383
|
+
* from the SIGTERM handler in your application.
|
|
384
|
+
*/
|
|
217
385
|
static async shutdown() {
|
|
218
386
|
for (const [_, hotMeshInstance] of MeshCall.workers) {
|
|
219
387
|
(await hotMeshInstance).stop();
|
|
@@ -226,10 +394,24 @@ class MeshCall {
|
|
|
226
394
|
}
|
|
227
395
|
exports.MeshCall = MeshCall;
|
|
228
396
|
_a = MeshCall;
|
|
397
|
+
/**
|
|
398
|
+
* @private
|
|
399
|
+
*/
|
|
229
400
|
MeshCall.workers = new Map();
|
|
401
|
+
/**
|
|
402
|
+
* @private
|
|
403
|
+
*/
|
|
230
404
|
MeshCall.engines = new Map();
|
|
405
|
+
/**
|
|
406
|
+
* @private
|
|
407
|
+
*/
|
|
231
408
|
MeshCall.connections = new Map();
|
|
409
|
+
/**
|
|
410
|
+
* @private
|
|
411
|
+
*/
|
|
232
412
|
MeshCall.getHotMeshClient = async (namespace, connection, options = {}) => {
|
|
413
|
+
//namespace isolation requires the connection options to be hashed
|
|
414
|
+
//as multiple intersecting databases can be used by the same service
|
|
233
415
|
const optionsHash = MeshCall.hashOptions(connection);
|
|
234
416
|
const targetNS = namespace ?? key_1.HMNS;
|
|
235
417
|
const connectionNS = `${optionsHash}.${targetNS}`;
|
|
@@ -238,6 +420,7 @@ MeshCall.getHotMeshClient = async (namespace, connection, options = {}) => {
|
|
|
238
420
|
await _a.verifyWorkflowActive(hotMeshClient, targetNS);
|
|
239
421
|
return hotMeshClient;
|
|
240
422
|
}
|
|
423
|
+
//create and cache an instance
|
|
241
424
|
const hotMeshClient = hotmesh_1.HotMesh.init({
|
|
242
425
|
guid: options.guid,
|
|
243
426
|
appId: targetNS,
|
|
@@ -251,6 +434,12 @@ MeshCall.getHotMeshClient = async (namespace, connection, options = {}) => {
|
|
|
251
434
|
await _a.activateWorkflow(await hotMeshClient, targetNS);
|
|
252
435
|
return hotMeshClient;
|
|
253
436
|
};
|
|
437
|
+
/**
|
|
438
|
+
* Creates a stream where messages can be published to ensure there is a
|
|
439
|
+
* channel in place when the message arrives (a race condition for those
|
|
440
|
+
* platforms without implicit topic setup).
|
|
441
|
+
* @private
|
|
442
|
+
*/
|
|
254
443
|
MeshCall.createStream = async (hotMeshClient, workflowTopic, namespace) => {
|
|
255
444
|
const params = { appId: namespace ?? key_1.HMNS, topic: workflowTopic };
|
|
256
445
|
const streamKey = hotMeshClient.engine.store.mintKey(key_1.KeyType.STREAMS, params);
|
|
@@ -258,5 +447,6 @@ MeshCall.createStream = async (hotMeshClient, workflowTopic, namespace) => {
|
|
|
258
447
|
await hotMeshClient.engine.stream.createConsumerGroup(streamKey, 'WORKER');
|
|
259
448
|
}
|
|
260
449
|
catch (err) {
|
|
450
|
+
//ignore if already exists
|
|
261
451
|
}
|
|
262
452
|
};
|
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
export declare const VERSION = "2";
|
|
2
|
+
/**
|
|
3
|
+
* Provides the YAML necessary to create a workflow for the `MeshCall` service.
|
|
4
|
+
* The `appId` parameter is optional and defaults to the HMNS constant ('hmsh').
|
|
5
|
+
*
|
|
6
|
+
* The version is a string as it supports semantic versioning. It is also optional
|
|
7
|
+
* and defaults to '1'.
|
|
8
|
+
*/
|
|
2
9
|
export declare const getWorkflowYAML: (appId?: string, version?: string) => string;
|
|
@@ -3,6 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getWorkflowYAML = exports.VERSION = void 0;
|
|
4
4
|
const key_1 = require("../../../modules/key");
|
|
5
5
|
exports.VERSION = '2';
|
|
6
|
+
/**
|
|
7
|
+
* Provides the YAML necessary to create a workflow for the `MeshCall` service.
|
|
8
|
+
* The `appId` parameter is optional and defaults to the HMNS constant ('hmsh').
|
|
9
|
+
*
|
|
10
|
+
* The version is a string as it supports semantic versioning. It is also optional
|
|
11
|
+
* and defaults to '1'.
|
|
12
|
+
*/
|
|
6
13
|
const getWorkflowYAML = (appId = key_1.HMNS, version = exports.VERSION) => {
|
|
7
14
|
return `app:
|
|
8
15
|
id: ${appId}
|