Skip to main content

The temporal worker connects to the service and runs workflows and activities.

Usage#

import { Worker } from '@temporalio/worker';
async function run() {
// Automatically locate and register Activities and Workflows relative to __dirname
// (assuming package was bootstrapped with `npm init @temporalio`).
// Worker connects to localhost by default and uses console error for logging.
// Customize the Worker by passing more options to create().
// create() tries to connect to the server and will throw if a connection could not be established.
// You may create multiple Workers in a single process in order to poll on multiple task queues.
// In order to configure the server connection parameters and other global options,
// use the Core.install() method to configure the Rust Core SDK singleton.
const worker = await Worker.create({ workDir: __dirname, taskQueue: 'tutorial' });
// Start accepting tasks on the `tutorial` queue
await worker.run();
}
run().catch((err) => {
console.error(err);
process.exit(1);
});

Namespaces#

Enumerations#

Classes#

Interfaces#

Type aliases#

ActivityOptions#

Ƭ ActivityOptions: RemoteActivityOptions | LocalActivityOptions

Used to configure the way activities are run


Headers#

Ƭ Headers: Map<string, IPayload>

Headers are just a mapping of header name to Payload


InjectedDependencies#

Ƭ InjectedDependencies<T>: { [P in keyof T]: InjectedDependency<T[P]>}

Turns a ExternalDependencies interface from a mapping of name to ExternalDependency to a mapping of name to InjectedDependency.

Used for type checking Workflow external dependency injection.

Type parameters#

NameType
TExternalDependencies

InjectedDependency#

Ƭ InjectedDependency<T>: { [P in keyof T]: InjectedDependencyFunction<T[P]>}

Turns a ExternalDependency from a mapping of name to function to a mapping of name to InjectedDependencyFunction

Type parameters#

NameType
TExternalDependency

InjectedDependencyFunction#

Ƭ InjectedDependencyFunction<F>: ReturnType<F> extends Promise<any> ? { applyMode: ASYNC ; callDuringReplay?: boolean ; fn: (info: WorkflowInfo, ...args: Parameters<F>) => ReturnType<F> | StripPromise<ReturnType<F>> } : ReturnType<F> extends void ? { applyMode: ASYNC_IGNORED ; callDuringReplay?: boolean ; fn: (info: WorkflowInfo, ...args: Parameters<F>) => ReturnType<F> | Promise<ReturnType<F>> } | InjectedDependencyFunctionIvmVariant<F, SYNC_IGNORED, ReturnType<F> | Promise<ReturnType<F>>> | InjectedDependencyFunctionIvmVariant<F, SYNC, ReturnType<F>> | InjectedDependencyFunctionIvmVariant<F, SYNC_PROMISE, Promise<ReturnType<F>>> : InjectedDependencyFunctionIvmVariant<F, SYNC, ReturnType<F>> | InjectedDependencyFunctionIvmVariant<F, SYNC_PROMISE, Promise<ReturnType<F>>>

Takes a ExternalDependencyFunction and turns it into a type safe specification consisting of the function implementation type and call configuration.

InjectedDependencyFunction consists of these attributes:

  • fn - type of the implementation function for dependency F
  • applyMode - defines how fn is called from the Workflow isolate (@see ApplyMode)
  • callDuringReplay - whether or not fn will be called during Workflow replay - defaults to false
  • arguments - configure how arguments are transferred between isolates

Type parameters#

NameType
FExternalDependencyFunction

InjectedDependencyFunctionIvmVariant#

Ƭ InjectedDependencyFunctionIvmVariant<F, A, R>: { applyMode: A ; arguments: "copy" ; callDuringReplay?: boolean ; fn: (info: WorkflowInfo, ...args: Parameters<F>) => R } | { applyMode: A ; arguments: "reference" ; callDuringReplay?: boolean ; fn: (info: WorkflowInfo, ...args: RefParameters<F>) => R }

Synchronous variant of injected dependency function - uses isolated-vm to inject into the isolate

Type parameters#

NameType
FExternalDependencyFunction
AApplyMode
R-

LogLevel#

Ƭ LogLevel: "DEBUG" | "INFO" | "WARNING" | "ERROR"


Next#

Ƭ Next<IF, FN>: Required<IF>[FN] extends AnyFunc ? OmitLastParam<Required<IF>[FN]> : never

Type of the next function for a given interceptor function

Called from an interceptor to continue the interception chain

Type parameters#

NameType
IF-
FNkeyof IF

State#

Ƭ State: "INITIALIZED" | "RUNNING" | "STOPPED" | "STOPPING" | "DRAINING" | "DRAINED" | "FAILED" | "SUSPENDED"

The worker's possible states

  • INITIALIZED - The initial state of the Worker after calling Worker.create and successful connection to the server
  • RUNNING - Worker.run was called, polling task queues
  • SUSPENDED - Worker.suspendPolling was called, not polling for new tasks
  • STOPPING - Worker.shutdown was called or received shutdown signal
  • DRAINING - Core has indicated that shutdown is complete, allow activations and tasks to complete with respect to shutdownGraceTime
  • DRAINED - Draining complete, completing shutdown
  • STOPPED - Shutdown complete, Worker.run resolves
  • FAILED - Worker encountered an unrecoverable error, Worker.run should reject with the error

StripPromise#

Ƭ StripPromise<T>: T extends Promise<infer I> ? I : T

Return T if T is not a Promise otherwise extracts the type wrapped in T

Type parameters#

Name
T

TLSConfig#

Ƭ TLSConfig: native.TLSConfig


WorkerSpecOptions#

Ƭ WorkerSpecOptions<T>: T extends { dependencies: ExternalDependencies } ? { dependencies: InjectedDependencies<T["dependencies"]> } & WorkerOptions : WorkerOptions

Same as WorkerOptions with WorkerSpec applied

Type parameters#

NameType
TWorkerSpec

Variables#

defaultDataConverter#

Const defaultDataConverter: CompositeDataConverter

Functions#

getIvmTransferOptions#

getIvmTransferOptions(fn: InjectedDependencyFunction<any>): ivm.TransferOptionsBidirectional | undefined

Helper for extracting ivm.TransferOptionsBidirectional (when applicable) from an InjectedDependencyFunction.

Parameters#

NameType
fnInjectedDependencyFunction<any>

Returns: ivm.TransferOptionsBidirectional | undefined