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.

Used by fetch to abort an in-progress request and child_process to abort a child process, and is supported by some other libraries as well.

See

Cancellation


cancelled​

β€’ Readonly cancelled: Promise<never>

Await this promise in an Activity to get notified of cancellation.

This promise will never resolveβ€”it will only be rejected with a CancelledFailure.

See

Cancellation


info​

β€’ info: Info

Holds information about the current executing Activity.

Methods​

heartbeat​

β–Έ heartbeat(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 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.

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

Parameters​

NameType
details?unknown

Returns​

void


sleep​

β–Έ sleep(ms): Promise<void>

Helper function for sleeping in an Activity.

Parameters​

NameTypeDescription
msstring | numberSleep 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


current​

β–Έ Static 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