@hotmeshio/hotmesh 0.0.52 → 0.0.54

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 (134) hide show
  1. package/README.md +22 -18
  2. package/build/index.d.ts +1 -2
  3. package/build/index.js +1 -3
  4. package/build/modules/enums.d.ts +8 -3
  5. package/build/modules/enums.js +16 -8
  6. package/build/modules/errors.d.ts +58 -20
  7. package/build/modules/errors.js +90 -33
  8. package/build/package.json +7 -2
  9. package/build/services/activities/activity.d.ts +8 -0
  10. package/build/services/activities/activity.js +63 -14
  11. package/build/services/activities/await.js +6 -6
  12. package/build/services/activities/cycle.d.ts +2 -2
  13. package/build/services/activities/cycle.js +5 -5
  14. package/build/services/activities/hook.js +9 -5
  15. package/build/services/activities/interrupt.d.ts +3 -3
  16. package/build/services/activities/interrupt.js +15 -6
  17. package/build/services/activities/signal.d.ts +2 -2
  18. package/build/services/activities/signal.js +4 -4
  19. package/build/services/activities/trigger.d.ts +5 -2
  20. package/build/services/activities/trigger.js +34 -4
  21. package/build/services/activities/worker.js +6 -6
  22. package/build/services/compiler/deployer.js +33 -5
  23. package/build/services/compiler/validator.d.ts +2 -0
  24. package/build/services/compiler/validator.js +5 -1
  25. package/build/services/durable/client.d.ts +7 -1
  26. package/build/services/durable/client.js +57 -38
  27. package/build/services/durable/exporter.d.ts +27 -81
  28. package/build/services/durable/exporter.js +153 -325
  29. package/build/services/durable/handle.d.ts +13 -8
  30. package/build/services/durable/handle.js +61 -48
  31. package/build/services/durable/index.d.ts +0 -2
  32. package/build/services/durable/index.js +0 -2
  33. package/build/services/durable/schemas/factory.d.ts +33 -0
  34. package/build/services/durable/schemas/factory.js +2356 -0
  35. package/build/services/durable/search.js +8 -8
  36. package/build/services/durable/worker.js +117 -25
  37. package/build/services/durable/workflow.d.ts +67 -52
  38. package/build/services/durable/workflow.js +322 -306
  39. package/build/services/engine/index.d.ts +2 -2
  40. package/build/services/engine/index.js +5 -2
  41. package/build/services/exporter/index.d.ts +2 -4
  42. package/build/services/exporter/index.js +4 -5
  43. package/build/services/hotmesh/index.d.ts +2 -2
  44. package/build/services/hotmesh/index.js +2 -2
  45. package/build/services/mapper/index.d.ts +6 -2
  46. package/build/services/mapper/index.js +6 -2
  47. package/build/services/pipe/functions/array.d.ts +2 -10
  48. package/build/services/pipe/functions/array.js +30 -28
  49. package/build/services/pipe/functions/conditional.d.ts +1 -0
  50. package/build/services/pipe/functions/conditional.js +3 -0
  51. package/build/services/pipe/functions/date.d.ts +1 -0
  52. package/build/services/pipe/functions/date.js +4 -0
  53. package/build/services/pipe/functions/index.d.ts +2 -0
  54. package/build/services/pipe/functions/index.js +2 -0
  55. package/build/services/pipe/functions/logical.d.ts +5 -0
  56. package/build/services/pipe/functions/logical.js +12 -0
  57. package/build/services/pipe/functions/object.d.ts +3 -0
  58. package/build/services/pipe/functions/object.js +25 -7
  59. package/build/services/pipe/index.d.ts +20 -3
  60. package/build/services/pipe/index.js +82 -16
  61. package/build/services/router/index.js +14 -3
  62. package/build/services/serializer/index.d.ts +3 -2
  63. package/build/services/serializer/index.js +11 -4
  64. package/build/services/store/clients/ioredis.js +6 -6
  65. package/build/services/store/clients/redis.js +7 -7
  66. package/build/services/store/index.d.ts +2 -0
  67. package/build/services/store/index.js +4 -1
  68. package/build/services/stream/clients/ioredis.js +8 -8
  69. package/build/services/stream/clients/redis.js +1 -1
  70. package/build/types/activity.d.ts +60 -5
  71. package/build/types/durable.d.ts +183 -36
  72. package/build/types/error.d.ts +48 -0
  73. package/build/types/error.js +2 -0
  74. package/build/types/exporter.d.ts +35 -7
  75. package/build/types/index.d.ts +4 -3
  76. package/build/types/job.d.ts +93 -6
  77. package/build/types/pipe.d.ts +81 -3
  78. package/build/types/stream.d.ts +61 -1
  79. package/build/types/stream.js +4 -0
  80. package/index.ts +1 -2
  81. package/modules/enums.ts +16 -8
  82. package/modules/errors.ts +139 -34
  83. package/package.json +7 -2
  84. package/services/activities/activity.ts +63 -14
  85. package/services/activities/await.ts +6 -6
  86. package/services/activities/cycle.ts +7 -6
  87. package/services/activities/hook.ts +12 -5
  88. package/services/activities/interrupt.ts +19 -9
  89. package/services/activities/signal.ts +6 -5
  90. package/services/activities/trigger.ts +43 -6
  91. package/services/activities/worker.ts +7 -7
  92. package/services/compiler/deployer.ts +33 -6
  93. package/services/compiler/validator.ts +7 -3
  94. package/services/durable/client.ts +49 -22
  95. package/services/durable/exporter.ts +162 -349
  96. package/services/durable/handle.ts +66 -53
  97. package/services/durable/index.ts +0 -2
  98. package/services/durable/schemas/factory.ts +2358 -0
  99. package/services/durable/search.ts +8 -8
  100. package/services/durable/worker.ts +128 -29
  101. package/services/durable/workflow.ts +371 -322
  102. package/services/engine/index.ts +8 -3
  103. package/services/exporter/index.ts +10 -12
  104. package/services/hotmesh/index.ts +4 -3
  105. package/services/mapper/index.ts +6 -2
  106. package/services/pipe/functions/array.ts +24 -37
  107. package/services/pipe/functions/conditional.ts +4 -0
  108. package/services/pipe/functions/date.ts +6 -0
  109. package/services/pipe/functions/index.ts +7 -5
  110. package/services/pipe/functions/logical.ts +11 -0
  111. package/services/pipe/functions/object.ts +26 -7
  112. package/services/pipe/index.ts +99 -21
  113. package/services/quorum/index.ts +1 -3
  114. package/services/router/index.ts +14 -3
  115. package/services/serializer/index.ts +12 -5
  116. package/services/store/clients/ioredis.ts +6 -6
  117. package/services/store/clients/redis.ts +7 -7
  118. package/services/store/index.ts +4 -1
  119. package/services/stream/clients/ioredis.ts +8 -8
  120. package/services/stream/clients/redis.ts +1 -1
  121. package/types/activity.ts +87 -15
  122. package/types/durable.ts +263 -75
  123. package/types/error.ts +52 -0
  124. package/types/exporter.ts +43 -9
  125. package/types/index.ts +14 -8
  126. package/types/job.ts +157 -36
  127. package/types/pipe.ts +84 -3
  128. package/types/stream.ts +82 -23
  129. package/build/services/durable/factory.d.ts +0 -17
  130. package/build/services/durable/factory.js +0 -817
  131. package/build/services/durable/meshos.d.ts +0 -127
  132. package/build/services/durable/meshos.js +0 -380
  133. package/services/durable/factory.ts +0 -818
  134. package/services/durable/meshos.ts +0 -441
