@temporalio/workflow 0.23.0 → 1.0.0

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 (44) hide show
  1. package/LICENSE.md +1 -1
  2. package/README.md +2 -2
  3. package/lib/cancellation-scope.js +6 -2
  4. package/lib/cancellation-scope.js.map +1 -1
  5. package/lib/errors.js +9 -5
  6. package/lib/errors.js.map +1 -1
  7. package/lib/index.d.ts +18 -12
  8. package/lib/index.js +16 -13
  9. package/lib/index.js.map +1 -1
  10. package/lib/interceptors.d.ts +3 -1
  11. package/lib/interfaces.d.ts +147 -12
  12. package/lib/interfaces.js +44 -0
  13. package/lib/interfaces.js.map +1 -1
  14. package/lib/internals.d.ts +11 -4
  15. package/lib/internals.js +47 -15
  16. package/lib/internals.js.map +1 -1
  17. package/lib/sinks.d.ts +2 -1
  18. package/lib/stack-helpers.d.ts +4 -0
  19. package/lib/stack-helpers.js +15 -0
  20. package/lib/stack-helpers.js.map +1 -0
  21. package/lib/trigger.d.ts +2 -1
  22. package/lib/trigger.js +7 -4
  23. package/lib/trigger.js.map +1 -1
  24. package/lib/worker-interface.d.ts +2 -1
  25. package/lib/worker-interface.js +16 -12
  26. package/lib/worker-interface.js.map +1 -1
  27. package/lib/workflow-handle.d.ts +1 -1
  28. package/lib/workflow.d.ts +115 -43
  29. package/lib/workflow.js +151 -74
  30. package/lib/workflow.js.map +1 -1
  31. package/package.json +10 -8
  32. package/src/alea.ts +88 -0
  33. package/src/cancellation-scope.ts +205 -0
  34. package/src/errors.ts +30 -0
  35. package/src/index.ts +91 -0
  36. package/src/interceptors.ts +239 -0
  37. package/src/interfaces.ts +294 -0
  38. package/src/internals.ts +611 -0
  39. package/src/sinks.ts +41 -0
  40. package/src/stack-helpers.ts +11 -0
  41. package/src/trigger.ts +49 -0
  42. package/src/worker-interface.ts +273 -0
  43. package/src/workflow-handle.ts +63 -0
  44. package/src/workflow.ts +1247 -0
