Skip to main content

Class: Context

activity.Context

Activity Context, used to:

  • Get Info about the current Activity Execution
  • Send heartbeats
  • Get notified of Activity cancellation
  • Sleep (cancellation-aware)

Call Context.current() from Activity code in order to get the current Activity's Context.

Properties

cancellationSignal

Readonly cancellationSignal: AbortSignal

An AbortSignal that can be used to react to Activity cancellation.

This can be passed in to libraries such as fetch to abort an in-progress request and child_process to abort a child process, as well as other built-in node modules and modules found on npm.

Note that to get notified of cancellation, an activity must also Context.heartbeat.

See

Cancellation


cancelled

Readonly cancelled: Promise<never>

A Promise that fails with a CancelledFailure when cancellation of this activity is requested. The promise is guaranteed to never successfully resolve. Await this promise in an Activity to get notified of cancellation.

Note that to get notified of cancellation, an activity must also Context.heartbeat.

See

Cancellation


heartbeat

Readonly heartbeat: (details?: unknown) => void

Send a heartbeat from an Activity.

If an Activity times out, then during the next retry, the last value of details is available at Info.heartbeatDetails. This acts as a periodic checkpoint mechanism for the progress of an Activity.

If an Activity times out on the final retry (relevant in cases in which RetryPolicy.maximumAttempts is set), the Activity function call in the Workflow code will throw an ActivityFailure with the cause attribute set to a TimeoutFailure, which has the last value of details available at TimeoutFailure.lastHeartbeatDetails.

Calling heartbeat() from a Local Activity has no effect.

The SDK automatically throttles heartbeat calls to the server with a duration of 80% of the specified activity heartbeat timeout. Throttling behavior may be customized with the `maxHeartbeatThrottleInterval | https://typescript.temporal.io/api/interfaces/worker.WorkerOptions#maxheartbeatthrottleinterval and defaultHeartbeatThrottleInterval | https://typescript.temporal.io/api/interfaces/worker.WorkerOptions#defaultheartbeatthrottleinterval worker options.

Activities must heartbeat in order to receive Cancellation (unless they're Local Activities, which don't need to).

⚠️ Cancellation is not propagated from this function, use cancelled or cancellationSignal to subscribe to cancellation notifications.

Type declaration

▸ (details?): void

Send a heartbeat from an Activity.

If an Activity times out, then during the next retry, the last value of details is available at Info.heartbeatDetails. This acts as a periodic checkpoint mechanism for the progress of an Activity.

If an Activity times out on the final retry (relevant in cases in which RetryPolicy.maximumAttempts is set), the Activity function call in the Workflow code will throw an ActivityFailure with the cause attribute set to a TimeoutFailure, which has the last value of details available at TimeoutFailure.lastHeartbeatDetails.

Calling heartbeat() from a Local Activity has no effect.

The SDK automatically throttles heartbeat calls to the server with a duration of 80% of the specified activity heartbeat timeout. Throttling behavior may be customized with the `maxHeartbeatThrottleInterval | https://typescript.temporal.io/api/interfaces/worker.WorkerOptions#maxheartbeatthrottleinterval and defaultHeartbeatThrottleInterval | https://typescript.temporal.io/api/interfaces/worker.WorkerOptions#defaultheartbeatthrottleinterval worker options.

Activities must heartbeat in order to receive Cancellation (unless they're Local Activities, which don't need to).

⚠️ Cancellation is not propagated from this function, use cancelled or cancellationSignal to subscribe to cancellation notifications.

Parameters
NameType
details?unknown
Returns

void


info

Readonly info: Info

Holds information about the current executing Activity.


log

log: Logger

The logger for this Activity.

This defaults to the Runtime's Logger (see Runtime.logger). Attributes from the current Activity context are automatically included as metadata on every log entries. An extra sdkComponent metadata attribute is also added, with value activity; this can be used for fine-grained filtering of log entries further downstream.

To customize log attributes, register a ActivityOutboundCallsInterceptor that intercepts the getLogAttributes() method.

Modifying the context logger (eg. context.log = myCustomLogger or by an ActivityInboundLogInterceptor with a custom logger as argument) is deprecated. Doing so will prevent automatic inclusion of custom log attributes through the getLogAttributes() interceptor. To customize where log messages are sent, set the Runtime.logger property instead.


sleep

Readonly sleep: (ms: Duration) => Promise<void>

Helper function for sleeping in an Activity.

Param

Sleep duration: number of milliseconds or ms-formatted string

Type declaration

▸ (ms): Promise<void>

Helper function for sleeping in an Activity.

Parameters
NameTypeDescription
msDurationSleep duration: number of milliseconds or ms-formatted string
Returns

Promise<void>

A Promise that either resolves when ms is reached or rejects when the Activity is cancelled

Methods

current

current(): Context

Gets the context of the current Activity.

Uses AsyncLocalStorage under the hood to make it accessible in nested callbacks and promises.

Returns

Context