@@ -1,18 +1,38 @@
1
+ /** Represents a policy for retrying stream operations based on error codes */
1
2
  export interface StreamRetryPolicy {
2
- [key: string]: [number, 'x'];
3
+ /**
4
+ * Key is error code, value is the retry profile.
5
+ * Tuple contains: [max retry count, retry type].
6
+ * 'x' denotes exponential backoff (default). Only 10, 100, 1000, 10000 are allowed retry intervals.
7
+ */
8
+ [key: string]: [number, 'x'?];
3
9
  }
10
+ /** A 3-digit status code representing the outcome of a stream operation */
4
11
  export type StreamCode = number;
12
+ /** Describes the structure of a stream error */
5
13
  export type StreamError = {
14
+ /** Descriptive message of the error */
6
15
  message: string;
16
+ /** Numeric code corresponding to the type of error */
7
17
  code: number;
18
+ /** Optional job identifier, used when communicating errors externally */
8
19
  job_id?: string;
20
+ /** Stack trace of the error if unhandled */
9
21
  stack?: string;
22
+ /** Name of the error if unhandled */
10
23
  name?: string;
24
+ /** Custom user-defined error details */
11
25
  error?: Record<string, unknown>;
26
+ /** True if originating via a standard transition message with an `error` status */
27
+ is_stream_error?: boolean;
12
28
  };
29
+ /** Enumerated status values for stream operations */
13
30
  export declare enum StreamStatus {
31
+ /** Indicates successful completion of the stream operation */
14
32
  SUCCESS = "success",
33
+ /** Indicates an error occurred during the stream operation */
15
34
  ERROR = "error",
35
+ /** Indicates the stream operation is still pending */
16
36
  PENDING = "pending"
17
37
  }