@@ -0,0 +1,294 @@
1
+ import { RetryPolicy, TemporalFailure } from '@temporalio/common';
2
+ import { checkExtends, CommonWorkflowOptions, SearchAttributes } from '@temporalio/internal-workflow-common';
3
+ import type { coresdk } from '@temporalio/proto';
4
+
5
+ /**
6
+ * Workflow Execution information
7
+ */
8
+ export interface WorkflowInfo {
9
+ /**
10
+ * ID of the Workflow, this can be set by the client during Workflow creation.
11
+ * A single Workflow may run multiple times e.g. when scheduled with cron.
12
+ */
13
+ workflowId: string;
14
+
15
+ /**
16
+ * ID of a single Workflow run
17
+ */
18
+ runId: string;
19
+
20
+ /**
21
+ * Workflow function's name
22
+ */
23
+ workflowType: string;
24
+
25
+ /**
26
+ * Indexed information attached to the Workflow Execution
27
+ *
28
+ * This value may change during the lifetime of an Execution.
29
+ */
30
+ searchAttributes: SearchAttributes;
31
+
32
+ /**
33
+ * Non-indexed information attached to the Workflow Execution
34
+ */
35
+ memo?: Record<string, unknown>;
36
+
37
+ /**
38
+ * Parent Workflow info (present if this is a Child Workflow)
39
+ */
40
+ parent?: ParentWorkflowInfo;
41
+
42
+ /**
43
+ * Result from the previous Run (present if this is a Cron Workflow or was Continued As New).
44
+ *
45
+ * An array of values, since other SDKs may return multiple values from a Workflow.
46
+ */
47
+ lastResult?: unknown;
48
+
49
+ /**
50
+ * Failure from the previous Run (present when this Run is a retry, or the last Run of a Cron Workflow failed)
51
+ */
52
+ lastFailure?: TemporalFailure;
53
+
54
+ /**
55
+ * Length of Workflow history up until the current Workflow Task.
56
+ *
57
+ * This value changes during the lifetime of an Execution.
58
+ *
59
+ * You may safely use this information to decide when to {@link continueAsNew}.
60
+ */
61
+ historyLength: number;
62
+
63
+ /**
64
+ * Task queue this Workflow is executing on
65
+ */
66
+ taskQueue: string;
67
+
68
+ /**
69
+ * Namespace this Workflow is executing in
70
+ */
71
+ namespace: string;
72
+
73
+ /**
74
+ * Run Id of the first Run in this Execution Chain
75
+ */
76
+ firstExecutionRunId: string;
77
+
78
+ /**
79
+ * The last Run Id in this Execution Chain
80
+ */
81
+ continuedFromExecutionRunId?: string;
82
+
83
+ // TODO expose from Core
84
+ /**
85
+ * Time at which the Workflow Run started
86
+ */
87
+ // startTime: Date;
88
+
89
+ /**
90
+ * Milliseconds after which the Workflow Execution is automatically terminated by Temporal Server. Set via {@link WorkflowOptions.workflowExecutionTimeout}.
91
+ */
92
+ executionTimeoutMs?: number;
93
+
94
+ /**
95
+ * Time at which the Workflow Execution expires
96
+ */
97
+ executionExpirationTime?: Date;
98
+
99
+ /**
100
+ * Milliseconds after which the Workflow Run is automatically terminated by Temporal Server. Set via {@link WorkflowOptions.workflowRunTimeout}.
101
+ */
102
+ runTimeoutMs?: number;
103
+
104
+ /**
105
+ * Maximum execution time of a Workflow Task in milliseconds. Set via {@link WorkflowOptions.workflowTaskTimeout}.
106
+ */
107
+ taskTimeoutMs: number;
108
+
109
+ /**
110
+ * Retry Policy for this Execution. Set via {@link WorkflowOptions.retry}.
111
+ */
112
+ retryPolicy?: RetryPolicy;
113
+
114
+ /**
115
+ * Starts at 1 and increments for every retry if there is a `retryPolicy`
116
+ */
117
+ attempt: number;
118
+
119
+ /**
120
+ * Cron Schedule for this Execution. Set via {@link WorkflowOptions.cronSchedule}.
121
+ */
122
+ cronSchedule?: string;
123
+
124
+ /**
125
+ * Milliseconds between Cron Runs
126
+ */
127
+ cronScheduleToScheduleInterval?: number;
128
+
129
+ unsafe: UnsafeWorkflowInfo;
130
+ }
131
+
132
+ /**
133
+ * Unsafe information about the current Workflow Execution.
134
+ *
135
+ * Never rely on this information in Workflow logic as it will cause non-deterministic behavior.
136
+ */
137
+ export interface UnsafeWorkflowInfo {
138
+ isReplaying: boolean;
139
+ }
140
+
141
+ export interface ParentWorkflowInfo {
142
+ workflowId: string;
143
+ runId: string;
144
+ namespace: string;
145
+ }
146
+
147
+ /**
148
+ * Not an actual error, used by the Workflow runtime to abort execution when {@link continueAsNew} is called
149
+ */
150
+ export class ContinueAsNew extends Error {
151
+ public readonly name = 'ContinueAsNew';
152
+
153
+ constructor(public readonly command: coresdk.workflow_commands.IContinueAsNewWorkflowExecution) {
154
+ super('Workflow continued as new');
155
+ }
156
+ }
157
+
158
+ /**
159
+ * Options for continuing a Workflow as new
160
+ */
161
+ export interface ContinueAsNewOptions {
162
+ /**
163
+ * A string representing the Workflow type name, e.g. the filename in the Node.js SDK or class name in Java
164
+ */
165
+ workflowType?: string;
166
+ /**
167
+ * Task queue to continue the Workflow in
168
+ */
169
+ taskQueue?: string;
170
+ /**
171
+ * Timeout for the entire Workflow run
172
+ * @format {@link https://www.npmjs.com/package/ms | ms} formatted string
173
+ */
174
+ workflowRunTimeout?: string;
175
+ /**
176
+ * Timeout for a single Workflow task
177
+ * @format {@link https://www.npmjs.com/package/ms | ms} formatted string
178
+ */
179
+ workflowTaskTimeout?: string;
180
+ /**
181
+ * Non-searchable attributes to attach to next Workflow run
182
+ */
183
+ memo?: Record<string, any>;
184
+ /**
185
+ * Searchable attributes to attach to next Workflow run
186
+ */
187
+ searchAttributes?: SearchAttributes;
188
+ }
189
+
190
+ /**
191
+ * Specifies:
192
+ * - whether cancellation requests are sent to the Child
193
+ * - whether and when a {@link CanceledFailure} is thrown from {@link executeChild} or
194
+ * {@link ChildWorkflowHandle.result}
195
+ *
196
+ * @default {@link ChildWorkflowCancellationType.WAIT_CANCELLATION_COMPLETED}
197
+ */
198
+ export enum ChildWorkflowCancellationType {
199
+ /**
200
+ * Don't send a cancellation request to the Child.
201
+ */
202
+ ABANDON = 0,
203
+
204
+ /**
205
+ * Send a cancellation request to the Child. Immediately throw the error.
206
+ */
207
+ TRY_CANCEL = 1,
208
+
209
+ /**
210
+ * Send a cancellation request to the Child. The Child may respect cancellation, in which case an error will be thrown
211
+ * when cancellation has completed, and {@link isCancellation}(error) will be true. On the other hand, the Child may
212
+ * ignore the cancellation request, in which case an error might be thrown with a different cause, or the Child may
213
+ * complete successfully.
214
+ *
215
+ * @default
216
+ */
217
+ WAIT_CANCELLATION_COMPLETED = 2,
218
+
219
+ /**
220
+ * Send a cancellation request to the Child. Throw the error once the Server receives the Child cancellation request.
221
+ */
222
+ WAIT_CANCELLATION_REQUESTED = 3,
223
+ }
224
+
225
+ checkExtends<coresdk.child_workflow.ChildWorkflowCancellationType, ChildWorkflowCancellationType>();
226
+
227
+ /**
228
+ * How a Child Workflow reacts to the Parent Workflow reaching a Closed state.
229
+ *
230
+ * @see {@link https://docs.temporal.io/concepts/what-is-a-parent-close-policy/ | Parent Close Policy}
231
+ */
232
+ export enum ParentClosePolicy {
233
+ /**
234
+ * If a `ParentClosePolicy` is set to this, or is not set at all, the server default value will be used.
235
+ */
236
+ PARENT_CLOSE_POLICY_UNSPECIFIED = 0,
237
+
238
+ /**
239
+ * When the Parent is Closed, the Child is Terminated.
240
+ *
241
+ * @default
242
+ */
243
+ PARENT_CLOSE_POLICY_TERMINATE = 1,
244
+
245
+ /**
246
+ * When the Parent is Closed, nothing is done to the Child.
247
+ */
248
+ PARENT_CLOSE_POLICY_ABANDON = 2,
249
+
250
+ /**
251
+ * When the Parent is Closed, the Child is Cancelled.
252
+ */
253
+ PARENT_CLOSE_POLICY_REQUEST_CANCEL = 3,
254
+ }
255
+
256
+ checkExtends<coresdk.child_workflow.ParentClosePolicy, ParentClosePolicy>();
257
+
258
+ export interface ChildWorkflowOptions extends CommonWorkflowOptions {
259
+ /**
260
+ * Workflow id to use when starting. If not specified a UUID is generated. Note that it is
261
+ * dangerous as in case of client side retries no deduplication will happen based on the
262
+ * generated id. So prefer assigning business meaningful ids if possible.
263
+ */
264
+ workflowId?: string;
265
+
266
+ /**
267
+ * Task queue to use for Workflow tasks. It should match a task queue specified when creating a
268
+ * `Worker` that hosts the Workflow code.
269
+ */
270
+ taskQueue?: string;
271
+
272
+ /**
273
+ * Specifies:
274
+ * - whether cancellation requests are sent to the Child
275
+ * - whether and when an error is thrown from {@link executeChild} or
276
+ * {@link ChildWorkflowHandle.result}
277
+ *
278
+ * @default {@link ChildWorkflowCancellationType.WAIT_CANCELLATION_COMPLETED}
279
+ */
280
+ cancellationType?: ChildWorkflowCancellationType;
281
+
282
+ /**
283
+ * Specifies how the Child reacts to the Parent Workflow reaching a Closed state.
284
+ *
285
+ * @default {@link ParentClosePolicy.PARENT_CLOSE_POLICY_TERMINATE}
286
+ */
287
+ parentClosePolicy?: ParentClosePolicy;
288
+ }
289
+
290
+ export type RequiredChildWorkflowOptions = Required<Pick<ChildWorkflowOptions, 'workflowId' | 'cancellationType'>> & {
291
+ args: unknown[];
292
+ };
293
+
294
+ export type ChildWorkflowOptionsWithDefaults = ChildWorkflowOptions & RequiredChildWorkflowOptions;