@temporalio/activity 1.8.6 → 1.9.0-rc.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.
- package/lib/index.d.ts +102 -38
- package/lib/index.js +142 -46
- package/lib/index.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +156 -49
package/lib/index.d.ts
CHANGED
|
@@ -99,77 +99,77 @@ export declare const asyncLocalStorage: AsyncLocalStorage<Context>;
|
|
|
99
99
|
* Holds information about the current Activity Execution. Retrieved inside an Activity with `Context.current().info`.
|
|
100
100
|
*/
|
|
101
101
|
export interface Info {
|
|
102
|
-
taskToken: Uint8Array;
|
|
102
|
+
readonly taskToken: Uint8Array;
|
|
103
103
|
/**
|
|
104
104
|
* Base64 encoded `taskToken`
|
|
105
105
|
*/
|
|
106
|
-
base64TaskToken: string;
|
|
107
|
-
activityId: string;
|
|
106
|
+
readonly base64TaskToken: string;
|
|
107
|
+
readonly activityId: string;
|
|
108
108
|
/**
|
|
109
109
|
* Exposed Activity function name
|
|
110
110
|
*/
|
|
111
|
-
activityType: string;
|
|
111
|
+
readonly activityType: string;
|
|
112
112
|
/**
|
|
113
113
|
* The namespace this Activity is running in
|
|
114
114
|
*/
|
|
115
|
-
activityNamespace: string;
|
|
115
|
+
readonly activityNamespace: string;
|
|
116
116
|
/**
|
|
117
117
|
* Attempt number for this activity
|
|
118
118
|
*/
|
|
119
|
-
attempt: number;
|
|
119
|
+
readonly attempt: number;
|
|
120
120
|
/**
|
|
121
121
|
* Whether this activity is scheduled in local or remote mode
|
|
122
122
|
*/
|
|
123
|
-
isLocal: boolean;
|
|
123
|
+
readonly isLocal: boolean;
|
|
124
124
|
/**
|
|
125
125
|
* Information about the Workflow that scheduled the Activity
|
|
126
126
|
*/
|
|
127
|
-
workflowExecution: {
|
|
128
|
-
workflowId: string;
|
|
129
|
-
runId: string;
|
|
127
|
+
readonly workflowExecution: {
|
|
128
|
+
readonly workflowId: string;
|
|
129
|
+
readonly runId: string;
|
|
130
130
|
};
|
|
131
131
|
/**
|
|
132
132
|
* The namespace of the Workflow that scheduled this Activity
|
|
133
133
|
*/
|
|
134
|
-
workflowNamespace: string;
|
|
134
|
+
readonly workflowNamespace: string;
|
|
135
135
|
/**
|
|
136
136
|
* The module name of the Workflow that scheduled this Activity
|
|
137
137
|
*/
|
|
138
|
-
workflowType: string;
|
|
138
|
+
readonly workflowType: string;
|
|
139
139
|
/**
|
|
140
140
|
* Timestamp for when this Activity was scheduled in milliseconds
|
|
141
141
|
*/
|
|
142
|
-
scheduledTimestampMs: number;
|
|
142
|
+
readonly scheduledTimestampMs: number;
|
|
143
143
|
/**
|
|
144
144
|
* Timeout for this Activity from schedule to close in milliseconds.
|
|
145
145
|
*/
|
|
146
|
-
scheduleToCloseTimeoutMs: number;
|
|
146
|
+
readonly scheduleToCloseTimeoutMs: number;
|
|
147
147
|
/**
|
|
148
148
|
* Timeout for this Activity from start to close in milliseconds
|
|
149
149
|
*/
|
|
150
|
-
startToCloseTimeoutMs: number;
|
|
150
|
+
readonly startToCloseTimeoutMs: number;
|
|
151
151
|
/**
|
|
152
152
|
* Timestamp for when the current attempt of this Activity was scheduled in milliseconds
|
|
153
153
|
*/
|
|
154
|
-
currentAttemptScheduledTimestampMs: number;
|
|
154
|
+
readonly currentAttemptScheduledTimestampMs: number;
|
|
155
155
|
/**
|
|
156
156
|
* Heartbeat timeout in milliseconds.
|
|
157
157
|
* If this timeout is defined, the Activity must heartbeat before the timeout is reached.
|
|
158
158
|
* The Activity must **not** heartbeat in case this timeout is not defined.
|
|
159
159
|
*/
|
|
160
|
-
heartbeatTimeoutMs?: number;
|
|
160
|
+
readonly heartbeatTimeoutMs?: number;
|
|
161
161
|
/**
|
|
162
162
|
* The {@link Context.heartbeat | details} from the last recorded heartbeat from the last attempt of this Activity.
|
|
163
163
|
*
|
|
164
164
|
* Use this to resume your Activity from a checkpoint.
|
|
165
165
|
*/
|
|
166
|
-
heartbeatDetails: any;
|
|
166
|
+
readonly heartbeatDetails: any;
|
|
167
167
|
/**
|
|
168
168
|
* Task Queue the Activity is scheduled in.
|
|
169
169
|
*
|
|
170
170
|
* For Local Activities, this is set to the Workflow's Task Queue.
|
|
171
171
|
*/
|
|
172
|
-
taskQueue: string;
|
|
172
|
+
readonly taskQueue: string;
|
|
173
173
|
}
|
|
174
174
|
/**
|
|
175
175
|
* Activity Context, used to:
|
|
@@ -182,14 +182,21 @@ export interface Info {
|
|
|
182
182
|
* Call `Context.current()` from Activity code in order to get the current Activity's Context.
|
|
183
183
|
*/
|
|
184
184
|
export declare class Context {
|
|
185
|
+
/**
|
|
186
|
+
* Gets the context of the current Activity.
|
|
187
|
+
*
|
|
188
|
+
* Uses {@link https://nodejs.org/docs/latest-v14.x/api/async_hooks.html#async_hooks_class_asynclocalstorage | AsyncLocalStorage} under the hood to make it accessible in nested callbacks and promises.
|
|
189
|
+
*/
|
|
190
|
+
static current(): Context;
|
|
185
191
|
/**
|
|
186
192
|
* Holds information about the current executing Activity.
|
|
187
193
|
*/
|
|
188
|
-
info: Info;
|
|
194
|
+
readonly info: Info;
|
|
189
195
|
/**
|
|
190
|
-
*
|
|
196
|
+
* A Promise that fails with a {@link CancelledFailure} when cancellation of this activity is requested. The promise
|
|
197
|
+
* is guaranteed to never successfully resolve. Await this promise in an Activity to get notified of cancellation.
|
|
191
198
|
*
|
|
192
|
-
*
|
|
199
|
+
* Note that to get notified of cancellation, an activity must _also_ {@link Context.heartbeat}.
|
|
193
200
|
*
|
|
194
201
|
* @see [Cancellation](/api/namespaces/activity#cancellation)
|
|
195
202
|
*/
|
|
@@ -204,6 +211,8 @@ export declare class Context {
|
|
|
204
211
|
* {@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options child_process}
|
|
205
212
|
* to abort a child process, as well as other built-in node modules and modules found on npm.
|
|
206
213
|
*
|
|
214
|
+
* Note that to get notified of cancellation, an activity must _also_ {@link Context.heartbeat}.
|
|
215
|
+
*
|
|
207
216
|
* @see [Cancellation](/api/namespaces/activity#cancellation)
|
|
208
217
|
*/
|
|
209
218
|
readonly cancellationSignal: AbortSignal;
|
|
@@ -214,14 +223,17 @@ export declare class Context {
|
|
|
214
223
|
/**
|
|
215
224
|
* The logger for this Activity.
|
|
216
225
|
*
|
|
217
|
-
* This defaults to the `Runtime`'s Logger (see {@link Runtime.logger}).
|
|
218
|
-
*
|
|
219
|
-
*
|
|
220
|
-
*
|
|
226
|
+
* This defaults to the `Runtime`'s Logger (see {@link Runtime.logger}). Attributes from the current Activity context
|
|
227
|
+
* will automatically be included as metadata on every log entries, and some key events of the Activity's lifecycle
|
|
228
|
+
* will automatically be logged (at 'DEBUG' level for most messages; 'WARN' for failures).
|
|
229
|
+
*
|
|
230
|
+
* To customize log attributes, register a {@link ActivityOutboundCallsInterceptor} that intercepts the
|
|
231
|
+
* `getLogAttributes()` method.
|
|
221
232
|
*
|
|
222
|
-
*
|
|
223
|
-
*
|
|
224
|
-
* customize
|
|
233
|
+
* Modifying the context logger (eg. `context.log = myCustomLogger` or by an {@link ActivityInboundLogInterceptor}
|
|
234
|
+
* with a custom logger as argument) is deprecated. Doing so will prevent automatic inclusion of custom log attributes
|
|
235
|
+
* through the `getLogAttributes()` interceptor. To customize _where_ log messages are sent, set the
|
|
236
|
+
* {@link Runtime.logger} property instead.
|
|
225
237
|
*/
|
|
226
238
|
log: Logger;
|
|
227
239
|
/**
|
|
@@ -229,7 +241,7 @@ export declare class Context {
|
|
|
229
241
|
*
|
|
230
242
|
* @ignore
|
|
231
243
|
*/
|
|
232
|
-
constructor(info: Info, cancelled: Promise<never>, cancellationSignal: AbortSignal, heartbeat: (details?: any) => void,
|
|
244
|
+
constructor(info: Info, cancelled: Promise<never>, cancellationSignal: AbortSignal, heartbeat: (details?: any) => void, log: Logger);
|
|
233
245
|
/**
|
|
234
246
|
* Send a {@link https://docs.temporal.io/concepts/what-is-an-activity-heartbeat | heartbeat} from an Activity.
|
|
235
247
|
*
|
|
@@ -251,17 +263,69 @@ export declare class Context {
|
|
|
251
263
|
* :warning: Cancellation is not propagated from this function, use {@link cancelled} or {@link cancellationSignal} to
|
|
252
264
|
* subscribe to cancellation notifications.
|
|
253
265
|
*/
|
|
254
|
-
heartbeat(details?: unknown)
|
|
255
|
-
/**
|
|
256
|
-
* Gets the context of the current Activity.
|
|
257
|
-
*
|
|
258
|
-
* Uses {@link https://nodejs.org/docs/latest-v14.x/api/async_hooks.html#async_hooks_class_asynclocalstorage | AsyncLocalStorage} under the hood to make it accessible in nested callbacks and promises.
|
|
259
|
-
*/
|
|
260
|
-
static current(): Context;
|
|
266
|
+
readonly heartbeat: (details?: unknown) => void;
|
|
261
267
|
/**
|
|
262
268
|
* Helper function for sleeping in an Activity.
|
|
263
269
|
* @param ms Sleep duration: number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
|
|
264
270
|
* @returns A Promise that either resolves when `ms` is reached or rejects when the Activity is cancelled
|
|
265
271
|
*/
|
|
266
|
-
sleep(ms: Duration)
|
|
272
|
+
readonly sleep: (ms: Duration) => Promise<void>;
|
|
267
273
|
}
|
|
274
|
+
/**
|
|
275
|
+
* The current Activity's context.
|
|
276
|
+
*/
|
|
277
|
+
export declare function activityInfo(): Info;
|
|
278
|
+
/**
|
|
279
|
+
* The logger for this Activity.
|
|
280
|
+
*
|
|
281
|
+
* This is a shortcut for `Context.current().log` (see {@link Context.log}).
|
|
282
|
+
*/
|
|
283
|
+
export declare const log: Logger;
|
|
284
|
+
/**
|
|
285
|
+
* Helper function for sleeping in an Activity.
|
|
286
|
+
*
|
|
287
|
+
* This is a shortcut for `Context.current().sleep(ms)` (see {@link Context.sleep}).
|
|
288
|
+
*
|
|
289
|
+
* @param ms Sleep duration: number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
|
|
290
|
+
* @returns A Promise that either resolves when `ms` is reached or rejects when the Activity is cancelled
|
|
291
|
+
*/
|
|
292
|
+
export declare function sleep(ms: Duration): Promise<void>;
|
|
293
|
+
/**
|
|
294
|
+
* Send a {@link https://docs.temporal.io/concepts/what-is-an-activity-heartbeat | heartbeat} from an Activity.
|
|
295
|
+
*
|
|
296
|
+
* If an Activity times out, then during the next retry, the last value of `details` is available at
|
|
297
|
+
* {@link Info.heartbeatDetails}. This acts as a periodic checkpoint mechanism for the progress of an Activity.
|
|
298
|
+
*
|
|
299
|
+
* If an Activity times out on the final retry (relevant in cases in which {@link RetryPolicy.maximumAttempts} is
|
|
300
|
+
* set), the Activity function call in the Workflow code will throw an {@link ActivityFailure} with the `cause`
|
|
301
|
+
* attribute set to a {@link TimeoutFailure}, which has the last value of `details` available at
|
|
302
|
+
* {@link TimeoutFailure.lastHeartbeatDetails}.
|
|
303
|
+
*
|
|
304
|
+
* This is a shortcut for `Context.current().heatbeat(ms)` (see {@link Context.heartbeat}).
|
|
305
|
+
*/
|
|
306
|
+
export declare function heartbeat(details?: unknown): void;
|
|
307
|
+
/**
|
|
308
|
+
* Return a Promise that fails with a {@link CancelledFailure} when cancellation of this activity is requested. The
|
|
309
|
+
* promise is guaranteed to never successfully resolve. Await this promise in an Activity to get notified of
|
|
310
|
+
* cancellation.
|
|
311
|
+
*
|
|
312
|
+
* Note that to get notified of cancellation, an activity must _also_ do {@link Context.heartbeat}.
|
|
313
|
+
*
|
|
314
|
+
* This is a shortcut for `Context.current().cancelled` (see {@link Context.cancelled}).
|
|
315
|
+
*/
|
|
316
|
+
export declare function cancelled(): Promise<never>;
|
|
317
|
+
/**
|
|
318
|
+
* Return an {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal | `AbortSignal`} that can be used to
|
|
319
|
+
* react to Activity cancellation.
|
|
320
|
+
*
|
|
321
|
+
* This can be passed in to libraries such as
|
|
322
|
+
* {@link https://www.npmjs.com/package/node-fetch#request-cancellation-with-abortsignal | fetch} to abort an
|
|
323
|
+
* in-progress request and
|
|
324
|
+
* {@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options child_process}
|
|
325
|
+
* to abort a child process, as well as other built-in node modules and modules found on npm.
|
|
326
|
+
*
|
|
327
|
+
* Note that to get notified of cancellation, an activity must _also_ do {@link Context.heartbeat}.
|
|
328
|
+
*
|
|
329
|
+
* This is a shortcut for `Context.current().cancellationSignal` (see {@link Context.cancellationSignal}).
|
|
330
|
+
*/
|
|
331
|
+
export declare function cancellationSignal(): AbortSignal;
|
package/lib/index.js
CHANGED
|
@@ -76,7 +76,8 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
76
76
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
77
77
|
};
|
|
78
78
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
79
|
-
exports.Context = exports.asyncLocalStorage = exports.CompleteAsyncError = exports.CancelledFailure = exports.ApplicationFailure = void 0;
|
|
79
|
+
exports.cancellationSignal = exports.cancelled = exports.heartbeat = exports.sleep = exports.log = exports.activityInfo = exports.Context = exports.asyncLocalStorage = exports.CompleteAsyncError = exports.CancelledFailure = exports.ApplicationFailure = void 0;
|
|
80
|
+
// Keep this around until we drop support for Node 14.
|
|
80
81
|
require("abort-controller/polyfill"); // eslint-disable-line import/no-unassigned-import
|
|
81
82
|
const node_async_hooks_1 = require("node:async_hooks");
|
|
82
83
|
const time_1 = require("@temporalio/common/lib/time");
|
|
@@ -124,42 +125,6 @@ exports.asyncLocalStorage = globalThis[asyncLocalStorageSymbol];
|
|
|
124
125
|
* Call `Context.current()` from Activity code in order to get the current Activity's Context.
|
|
125
126
|
*/
|
|
126
127
|
class Context {
|
|
127
|
-
/**
|
|
128
|
-
* **Not** meant to instantiated by Activity code, used by the worker.
|
|
129
|
-
*
|
|
130
|
-
* @ignore
|
|
131
|
-
*/
|
|
132
|
-
constructor(info, cancelled, cancellationSignal, heartbeat, logger) {
|
|
133
|
-
this.info = info;
|
|
134
|
-
this.cancelled = cancelled;
|
|
135
|
-
this.cancellationSignal = cancellationSignal;
|
|
136
|
-
this.heartbeatFn = heartbeat;
|
|
137
|
-
this.log = logger;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Send a {@link https://docs.temporal.io/concepts/what-is-an-activity-heartbeat | heartbeat} from an Activity.
|
|
141
|
-
*
|
|
142
|
-
* If an Activity times out, then during the next retry, the last value of `details` is available at
|
|
143
|
-
* {@link Info.heartbeatDetails}. This acts as a periodic checkpoint mechanism for the progress of an Activity.
|
|
144
|
-
*
|
|
145
|
-
* If an Activity times out on the final retry (relevant in cases in which {@link RetryPolicy.maximumAttempts} is
|
|
146
|
-
* set), the Activity function call in the Workflow code will throw an {@link ActivityFailure} with the `cause`
|
|
147
|
-
* attribute set to a {@link TimeoutFailure}, which has the last value of `details` available at
|
|
148
|
-
* {@link TimeoutFailure.lastHeartbeatDetails}.
|
|
149
|
-
*
|
|
150
|
-
* Calling `heartbeat()` from a Local Activity has no effect.
|
|
151
|
-
*
|
|
152
|
-
* The SDK automatically throttles heartbeat calls to the server with a duration of 80% of the specified activity
|
|
153
|
-
* heartbeat timeout. Throttling behavior may be customized with the `{@link maxHeartbeatThrottleInterval | https://typescript.temporal.io/api/interfaces/worker.WorkerOptions#maxheartbeatthrottleinterval} and {@link defaultHeartbeatThrottleInterval | https://typescript.temporal.io/api/interfaces/worker.WorkerOptions#defaultheartbeatthrottleinterval} worker options.
|
|
154
|
-
*
|
|
155
|
-
* Activities must heartbeat in order to receive Cancellation (unless they're Local Activities, which don't need to).
|
|
156
|
-
*
|
|
157
|
-
* :warning: Cancellation is not propagated from this function, use {@link cancelled} or {@link cancellationSignal} to
|
|
158
|
-
* subscribe to cancellation notifications.
|
|
159
|
-
*/
|
|
160
|
-
heartbeat(details) {
|
|
161
|
-
this.heartbeatFn(details);
|
|
162
|
-
}
|
|
163
128
|
/**
|
|
164
129
|
* Gets the context of the current Activity.
|
|
165
130
|
*
|
|
@@ -173,17 +138,148 @@ class Context {
|
|
|
173
138
|
return store;
|
|
174
139
|
}
|
|
175
140
|
/**
|
|
176
|
-
*
|
|
177
|
-
*
|
|
178
|
-
* @
|
|
141
|
+
* **Not** meant to instantiated by Activity code, used by the worker.
|
|
142
|
+
*
|
|
143
|
+
* @ignore
|
|
179
144
|
*/
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
145
|
+
constructor(info, cancelled, cancellationSignal, heartbeat, log) {
|
|
146
|
+
/**
|
|
147
|
+
* Send a {@link https://docs.temporal.io/concepts/what-is-an-activity-heartbeat | heartbeat} from an Activity.
|
|
148
|
+
*
|
|
149
|
+
* If an Activity times out, then during the next retry, the last value of `details` is available at
|
|
150
|
+
* {@link Info.heartbeatDetails}. This acts as a periodic checkpoint mechanism for the progress of an Activity.
|
|
151
|
+
*
|
|
152
|
+
* If an Activity times out on the final retry (relevant in cases in which {@link RetryPolicy.maximumAttempts} is
|
|
153
|
+
* set), the Activity function call in the Workflow code will throw an {@link ActivityFailure} with the `cause`
|
|
154
|
+
* attribute set to a {@link TimeoutFailure}, which has the last value of `details` available at
|
|
155
|
+
* {@link TimeoutFailure.lastHeartbeatDetails}.
|
|
156
|
+
*
|
|
157
|
+
* Calling `heartbeat()` from a Local Activity has no effect.
|
|
158
|
+
*
|
|
159
|
+
* The SDK automatically throttles heartbeat calls to the server with a duration of 80% of the specified activity
|
|
160
|
+
* heartbeat timeout. Throttling behavior may be customized with the `{@link maxHeartbeatThrottleInterval | https://typescript.temporal.io/api/interfaces/worker.WorkerOptions#maxheartbeatthrottleinterval} and {@link defaultHeartbeatThrottleInterval | https://typescript.temporal.io/api/interfaces/worker.WorkerOptions#defaultheartbeatthrottleinterval} worker options.
|
|
161
|
+
*
|
|
162
|
+
* Activities must heartbeat in order to receive Cancellation (unless they're Local Activities, which don't need to).
|
|
163
|
+
*
|
|
164
|
+
* :warning: Cancellation is not propagated from this function, use {@link cancelled} or {@link cancellationSignal} to
|
|
165
|
+
* subscribe to cancellation notifications.
|
|
166
|
+
*/
|
|
167
|
+
this.heartbeat = (details) => {
|
|
168
|
+
this.heartbeatFn(details);
|
|
169
|
+
};
|
|
170
|
+
/**
|
|
171
|
+
* Helper function for sleeping in an Activity.
|
|
172
|
+
* @param ms Sleep duration: number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
|
|
173
|
+
* @returns A Promise that either resolves when `ms` is reached or rejects when the Activity is cancelled
|
|
174
|
+
*/
|
|
175
|
+
this.sleep = (ms) => {
|
|
176
|
+
let handle;
|
|
177
|
+
const timer = new Promise((resolve) => {
|
|
178
|
+
handle = setTimeout(resolve, (0, time_1.msToNumber)(ms));
|
|
179
|
+
});
|
|
180
|
+
return Promise.race([this.cancelled.finally(() => clearTimeout(handle)), timer]);
|
|
181
|
+
};
|
|
182
|
+
this.info = info;
|
|
183
|
+
this.cancelled = cancelled;
|
|
184
|
+
this.cancellationSignal = cancellationSignal;
|
|
185
|
+
this.heartbeatFn = heartbeat;
|
|
186
|
+
this.log = log;
|
|
186
187
|
}
|
|
187
188
|
}
|
|
188
189
|
exports.Context = Context;
|
|
190
|
+
/**
|
|
191
|
+
* The current Activity's context.
|
|
192
|
+
*/
|
|
193
|
+
function activityInfo() {
|
|
194
|
+
// For consistency with workflow.workflowInfo(), we want activityInfo() to be a function, rather than a const object.
|
|
195
|
+
return Context.current().info;
|
|
196
|
+
}
|
|
197
|
+
exports.activityInfo = activityInfo;
|
|
198
|
+
/**
|
|
199
|
+
* The logger for this Activity.
|
|
200
|
+
*
|
|
201
|
+
* This is a shortcut for `Context.current().log` (see {@link Context.log}).
|
|
202
|
+
*/
|
|
203
|
+
exports.log = {
|
|
204
|
+
// Context.current().log may legitimately change during the lifetime of an Activity, so we can't
|
|
205
|
+
// just initialize that field to the value of Context.current().log and move on. Hence this indirection.
|
|
206
|
+
log(level, message, meta) {
|
|
207
|
+
return Context.current().log.log(level, message, meta);
|
|
208
|
+
},
|
|
209
|
+
trace(message, meta) {
|
|
210
|
+
return Context.current().log.trace(message, meta);
|
|
211
|
+
},
|
|
212
|
+
debug(message, meta) {
|
|
213
|
+
return Context.current().log.debug(message, meta);
|
|
214
|
+
},
|
|
215
|
+
info(message, meta) {
|
|
216
|
+
return Context.current().log.info(message, meta);
|
|
217
|
+
},
|
|
218
|
+
warn(message, meta) {
|
|
219
|
+
return Context.current().log.warn(message, meta);
|
|
220
|
+
},
|
|
221
|
+
error(message, meta) {
|
|
222
|
+
return Context.current().log.error(message, meta);
|
|
223
|
+
},
|
|
224
|
+
};
|
|
225
|
+
/**
|
|
226
|
+
* Helper function for sleeping in an Activity.
|
|
227
|
+
*
|
|
228
|
+
* This is a shortcut for `Context.current().sleep(ms)` (see {@link Context.sleep}).
|
|
229
|
+
*
|
|
230
|
+
* @param ms Sleep duration: number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
|
|
231
|
+
* @returns A Promise that either resolves when `ms` is reached or rejects when the Activity is cancelled
|
|
232
|
+
*/
|
|
233
|
+
function sleep(ms) {
|
|
234
|
+
return Context.current().sleep(ms);
|
|
235
|
+
}
|
|
236
|
+
exports.sleep = sleep;
|
|
237
|
+
/**
|
|
238
|
+
* Send a {@link https://docs.temporal.io/concepts/what-is-an-activity-heartbeat | heartbeat} from an Activity.
|
|
239
|
+
*
|
|
240
|
+
* If an Activity times out, then during the next retry, the last value of `details` is available at
|
|
241
|
+
* {@link Info.heartbeatDetails}. This acts as a periodic checkpoint mechanism for the progress of an Activity.
|
|
242
|
+
*
|
|
243
|
+
* If an Activity times out on the final retry (relevant in cases in which {@link RetryPolicy.maximumAttempts} is
|
|
244
|
+
* set), the Activity function call in the Workflow code will throw an {@link ActivityFailure} with the `cause`
|
|
245
|
+
* attribute set to a {@link TimeoutFailure}, which has the last value of `details` available at
|
|
246
|
+
* {@link TimeoutFailure.lastHeartbeatDetails}.
|
|
247
|
+
*
|
|
248
|
+
* This is a shortcut for `Context.current().heatbeat(ms)` (see {@link Context.heartbeat}).
|
|
249
|
+
*/
|
|
250
|
+
function heartbeat(details) {
|
|
251
|
+
Context.current().heartbeat(details);
|
|
252
|
+
}
|
|
253
|
+
exports.heartbeat = heartbeat;
|
|
254
|
+
/**
|
|
255
|
+
* Return a Promise that fails with a {@link CancelledFailure} when cancellation of this activity is requested. The
|
|
256
|
+
* promise is guaranteed to never successfully resolve. Await this promise in an Activity to get notified of
|
|
257
|
+
* cancellation.
|
|
258
|
+
*
|
|
259
|
+
* Note that to get notified of cancellation, an activity must _also_ do {@link Context.heartbeat}.
|
|
260
|
+
*
|
|
261
|
+
* This is a shortcut for `Context.current().cancelled` (see {@link Context.cancelled}).
|
|
262
|
+
*/
|
|
263
|
+
function cancelled() {
|
|
264
|
+
return Context.current().cancelled;
|
|
265
|
+
}
|
|
266
|
+
exports.cancelled = cancelled;
|
|
267
|
+
/**
|
|
268
|
+
* Return an {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal | `AbortSignal`} that can be used to
|
|
269
|
+
* react to Activity cancellation.
|
|
270
|
+
*
|
|
271
|
+
* This can be passed in to libraries such as
|
|
272
|
+
* {@link https://www.npmjs.com/package/node-fetch#request-cancellation-with-abortsignal | fetch} to abort an
|
|
273
|
+
* in-progress request and
|
|
274
|
+
* {@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options child_process}
|
|
275
|
+
* to abort a child process, as well as other built-in node modules and modules found on npm.
|
|
276
|
+
*
|
|
277
|
+
* Note that to get notified of cancellation, an activity must _also_ do {@link Context.heartbeat}.
|
|
278
|
+
*
|
|
279
|
+
* This is a shortcut for `Context.current().cancellationSignal` (see {@link Context.cancellationSignal}).
|
|
280
|
+
*/
|
|
281
|
+
function cancellationSignal() {
|
|
282
|
+
return Context.current().cancellationSignal;
|
|
283
|
+
}
|
|
284
|
+
exports.cancellationSignal = cancellationSignal;
|
|
189
285
|
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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O,EAAE,CAAC,GAAG,CAAC,KAAK,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;IACpD,CAAC;CACF,CAAC;AAEF;;;;;;;GAOG;AACH,SAAgB,KAAK,CAAC,EAAY;IAChC,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AACrC,CAAC;AAFD,sBAEC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,SAAS,CAAC,OAAiB;IACzC,OAAO,CAAC,OAAO,EAAE,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AACvC,CAAC;AAFD,8BAEC;AAED;;;;;;;;GAQG;AACH,SAAgB,SAAS;IACvB,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,SAAS,CAAC;AACrC,CAAC;AAFD,8BAEC;AAED;;;;;;;;;;;;;GAaG;AACH,SAAgB,kBAAkB;IAChC,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC,kBAAkB,CAAC;AAC9C,CAAC;AAFD,gDAEC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@temporalio/activity",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.0-rc.0",
|
|
4
4
|
"description": "Temporal.io SDK Activity sub-package",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"author": "Temporal Technologies Inc. <sdk@temporal.io>",
|
|
14
14
|
"license": "MIT",
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@temporalio/common": "1.
|
|
16
|
+
"@temporalio/common": "1.9.0-rc.0",
|
|
17
17
|
"abort-controller": "^3.0.0"
|
|
18
18
|
},
|
|
19
19
|
"bugs": {
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"src",
|
|
33
33
|
"lib"
|
|
34
34
|
],
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "ca3e508e62de02b2c9bb40d0d889003cebba282d"
|
|
36
36
|
}
|
package/src/index.ts
CHANGED
|
@@ -69,9 +69,10 @@
|
|
|
69
69
|
* @module
|
|
70
70
|
*/
|
|
71
71
|
|
|
72
|
+
// Keep this around until we drop support for Node 14.
|
|
72
73
|
import 'abort-controller/polyfill'; // eslint-disable-line import/no-unassigned-import
|
|
73
74
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
74
|
-
import { Logger, Duration } from '@temporalio/common';
|
|
75
|
+
import { Logger, Duration, LogLevel, LogMetadata } from '@temporalio/common';
|
|
75
76
|
import { msToNumber } from '@temporalio/common/lib/time';
|
|
76
77
|
import { SymbolBasedInstanceOfError } from '@temporalio/common/lib/type-helpers';
|
|
77
78
|
|
|
@@ -115,78 +116,78 @@ export const asyncLocalStorage: AsyncLocalStorage<Context> = (globalThis as any)
|
|
|
115
116
|
* Holds information about the current Activity Execution. Retrieved inside an Activity with `Context.current().info`.
|
|
116
117
|
*/
|
|
117
118
|
export interface Info {
|
|
118
|
-
taskToken: Uint8Array;
|
|
119
|
+
readonly taskToken: Uint8Array;
|
|
119
120
|
/**
|
|
120
121
|
* Base64 encoded `taskToken`
|
|
121
122
|
*/
|
|
122
|
-
base64TaskToken: string;
|
|
123
|
-
activityId: string;
|
|
123
|
+
readonly base64TaskToken: string;
|
|
124
|
+
readonly activityId: string;
|
|
124
125
|
/**
|
|
125
126
|
* Exposed Activity function name
|
|
126
127
|
*/
|
|
127
|
-
activityType: string;
|
|
128
|
+
readonly activityType: string;
|
|
128
129
|
/**
|
|
129
130
|
* The namespace this Activity is running in
|
|
130
131
|
*/
|
|
131
|
-
activityNamespace: string;
|
|
132
|
+
readonly activityNamespace: string;
|
|
132
133
|
/**
|
|
133
134
|
* Attempt number for this activity
|
|
134
135
|
*/
|
|
135
|
-
attempt: number;
|
|
136
|
+
readonly attempt: number;
|
|
136
137
|
/**
|
|
137
138
|
* Whether this activity is scheduled in local or remote mode
|
|
138
139
|
*/
|
|
139
|
-
isLocal: boolean;
|
|
140
|
+
readonly isLocal: boolean;
|
|
140
141
|
/**
|
|
141
142
|
* Information about the Workflow that scheduled the Activity
|
|
142
143
|
*/
|
|
143
|
-
workflowExecution: {
|
|
144
|
-
workflowId: string;
|
|
145
|
-
runId: string;
|
|
144
|
+
readonly workflowExecution: {
|
|
145
|
+
readonly workflowId: string;
|
|
146
|
+
readonly runId: string;
|
|
146
147
|
};
|
|
147
148
|
/**
|
|
148
149
|
* The namespace of the Workflow that scheduled this Activity
|
|
149
150
|
*/
|
|
150
|
-
workflowNamespace: string;
|
|
151
|
+
readonly workflowNamespace: string;
|
|
151
152
|
/**
|
|
152
153
|
* The module name of the Workflow that scheduled this Activity
|
|
153
154
|
*/
|
|
154
|
-
workflowType: string;
|
|
155
|
+
readonly workflowType: string;
|
|
155
156
|
/**
|
|
156
157
|
* Timestamp for when this Activity was scheduled in milliseconds
|
|
157
158
|
*/
|
|
158
|
-
scheduledTimestampMs: number;
|
|
159
|
+
readonly scheduledTimestampMs: number;
|
|
159
160
|
/**
|
|
160
161
|
* Timeout for this Activity from schedule to close in milliseconds.
|
|
161
162
|
*/
|
|
162
|
-
scheduleToCloseTimeoutMs: number;
|
|
163
|
+
readonly scheduleToCloseTimeoutMs: number;
|
|
163
164
|
/**
|
|
164
165
|
* Timeout for this Activity from start to close in milliseconds
|
|
165
166
|
*/
|
|
166
|
-
startToCloseTimeoutMs: number;
|
|
167
|
+
readonly startToCloseTimeoutMs: number;
|
|
167
168
|
/**
|
|
168
169
|
* Timestamp for when the current attempt of this Activity was scheduled in milliseconds
|
|
169
170
|
*/
|
|
170
|
-
currentAttemptScheduledTimestampMs: number;
|
|
171
|
+
readonly currentAttemptScheduledTimestampMs: number;
|
|
171
172
|
/**
|
|
172
173
|
* Heartbeat timeout in milliseconds.
|
|
173
174
|
* If this timeout is defined, the Activity must heartbeat before the timeout is reached.
|
|
174
175
|
* The Activity must **not** heartbeat in case this timeout is not defined.
|
|
175
176
|
*/
|
|
176
|
-
heartbeatTimeoutMs?: number;
|
|
177
|
+
readonly heartbeatTimeoutMs?: number;
|
|
177
178
|
/**
|
|
178
179
|
* The {@link Context.heartbeat | details} from the last recorded heartbeat from the last attempt of this Activity.
|
|
179
180
|
*
|
|
180
181
|
* Use this to resume your Activity from a checkpoint.
|
|
181
182
|
*/
|
|
182
|
-
heartbeatDetails: any;
|
|
183
|
+
readonly heartbeatDetails: any;
|
|
183
184
|
|
|
184
185
|
/**
|
|
185
186
|
* Task Queue the Activity is scheduled in.
|
|
186
187
|
*
|
|
187
188
|
* For Local Activities, this is set to the Workflow's Task Queue.
|
|
188
189
|
*/
|
|
189
|
-
taskQueue: string;
|
|
190
|
+
readonly taskQueue: string;
|
|
190
191
|
}
|
|
191
192
|
|
|
192
193
|
/**
|
|
@@ -200,18 +201,34 @@ export interface Info {
|
|
|
200
201
|
* Call `Context.current()` from Activity code in order to get the current Activity's Context.
|
|
201
202
|
*/
|
|
202
203
|
export class Context {
|
|
204
|
+
/**
|
|
205
|
+
* Gets the context of the current Activity.
|
|
206
|
+
*
|
|
207
|
+
* Uses {@link https://nodejs.org/docs/latest-v14.x/api/async_hooks.html#async_hooks_class_asynclocalstorage | AsyncLocalStorage} under the hood to make it accessible in nested callbacks and promises.
|
|
208
|
+
*/
|
|
209
|
+
public static current(): Context {
|
|
210
|
+
const store = asyncLocalStorage.getStore();
|
|
211
|
+
if (store === undefined) {
|
|
212
|
+
throw new Error('Activity context not initialized');
|
|
213
|
+
}
|
|
214
|
+
return store;
|
|
215
|
+
}
|
|
216
|
+
|
|
203
217
|
/**
|
|
204
218
|
* Holds information about the current executing Activity.
|
|
205
219
|
*/
|
|
206
|
-
public info: Info;
|
|
220
|
+
public readonly info: Info;
|
|
221
|
+
|
|
207
222
|
/**
|
|
208
|
-
*
|
|
223
|
+
* A Promise that fails with a {@link CancelledFailure} when cancellation of this activity is requested. The promise
|
|
224
|
+
* is guaranteed to never successfully resolve. Await this promise in an Activity to get notified of cancellation.
|
|
209
225
|
*
|
|
210
|
-
*
|
|
226
|
+
* Note that to get notified of cancellation, an activity must _also_ {@link Context.heartbeat}.
|
|
211
227
|
*
|
|
212
228
|
* @see [Cancellation](/api/namespaces/activity#cancellation)
|
|
213
229
|
*/
|
|
214
230
|
public readonly cancelled: Promise<never>;
|
|
231
|
+
|
|
215
232
|
/**
|
|
216
233
|
* An {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal | `AbortSignal`} that can be used to react to
|
|
217
234
|
* Activity cancellation.
|
|
@@ -222,24 +239,31 @@ export class Context {
|
|
|
222
239
|
* {@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options child_process}
|
|
223
240
|
* to abort a child process, as well as other built-in node modules and modules found on npm.
|
|
224
241
|
*
|
|
242
|
+
* Note that to get notified of cancellation, an activity must _also_ {@link Context.heartbeat}.
|
|
243
|
+
*
|
|
225
244
|
* @see [Cancellation](/api/namespaces/activity#cancellation)
|
|
226
245
|
*/
|
|
227
246
|
public readonly cancellationSignal: AbortSignal;
|
|
247
|
+
|
|
228
248
|
/**
|
|
229
249
|
* The heartbeat implementation, injected via the constructor.
|
|
230
250
|
*/
|
|
231
251
|
protected readonly heartbeatFn: (details?: any) => void;
|
|
252
|
+
|
|
232
253
|
/**
|
|
233
254
|
* The logger for this Activity.
|
|
234
255
|
*
|
|
235
|
-
* This defaults to the `Runtime`'s Logger (see {@link Runtime.logger}).
|
|
236
|
-
*
|
|
237
|
-
*
|
|
238
|
-
* Activity's life cycle will automatically be logged (at 'DEBUG' level for most messages; 'WARN' for failures).
|
|
256
|
+
* This defaults to the `Runtime`'s Logger (see {@link Runtime.logger}). Attributes from the current Activity context
|
|
257
|
+
* will automatically be included as metadata on every log entries, and some key events of the Activity's lifecycle
|
|
258
|
+
* will automatically be logged (at 'DEBUG' level for most messages; 'WARN' for failures).
|
|
239
259
|
*
|
|
240
|
-
* To
|
|
241
|
-
* `
|
|
242
|
-
*
|
|
260
|
+
* To customize log attributes, register a {@link ActivityOutboundCallsInterceptor} that intercepts the
|
|
261
|
+
* `getLogAttributes()` method.
|
|
262
|
+
*
|
|
263
|
+
* Modifying the context logger (eg. `context.log = myCustomLogger` or by an {@link ActivityInboundLogInterceptor}
|
|
264
|
+
* with a custom logger as argument) is deprecated. Doing so will prevent automatic inclusion of custom log attributes
|
|
265
|
+
* through the `getLogAttributes()` interceptor. To customize _where_ log messages are sent, set the
|
|
266
|
+
* {@link Runtime.logger} property instead.
|
|
243
267
|
*/
|
|
244
268
|
public log: Logger;
|
|
245
269
|
|
|
@@ -253,13 +277,13 @@ export class Context {
|
|
|
253
277
|
cancelled: Promise<never>,
|
|
254
278
|
cancellationSignal: AbortSignal,
|
|
255
279
|
heartbeat: (details?: any) => void,
|
|
256
|
-
|
|
280
|
+
log: Logger
|
|
257
281
|
) {
|
|
258
282
|
this.info = info;
|
|
259
283
|
this.cancelled = cancelled;
|
|
260
284
|
this.cancellationSignal = cancellationSignal;
|
|
261
285
|
this.heartbeatFn = heartbeat;
|
|
262
|
-
this.log =
|
|
286
|
+
this.log = log;
|
|
263
287
|
}
|
|
264
288
|
|
|
265
289
|
/**
|
|
@@ -283,33 +307,116 @@ export class Context {
|
|
|
283
307
|
* :warning: Cancellation is not propagated from this function, use {@link cancelled} or {@link cancellationSignal} to
|
|
284
308
|
* subscribe to cancellation notifications.
|
|
285
309
|
*/
|
|
286
|
-
public heartbeat(details?: unknown): void {
|
|
310
|
+
public readonly heartbeat = (details?: unknown): void => {
|
|
287
311
|
this.heartbeatFn(details);
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
/**
|
|
291
|
-
* Gets the context of the current Activity.
|
|
292
|
-
*
|
|
293
|
-
* Uses {@link https://nodejs.org/docs/latest-v14.x/api/async_hooks.html#async_hooks_class_asynclocalstorage | AsyncLocalStorage} under the hood to make it accessible in nested callbacks and promises.
|
|
294
|
-
*/
|
|
295
|
-
public static current(): Context {
|
|
296
|
-
const store = asyncLocalStorage.getStore();
|
|
297
|
-
if (store === undefined) {
|
|
298
|
-
throw new Error('Activity context not initialized');
|
|
299
|
-
}
|
|
300
|
-
return store;
|
|
301
|
-
}
|
|
312
|
+
};
|
|
302
313
|
|
|
303
314
|
/**
|
|
304
315
|
* Helper function for sleeping in an Activity.
|
|
305
316
|
* @param ms Sleep duration: number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
|
|
306
317
|
* @returns A Promise that either resolves when `ms` is reached or rejects when the Activity is cancelled
|
|
307
318
|
*/
|
|
308
|
-
public sleep(ms: Duration): Promise<void> {
|
|
319
|
+
public readonly sleep = (ms: Duration): Promise<void> => {
|
|
309
320
|
let handle: NodeJS.Timeout;
|
|
310
321
|
const timer = new Promise<void>((resolve) => {
|
|
311
322
|
handle = setTimeout(resolve, msToNumber(ms));
|
|
312
323
|
});
|
|
313
324
|
return Promise.race([this.cancelled.finally(() => clearTimeout(handle)), timer]);
|
|
314
|
-
}
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* The current Activity's context.
|
|
330
|
+
*/
|
|
331
|
+
export function activityInfo(): Info {
|
|
332
|
+
// For consistency with workflow.workflowInfo(), we want activityInfo() to be a function, rather than a const object.
|
|
333
|
+
return Context.current().info;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* The logger for this Activity.
|
|
338
|
+
*
|
|
339
|
+
* This is a shortcut for `Context.current().log` (see {@link Context.log}).
|
|
340
|
+
*/
|
|
341
|
+
export const log: Logger = {
|
|
342
|
+
// Context.current().log may legitimately change during the lifetime of an Activity, so we can't
|
|
343
|
+
// just initialize that field to the value of Context.current().log and move on. Hence this indirection.
|
|
344
|
+
log(level: LogLevel, message: string, meta?: LogMetadata): any {
|
|
345
|
+
return Context.current().log.log(level, message, meta);
|
|
346
|
+
},
|
|
347
|
+
trace(message: string, meta?: LogMetadata): any {
|
|
348
|
+
return Context.current().log.trace(message, meta);
|
|
349
|
+
},
|
|
350
|
+
debug(message: string, meta?: LogMetadata): any {
|
|
351
|
+
return Context.current().log.debug(message, meta);
|
|
352
|
+
},
|
|
353
|
+
info(message: string, meta?: LogMetadata): any {
|
|
354
|
+
return Context.current().log.info(message, meta);
|
|
355
|
+
},
|
|
356
|
+
warn(message: string, meta?: LogMetadata): any {
|
|
357
|
+
return Context.current().log.warn(message, meta);
|
|
358
|
+
},
|
|
359
|
+
error(message: string, meta?: LogMetadata): any {
|
|
360
|
+
return Context.current().log.error(message, meta);
|
|
361
|
+
},
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Helper function for sleeping in an Activity.
|
|
366
|
+
*
|
|
367
|
+
* This is a shortcut for `Context.current().sleep(ms)` (see {@link Context.sleep}).
|
|
368
|
+
*
|
|
369
|
+
* @param ms Sleep duration: number of milliseconds or {@link https://www.npmjs.com/package/ms | ms-formatted string}
|
|
370
|
+
* @returns A Promise that either resolves when `ms` is reached or rejects when the Activity is cancelled
|
|
371
|
+
*/
|
|
372
|
+
export function sleep(ms: Duration): Promise<void> {
|
|
373
|
+
return Context.current().sleep(ms);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Send a {@link https://docs.temporal.io/concepts/what-is-an-activity-heartbeat | heartbeat} from an Activity.
|
|
378
|
+
*
|
|
379
|
+
* If an Activity times out, then during the next retry, the last value of `details` is available at
|
|
380
|
+
* {@link Info.heartbeatDetails}. This acts as a periodic checkpoint mechanism for the progress of an Activity.
|
|
381
|
+
*
|
|
382
|
+
* If an Activity times out on the final retry (relevant in cases in which {@link RetryPolicy.maximumAttempts} is
|
|
383
|
+
* set), the Activity function call in the Workflow code will throw an {@link ActivityFailure} with the `cause`
|
|
384
|
+
* attribute set to a {@link TimeoutFailure}, which has the last value of `details` available at
|
|
385
|
+
* {@link TimeoutFailure.lastHeartbeatDetails}.
|
|
386
|
+
*
|
|
387
|
+
* This is a shortcut for `Context.current().heatbeat(ms)` (see {@link Context.heartbeat}).
|
|
388
|
+
*/
|
|
389
|
+
export function heartbeat(details?: unknown): void {
|
|
390
|
+
Context.current().heartbeat(details);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Return a Promise that fails with a {@link CancelledFailure} when cancellation of this activity is requested. The
|
|
395
|
+
* promise is guaranteed to never successfully resolve. Await this promise in an Activity to get notified of
|
|
396
|
+
* cancellation.
|
|
397
|
+
*
|
|
398
|
+
* Note that to get notified of cancellation, an activity must _also_ do {@link Context.heartbeat}.
|
|
399
|
+
*
|
|
400
|
+
* This is a shortcut for `Context.current().cancelled` (see {@link Context.cancelled}).
|
|
401
|
+
*/
|
|
402
|
+
export function cancelled(): Promise<never> {
|
|
403
|
+
return Context.current().cancelled;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Return an {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal | `AbortSignal`} that can be used to
|
|
408
|
+
* react to Activity cancellation.
|
|
409
|
+
*
|
|
410
|
+
* This can be passed in to libraries such as
|
|
411
|
+
* {@link https://www.npmjs.com/package/node-fetch#request-cancellation-with-abortsignal | fetch} to abort an
|
|
412
|
+
* in-progress request and
|
|
413
|
+
* {@link https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options child_process}
|
|
414
|
+
* to abort a child process, as well as other built-in node modules and modules found on npm.
|
|
415
|
+
*
|
|
416
|
+
* Note that to get notified of cancellation, an activity must _also_ do {@link Context.heartbeat}.
|
|
417
|
+
*
|
|
418
|
+
* This is a shortcut for `Context.current().cancellationSignal` (see {@link Context.cancellationSignal}).
|
|
419
|
+
*/
|
|
420
|
+
export function cancellationSignal(): AbortSignal {
|
|
421
|
+
return Context.current().cancellationSignal;
|
|
315
422
|
}
|