18
38
  export declare enum StreamDataType {
@@ -26,27 +46,51 @@ export declare enum StreamDataType {
26
46
  SIGNAL = "signal",
27
47
  INTERRUPT = "interrupt"
28
48
  }
49
+ /** Defines the structure of stream data used when passing stream messages (transitions) */
29
50
  export interface StreamData {
51
+ /** Metadata associated with the stream data */
30
52
  metadata: {
53
+ /** Globally unique identifier for the StreamData message to distinguish `retries` from new 'reentry/cycles' */
31
54
  guid: string;
55
+ /** Workflow/job topic */
32
56
  topic?: string;
57
+ /** Workflow/job ID */
33
58
  jid?: string;
59
+ /** Workflow Generational ID (internal GUID) */
34
60
  gid?: string;
61
+ /** Dimensional address indicating the message routing specifics */
35
62
  dad?: string;
63
+ /** Activity ID */
36
64
  aid: string;
65
+ /** OpenTelemetry Trace identifier */
37
66
  trc?: string;
67
+ /** OpenTelemetry Span identifier */
38
68
  spn?: string;
69
+ /** Current try count, used for retry logic */
39
70
  try?: number;
71
+ /**
72
+ * Indicates if the message should wait for a response.
73
+ * If explicitly false, the connection is severed immediately
74
+ * upon verifying (and returning) the Job ID.
75
+ */
40
76
  await?: boolean;
41
77
  };
78
+ /** Type of the data being streamed, optional */
42
79
  type?: StreamDataType;
80
+ /** Actual data being transmitted as a record of key-value pairs */
43
81
  data: Record<string, unknown>;
82
+ /** Policies related to retry logic, optional */
44
83
  policies?: {
45
84
  retry?: StreamRetryPolicy;
46
85
  };
86
+ /** Status of the stream, default assumed as 'success' */
47
87
  status?: StreamStatus;
88
+ /** HTTP-like status code for the stream, default assumed as 200 */
48
89
  code?: number;
90
+ /** Error stack trace */
91
+ stack?: string;
49
92
  }
93
+ /** Extends StreamData for responses, allowing for inheritance of the base properties */
50
94
  export interface StreamDataResponse extends StreamData {
51
95
  }
52
96
  export declare enum StreamRole {
@@ -54,19 +98,35 @@ export declare enum StreamRole {
54
98
  ENGINE = "engine",
55
99
  SYSTEM = "system"
56
100
  }
101
+ /**
102
+ * Represents a type for messages that have been reclaimed from a stream.
103
+ * Each item is a tuple containing a messageId and its details.
104
+ */
57
105
  export type ReclaimedMessageType = [
106
+ /** The stream ID, typically formatted as `<timestamp>-<count>` */
58
107
  messageId: string,
108
+ /** Details of the message, consisting of a key and its value */
59
109
  details: [
110
+ /** Key is always 'message' */
60
111
  key: string,
112
+ /** Value is a stringified representation of StreamData */
61
113
  value: string
62
114
  ]
63
115
  ][];
116
+ /** Configuration parameters for a stream */
64
117
  export type StreamConfig = {
118
+ /** Namespace under which the stream operates */
65
119
  namespace: string;
120
+ /** Application identifier */
66
121
  appId: string;
122
+ /** Globally unique identifier for the stream */
67
123
  guid: string;
124
+ /** Role associated with the stream */
68
125
  role: StreamRole;
126
+ /** Optional topic for the stream */
69
127
  topic?: string;
128
+ /** Delay before a message can be reclaimed, defaults to 60,000 milliseconds */
70
129
  reclaimDelay?: number;
130
+ /** Maximum number of reclaims allowed, defaults to 3. Values greater throw an error */
71
131
  reclaimCount?: number;
72
132
  };
@@ -1,10 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.StreamRole = exports.StreamDataType = exports.StreamStatus = void 0;
4
+ /** Enumerated status values for stream operations */
4
5
  var StreamStatus;
5
6
  (function (StreamStatus) {
7
+ /** Indicates successful completion of the stream operation */
6
8
  StreamStatus["SUCCESS"] = "success";
9
+ /** Indicates an error occurred during the stream operation */
7
10
  StreamStatus["ERROR"] = "error";
11
+ /** Indicates the stream operation is still pending */
8
12
  StreamStatus["PENDING"] = "pending";
9
13
  })(StreamStatus = exports.StreamStatus || (exports.StreamStatus = {}));
10
14
  var StreamDataType;
package/index.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  import { Durable } from './services/durable';
2
- import { MeshOSService as MeshOS } from './services/durable/meshos';
3
2
  import { HotMeshService as HotMesh } from './services/hotmesh';
4
3
  import { HotMeshConfig } from './types/hotmesh';
5
4
 
6
- export { Durable, HotMesh, HotMeshConfig, MeshOS };
5
+ export { Durable, HotMesh, HotMeshConfig };
7
6
  export * as Types from './types';
package/modules/enums.ts CHANGED
@@ -3,7 +3,7 @@ import { LogLevel } from "../types/logger";
3
3
  // HOTMESH SYSTEM
4
4
  export const HMSH_LOGLEVEL = process.env.HMSH_LOGLEVEL as LogLevel || 'info';
5
5
 
6
- // STATUS CODES AND MESSAGES
6
+ // HOTMESH STATUS CODES
7
7
  export const HMSH_CODE_SUCCESS = 200;
8
8
  export const HMSH_CODE_PENDING = 202;
9
9
  export const HMSH_CODE_NOTFOUND = 404;
@@ -11,15 +11,18 @@ export const HMSH_CODE_INTERRUPT = 410;
11
11
  export const HMSH_CODE_UNKNOWN = 500;
12
12
  export const HMSH_CODE_TIMEOUT = 504;
13
13
  export const HMSH_CODE_UNACKED = 999;
14
-
15
- export const HMSH_CODE_DURABLE_SLEEPFOR = 592;
16
- export const HMSH_CODE_DURABLE_INCOMPLETE = 593;
17
- export const HMSH_CODE_DURABLE_WAITFOR = 594;
14
+ // DURABLE STATUS CODES
15
+ export const HMSH_CODE_DURABLE_SLEEP = 588;
16
+ export const HMSH_CODE_DURABLE_ALL = 589;
17
+ export const HMSH_CODE_DURABLE_CHILD = 590;
18
+ export const HMSH_CODE_DURABLE_PROXY = 591;
19
+ export const HMSH_CODE_DURABLE_WAIT = 595;
18
20
  export const HMSH_CODE_DURABLE_TIMEOUT = 596;
19
21
  export const HMSH_CODE_DURABLE_MAXED = 597;
20
22
  export const HMSH_CODE_DURABLE_FATAL = 598;
21
23
  export const HMSH_CODE_DURABLE_RETRYABLE = 599;
22
24
 
25
+ // HOTMESH MESSAGES
23
26
  export const HMSH_STATUS_UNKNOWN = 'unknown';
24
27
 
25
28
  // QUORUM
@@ -36,8 +39,13 @@ export const HMSH_MAX_RETRIES = parseInt(process.env.HMSH_MAX_RETRIES, 10) || 3;
36
39
  export const HMSH_MAX_TIMEOUT_MS = parseInt(process.env.HMSH_MAX_TIMEOUT_MS, 10) || 60000;
37
40
  export const HMSH_GRADUATED_INTERVAL_MS = parseInt(process.env.HMSH_GRADUATED_INTERVAL_MS, 10) || 5000;
38
41
 
39
- const BASE_BLOCK_DURATION = 10000; // Modified for clarity
40
- const TEST_BLOCK_DURATION = 1000; // Modified for clarity
42
+ // DURABLE
43
+ export const HMSH_DURABLE_MAX_ATTEMPTS = 3;
44
+ export const HMSH_DURABLE_MAX_INTERVAL = '120s';
45
+ export const HMSH_DURABLE_EXP_BACKOFF = 10;
46
+
47
+ const BASE_BLOCK_DURATION = 10000;
48
+ const TEST_BLOCK_DURATION = 1000;
41
49
  export const HMSH_BLOCK_TIME_MS = process.env.HMSH_BLOCK_TIME_MS ? parseInt(process.env.HMSH_BLOCK_TIME_MS, 10) : (process.env.NODE_ENV === 'test' ? TEST_BLOCK_DURATION : BASE_BLOCK_DURATION);
42
50
 
43
51
  export const HMSH_XCLAIM_DELAY_MS = parseInt(process.env.HMSH_XCLAIM_DELAY_MS, 10) || 1000 * 60;
@@ -48,7 +56,7 @@ export const HMSH_XPENDING_COUNT = parseInt(process.env.HMSH_XPENDING_COUNT, 10)
48
56
  export const HMSH_EXPIRE_DURATION = parseInt(process.env.HMSH_EXPIRE_DURATION, 10) || 1;
49
57
 
50
58
  const BASE_FIDELITY_SECONDS = 5;
51
- const TEST_FIDELITY_SECONDS = 5;
59
+ const TEST_FIDELITY_SECONDS = 1;
52
60
  export const HMSH_FIDELITY_SECONDS = process.env.HMSH_FIDELITY_SECONDS ? parseInt(process.env.HMSH_FIDELITY_SECONDS, 10) : (process.env.NODE_ENV === 'test' ? TEST_FIDELITY_SECONDS : BASE_FIDELITY_SECONDS);
53
61
 
54
62
  export const HMSH_SCOUT_INTERVAL_SECONDS = parseInt(process.env.HMSH_SCOUT_INTERVAL_SECONDS, 10) || 60;
package/modules/errors.ts CHANGED
@@ -1,14 +1,22 @@
1
- import { ActivityDuplex } from "../types/activity";
2
- import { CollationFaultType, CollationStage } from "../types/collator";
3
1
  import {
4
2
  HMSH_CODE_DURABLE_MAXED,
5
3
  HMSH_CODE_DURABLE_TIMEOUT,
6
4
  HMSH_CODE_DURABLE_FATAL,
7
- HMSH_CODE_DURABLE_INCOMPLETE,
8
5
  HMSH_CODE_NOTFOUND,
9
6
  HMSH_CODE_DURABLE_RETRYABLE,
10
- HMSH_CODE_DURABLE_SLEEPFOR,
11
- HMSH_CODE_DURABLE_WAITFOR } from "./enums";
7
+ HMSH_CODE_DURABLE_WAIT,
8
+ HMSH_CODE_DURABLE_PROXY,
9
+ HMSH_CODE_DURABLE_CHILD,
10
+ HMSH_CODE_DURABLE_ALL,
11
+ HMSH_CODE_DURABLE_SLEEP } from "./enums";
12
+ import { ActivityDuplex } from "../types/activity";
13
+ import { CollationFaultType, CollationStage } from "../types/collator";
14
+ import {
15
+ DurableChildErrorType,
16
+ DurableProxyErrorType,
17
+ DurableSleepErrorType,
18
+ DurableWaitForAllErrorType,
19
+ DurableWaitForErrorType } from "../types/error";
12
20
 
13
21
  class GetStateError extends Error {
14
22
  jobId: string;
@@ -24,65 +32,158 @@ class SetStateError extends Error {
24
32
  }
25
33
  }
26
34
 
27
- //thrown when a signal set is incomplete but already configured
28
- //if a waitFor set has 'n' items, this can be thrown `n - 1` times
29
- class DurableIncompleteSignalError extends Error {
35
+ class DurableWaitForError extends Error {
30
36
  code: number;
31
- constructor(message: string) {
32
- super(message);
33
- this.code = HMSH_CODE_DURABLE_INCOMPLETE;
37
+ signalId: string;
38
+ workflowId: string;
39
+ index: number;
40
+ workflowDimension: string; //hook workflowDimension (e.g., ',0,1,0') (use empty string for `null`)
41
+ constructor(params: DurableWaitForErrorType) {
42
+ super(`WaitFor Interruption`);
43
+ this.signalId = params.signalId;
44
+ this.index = params.index;
45
+ this.workflowDimension = params.workflowDimension;
46
+ this.code = HMSH_CODE_DURABLE_WAIT;
34
47
  }
35
48
  }
36
49
 
37
- //the original waitFor error that is thrown for a new signal set
38
- class DurableWaitForSignalError extends Error {
50
+ class DurableProxyError extends Error {
51
+ activityName: string;
52
+ arguments: string[];
53
+ backoffCoefficient: number;
39
54
  code: number;
40
- signals: {signal: string, index: number}[]; //signal id and execution order in the workflow
41
- constructor(message: string, signals: {signal: string, index: number}[]) {
42
- super(message);
43
- this.signals = signals;
44
- this.code = HMSH_CODE_DURABLE_WAITFOR;
55
+ index: number;
56
+ maximumAttempts: number;
57
+ maximumInterval: number;
58
+ originJobId: string | null;
59
+ parentWorkflowId: string;
60
+ workflowDimension: string;
61
+ workflowId: string;
62
+ workflowTopic: string;
63
+ constructor(params: DurableProxyErrorType) {
64
+ super(`ProxyActivity Interruption`);
65
+ this.arguments = params.arguments;
66
+ this.workflowId = params.workflowId;
67
+ this.workflowTopic = params.workflowTopic;
68
+ this.parentWorkflowId = params.parentWorkflowId;
69
+ this.originJobId = params.originJobId;
70
+ this.index = params.index;
71
+ this.activityName = params.activityName;
72
+ this.workflowDimension = params.workflowDimension;
73
+ this.backoffCoefficient = params.backoffCoefficient;
74
+ this.maximumAttempts = params.maximumAttempts;
75
+ this.maximumInterval = params.maximumInterval;
76
+ this.code = HMSH_CODE_DURABLE_PROXY;
45
77
  }
46
78
  }
47
79
 
48
- class DurableSleepForError extends Error {
80
+ class DurableChildError extends Error {
81
+ await: boolean;
82
+ arguments: string[];
83
+ backoffCoefficient: number;
84
+ code: number;
85
+ workflowDimension: string;
86
+ index: number;
87
+ maximumAttempts: number;
88
+ maximumInterval: number;
89
+ originJobId: string | null;
90
+ parentWorkflowId: string;
91
+ workflowId: string;
92
+ workflowTopic: string;
93
+ constructor(params: DurableChildErrorType) {
94
+ super(`ExecChild Interruption`);
95
+ this.arguments = params.arguments;
96
+ this.workflowId = params.workflowId;
97
+ this.workflowTopic = params.workflowTopic;
98
+ this.parentWorkflowId = params.parentWorkflowId;
99
+ this.originJobId = params.originJobId;
100
+ this.index = params.index;
101
+ this.workflowDimension = params.workflowDimension;
102
+ this.code = HMSH_CODE_DURABLE_CHILD;
103
+ this.await = params.await;
104
+ this.backoffCoefficient = params.backoffCoefficient;
105
+ this.maximumAttempts = params.maximumAttempts;
106
+ this.maximumInterval = params.maximumInterval;
107
+ }
108
+ }
109
+
110
+ class DurableWaitForAllError extends Error {
111
+ items: any[];
112
+ code: number;
113
+ workflowDimension: string;
114
+ size: number;
115
+ index: number;
116
+ originJobId: string | null;
117
+ parentWorkflowId: string;
118
+ workflowId: string;
119
+ workflowTopic: string;
120
+ constructor(params: DurableWaitForAllErrorType) {
121
+ super(`Collation Interruption`);
122
+ this.items = params.items;
123
+ this.size = params.size;
124
+ this.workflowId = params.workflowId;
125
+ this.workflowTopic = params.workflowTopic;
126
+ this.parentWorkflowId = params.parentWorkflowId;
127
+ this.originJobId = params.originJobId;
128
+ this.index = params.index;
129
+ this.workflowDimension = params.workflowDimension;
130
+ this.code = HMSH_CODE_DURABLE_ALL;
131
+ }
132
+ }
133
+
134
+ class DurableSleepError extends Error {
135
+ workflowId: string;
49
136
  code: number;
50
137
  duration: number; //seconds
51
- index: number; //execution order in the workflow
52
- dimension: string; //hook dimension (e.g., ',0,1,0') (uses empty string for `null`)
53
- constructor(message: string, duration: number, index: number, dimension: string) {
54
- super(message);
55
- this.duration = duration;
56
- this.index = index;
57
- this.dimension = dimension;
58
- this.code = HMSH_CODE_DURABLE_SLEEPFOR;
138
+ index: number;
139
+ workflowDimension: string; //empty string for null
140
+ constructor(params: DurableSleepErrorType) {
141
+ super(`SleepFor Interruption`);
142
+ this.duration = params.duration;
143
+ this.workflowId = params.workflowId;
144
+ this.index = params.index;
145
+ this.workflowDimension = params.workflowDimension;
146
+ this.code = HMSH_CODE_DURABLE_SLEEP;
59
147
  }
60
148
  }
149
+
61
150
  class DurableTimeoutError extends Error {
62
151
  code: number;
63
- constructor(message: string) {
152
+ constructor(message: string, stack?: string) {
64
153
  super(message);
154
+ if (this.stack) {
155
+ this.stack = stack;
156
+ }
65
157
  this.code = HMSH_CODE_DURABLE_TIMEOUT;
66
158
  }
67
159
  }
68
160
  class DurableMaxedError extends Error {
69
161
  code: number;
70
- constructor(message: string) {
162
+ constructor(message: string, stackTrace?: string) {
71
163
  super(message);
164
+ if (stackTrace) {
165
+ this.stack = stackTrace;
166
+ }
72
167
  this.code = HMSH_CODE_DURABLE_MAXED;
73
168
  }
74
169
  }
75
170
  class DurableFatalError extends Error {
76
171
  code: number;
77
- constructor(message: string) {
172
+ constructor(message: string, stackTrace?: string) {
78
173
  super(message);
174
+ if (stackTrace) {
175
+ this.stack = stackTrace;
176
+ }
79
177
  this.code = HMSH_CODE_DURABLE_FATAL;
80
178
  }
81
179
  }
82
180
  class DurableRetryError extends Error {
83
181
  code: number;
84
- constructor(message: string) {
182
+ constructor(message: string, stackTrace?: string) {
85
183
  super(message);
184
+ if (stackTrace) {
185
+ this.stack = stackTrace;
186
+ }
86
187
  this.code = HMSH_CODE_DURABLE_RETRYABLE;
87
188
  }
88
189
  }
@@ -100,8 +201,10 @@ class RegisterTimeoutError extends Error {
100
201
  }
101
202
 
102
203
  class DuplicateJobError extends Error {
204
+ jobId: string;
103
205
  constructor(jobId: string) {
104
206
  super("Duplicate job");
207
+ this.jobId = jobId;
105
208
  this.message = `Duplicate job: ${jobId}`;
106
209
  }
107
210
  }
@@ -157,13 +260,15 @@ class CollationError extends Error {
157
260
 
158
261
  export {
159
262
  CollationError,
263
+ DurableChildError,
160
264
  DurableFatalError,
161
- DurableIncompleteSignalError,
162
265
  DurableMaxedError,
266
+ DurableProxyError,
163
267
  DurableRetryError,
164
- DurableSleepForError,
268
+ DurableSleepError,
165
269
  DurableTimeoutError,
166
- DurableWaitForSignalError,
270
+ DurableWaitForAllError,
271
+ DurableWaitForError,
167
272
  DuplicateJobError,
168
273
  ExecActivityError,
169
274
  GenerationalError,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hotmeshio/hotmesh",
3
- "version": "0.0.52",
3
+ "version": "0.0.54",
4
4
  "description": "Unbreakable Workflows",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -21,13 +21,16 @@
21
21
  "start": "ts-node src/index.ts",
22
22
  "test": "NODE_ENV=test jest --detectOpenHandles --forceExit --verbose",
23
23
  "test:hmsh": "NODE_ENV=test jest ./tests/functional/index.test.ts --detectOpenHandles --verbose",
24
+ "test:pipe": "NODE_ENV=test jest ./tests/unit/services/pipe/index.test.ts --detectOpenHandles --forceExit --verbose",
24
25
  "test:compile": "NODE_ENV=test jest ./tests/functional/compile/index.test.ts --detectOpenHandles --forceExit --verbose",
25
26
  "test:cycle": "NODE_ENV=test jest ./tests/functional/cycle/index.test.ts --detectOpenHandles --forceExit --verbose",
26
27
  "test:trigger": "NODE_ENV=test jest ./tests/unit/services/activities/trigger.test.ts --detectOpenHandles --forceExit --verbose",
27
28
  "test:connect": "NODE_ENV=test jest ./tests/unit/services/connector/index.test.ts --detectOpenHandles --forceExit --verbose",
28
29
  "test:connect:redis": "NODE_ENV=test jest ./tests/unit/services/connector/clients/redis.test.ts --detectOpenHandles --forceExit --verbose",
29
30
  "test:connect:ioredis": "NODE_ENV=test jest ./tests/unit/services/connector/clients/ioredis.test.ts --detectOpenHandles --forceExit --verbose",
31
+ "test:functional": "NODE_ENV=test jest ./tests/functional/*/index.test.ts --detectOpenHandles --forceExit --verbose",
30
32
  "test:emit": "NODE_ENV=test jest ./tests/functional/emit/index.test.ts --detectOpenHandles --forceExit --verbose",
33
+ "test:reentrant": "NODE_ENV=test jest ./tests/functional/reentrant/index.test.ts --detectOpenHandles --forceExit --verbose",
31
34
  "test:await": "NODE_ENV=test jest ./tests/functional/awaiter/index.test.ts --detectOpenHandles --forceExit --verbose",
32
35
  "test:hook": "NODE_ENV=test jest ./tests/functional/hook/index.test.ts --detectOpenHandles --forceExit --verbose",
33
36
  "test:signal": "NODE_ENV=test jest ./tests/functional/signal/index.test.ts --detectOpenHandles --forceExit --verbose",
@@ -46,13 +49,15 @@
46
49
  "test:sub:redis": "NODE_ENV=test jest ./tests/functional/sub/clients/redis.test.ts --detectOpenHandles --forceExit --verbose",
47
50
  "test:sub:ioredis": "NODE_ENV=test jest ./tests/functional/sub/clients/ioredis.test.ts --detectOpenHandles --forceExit --verbose",
48
51
  "test:durable": "NODE_ENV=test jest ./tests/durable/*/index.test.ts --detectOpenHandles --forceExit --verbose",
49
- "test:durable:meshos": "NODE_ENV=test jest ./tests/durable/meshos/index.test.ts --detectOpenHandles --forceExit --verbose",
52
+ "test:durable:basic": "NODE_ENV=test jest ./tests/durable/basic/index.test.ts --detectOpenHandles --forceExit --verbose",
53
+ "test:durable:collision": "NODE_ENV=test jest ./tests/durable/collision/index.test.ts --detectOpenHandles --forceExit --verbose",
50
54
  "test:durable:hello": "NODE_ENV=test jest ./tests/durable/helloworld/index.test.ts --detectOpenHandles --forceExit --verbose",
51
55
  "test:durable:interrupt": "NODE_ENV=test jest ./tests/durable/interrupt/index.test.ts --detectOpenHandles --forceExit --verbose",
52
56
  "test:durable:goodbye": "NODE_ENV=test jest ./tests/durable/goodbye/index.test.ts --detectOpenHandles --forceExit --verbose",
53
57
  "test:durable:hook": "NODE_ENV=test jest ./tests/durable/hook/index.test.ts --detectOpenHandles --forceExit --verbose",
54
58
  "test:durable:retry": "NODE_ENV=test jest ./tests/durable/retry/index.test.ts --detectOpenHandles --forceExit --verbose",
55
59
  "test:durable:fatal": "NODE_ENV=test jest ./tests/durable/fatal/index.test.ts --detectOpenHandles --forceExit --verbose",
60
+ "test:durable:unknown": "NODE_ENV=test jest ./tests/durable/unknown/index.test.ts --detectOpenHandles --forceExit --verbose",
56
61
  "test:durable:sleep": "NODE_ENV=test jest ./tests/durable/sleep/index.test.ts --detectOpenHandles --forceExit --verbose",
57
62
  "test:durable:signal": "NODE_ENV=test jest ./tests/durable/signal/index.test.ts --detectOpenHandles --forceExit --verbose",
58
63
  "test:durable:loopactivity": "NODE_ENV=test jest ./tests/durable/loopactivity/index.test.ts --detectOpenHandles --forceExit --verbose",
@@ -143,19 +143,19 @@ class Activity {
143
143
  this.transitionAdjacent(multiResponse, telemetry);
144
144
  } catch (error) {
145
145
  if (error instanceof CollationError) {
146
- this.logger.info('process-event-inactive-error', { error });
146
+ this.logger.info('process-event-inactive-error', { ...error });
147
147
  return;
148
148
  } else if (error instanceof InactiveJobError) {
149
- this.logger.info('process-event-inactive-job-error', { error });
149
+ this.logger.info('process-event-inactive-job-error', { ...error });
150
150
  return;
151
151
  } else if (error instanceof GenerationalError) {
152
- this.logger.info('process-event-generational-job-error', { error });
152
+ this.logger.info('process-event-generational-job-error', { ...error });
153
153
  return;
154
154
  } else if (error instanceof GetStateError) {
155
- this.logger.info('process-event-get-job-error', { error });
155
+ this.logger.info('process-event-get-job-error', { ...error });
156
156
  return;
157
157
  }
158
- this.logger.error('activity-process-event-error', { error });
158
+ this.logger.error('activity-process-event-error', { ...error, message: error.message, stack: error.stack, name: error.name });
159
159
  telemetry && telemetry.setActivityError(error.message);
160
160
  throw error;
161
161
  } finally {
@@ -191,6 +191,10 @@ class Activity {
191
191
  async processError(telemetry: TelemetryService, type: string): Promise<MultiResponseFlags> {
192
192
  this.bindActivityError(this.data);
193
193
  this.adjacencyList = await this.filterAdjacent();
194
+ if (!this.adjacencyList.length) {
195
+ this.bindJobError(this.data);
196
+ }
197
+ this.mapJobData();
194
198
  const multi = this.store.getMulti();
195
199
  await this.setState(multi);
196
200
  await CollatorService.notarizeCompletion(this, multi);
@@ -205,7 +209,7 @@ class Activity {
205
209
  const attrs: StringScalarType = { 'app.job.jss': jobStatus };
206
210
  //adjacencyList membership has already been set at this point (according to activity status)
207
211
  const messageIds = await this.transition(this.adjacencyList, jobStatus);
208
- if (messageIds.length) {
212
+ if (messageIds?.length) {
209
213
  attrs['app.activity.mids'] = messageIds.join(',')
210
214
  }
211
215
  telemetry.setActivityAttributes(attrs);
@@ -223,7 +227,30 @@ class Activity {
223
227
  mapJobData(): void {
224
228
  if(this.config.job?.maps) {
225
229
  const mapper = new MapperService(this.config.job.maps, this.context);
226
- this.context.data = mapper.mapRules();
230
+ const output = mapper.mapRules();
231
+ if (output) {
232
+ for (const key in output) {
233
+ const f1 = key.indexOf('[');
234
+ //keys with array notation suffix `somekey[]` represent
235
+ //dynamically-keyed mappings whose `value` must be moved to the output.
236
+ //The `value` must be an object with keys appropriate to the
237
+ //notation type: `somekey[0] (array)`, `somekey[-] (mark)`, OR `somekey[_] (search)`
238
+ if (f1 > -1) {
239
+ const amount = key.substring(f1 + 1).split(']')[0];
240
+ if (!isNaN(Number(amount))) {
241
+ const left = key.substring(0, f1);
242
+ output[left] = output[key];
243
+ delete output[key];
244
+ } else if (amount === '-' || amount === '_') {
245
+ const obj = output[key];
246
+ Object.keys(obj).forEach((newKey) => {
247
+ output[newKey] = obj[newKey];
248
+ });
249
+ }
250
+ }
251
+ }
252
+ }
253
+ this.context.data = output;
227
254
  }
228
255
  }
229
256
 
@@ -249,10 +276,22 @@ class Activity {
249
276
  //set timeout in support of hook and/or duplex
250
277
  }
251
278
 
279
+ /**
280
+ * Any StreamMessage with a status of ERROR is bound to the activity
281
+ */
252
282
  bindActivityError(data: Record<string, unknown>): void {
253
- //todo: map activity error data into the job error (if defined)
254
- // map job status via: (500: [3**, 4**, 5**], 202: [$pending])
255
- this.context.metadata.err = JSON.stringify(data);
283
+ const md = this.context[this.metadata.aid].output.metadata;
284
+ md.err = JSON.stringify(this.data);
285
+ //(temporary...useful for mapping error parts in the app.yaml)
286
+ md.$error = { ...data, is_stream_error: true };
287
+ }
288
+
289
+ /**
290
+ * unhandled activity errors (activities that return an ERROR StreamMessage
291
+ * status and have no adjacent children to transition to) are bound to the job
292
+ */
293
+ bindJobError(data: Record<string, unknown>): void {
294
+ this.context.metadata.err = JSON.stringify({ ...data, is_stream_error: true });
256
295
  }
257
296
 
258
297
  async getTriggerConfig(): Promise<ActivityType> {
@@ -322,9 +361,9 @@ class Activity {
322
361
  if (this.status === StreamStatus.ERROR) {
323
362
  self.output.metadata.err = JSON.stringify(this.data);
324
363
  }
325
- //todo: verify leg2 never overwrites leg1 `ac`
326
- self.output.metadata.ac =
327
- self.output.metadata.au = formatISODate(new Date());
364
+ const ts = formatISODate(new Date());
365
+ self.output.metadata.ac = ts;
366
+ self.output.metadata.au = ts;
328
367
  self.output.metadata.atp = this.config.type;
329
368
  if (this.config.subtype) {
330
369
  self.output.metadata.stp = this.config.subtype;
@@ -344,6 +383,11 @@ class Activity {
344
383
  state[path] = value;
345
384
  }
346
385
  }
386
+ for (let key in this.context?.data ?? {}) {
387
+ if (key.startsWith('-') || key.startsWith('_')) {
388
+ state[key] = this.context.data[key];
389
+ }
390
+ }
347
391
  TelemetryService.bindJobTelemetryToState(state, this.config, this.context);
348
392
  }
349
393
 
@@ -398,7 +442,7 @@ class Activity {
398
442
  let { dad, jid } = this.context.metadata;
399
443
  const dIds = CollatorService.getDimensionsById([...this.config.ancestors, this.metadata.aid], dad || '');
400
444
  //`state` is a unidimensional hash; context is a tree
401
- const [state, status] = await this.store.getState(jid, consumes, dIds);
445
+ const [state, _status] = await this.store.getState(jid, consumes, dIds);
402
446
  this.context = restoreHierarchy(state) as JobState;
403
447
  this.assertGenerationalId(this.context?.metadata?.gid, gid);
404
448
  this.initDimensionalAddress(dad);
@@ -443,6 +487,11 @@ class Activity {
443
487
  if (!self.hook) {
444
488
  self.hook = { };
445
489
  }
490
+ if (!self.output.metadata) {
491
+ self.output.metadata = { };
492
+ }
493
+ //prebind the updated timestamp (mappings need the time)
494
+ self.output.metadata.au = formatISODate(new Date());
446
495
  context['$self'] = self;
447
496
  context['$job'] = context; //NEVER call STRINGIFY! (now circular)
448
497
  return context as JobState;