Skip to main content

Namespace: worker

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

Usage

import { Worker } from '@temporalio/worker';
import * as activities from './activities';

async function run() {
// Step 1: Register Workflows and Activities with the Worker and connect to
// the Temporal server.
const worker = await Worker.create({
workflowsPath: require.resolve('./workflows'),
activities,
taskQueue: 'hello-world',
});
// Worker connects to localhost by default and uses console.error for logging.
// Customize the Worker by passing more options to create():
// https://typescript.temporal.io/api/classes/worker.Worker
// If you need to configure server connection parameters, see docs:
// https://docs.temporal.io/typescript/security#encryption-in-transit-with-mtls

// Step 2: Start accepting tasks on the `hello-world` queue
await worker.run();
}

run().catch((err) => {
console.error(err);
process.exit(1);
});

Namespaces

Classes

Interfaces

References

IllegalStateError

Re-exports IllegalStateError

Type Aliases

Headers

Ƭ Headers: Record<string, Payload>

Headers are just a mapping of header name to Payload


History

Ƭ History: IHistory


InjectedSink

Ƭ InjectedSink<T>: { [P in keyof T]: InjectedSinkFunction<T[P]> }

Converts a Sink from a mapping of name to function to a mapping of name to InjectedSinkFunction

Type parameters

NameType
Textends Sink

InjectedSinks

Ƭ InjectedSinks<T>: { [P in keyof T]: InjectedSink<T[P]> }

Converts a Sinks interface from a mapping of name to Sink to a mapping of name to InjectedSink.

Used for type checking Sink injection against supplied type param T.

Type parameters

NameType
Textends Sinks

LogLevel

Ƭ LogLevel: "TRACE" | "DEBUG" | "INFO" | "WARN" | "ERROR"

Log level - must match rust log level names


LogMetadata

Ƭ LogMetadata: Record<string | symbol, any>


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
IFIF
FNextends keyof IF

RequiredNativeConnectionOptions

Ƭ RequiredNativeConnectionOptions: Omit<Required<NativeConnectionOptions>, "tls" | "metadata"> & { metadata?: NativeConnectionOptions["metadata"] ; sdkVersion: string ; tls?: NativeConnectionOptions["tls"] }


State

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

The worker's possible states

  • INITIALIZED - The initial state of the Worker after calling create and successful connection to the server
  • RUNNING - run was called, polling task queues
  • STOPPING - shutdown was called or received shutdown signal, worker will forcefully shutdown in shutdownGraceTime
  • DRAINING - Core has indicated that shutdown is complete and all Workflow tasks have been drained, waiting for activities and cached workflows eviction
  • DRAINED - All activities and workflows have completed, ready to shutdown
  • STOPPED - Shutdown complete, run resolves
  • FAILED - Worker encountered an unrecoverable error, run should reject with the error

TLSConfig

Ƭ TLSConfig: TLSConfig


TelemLogger

Ƭ TelemLogger: ConsoleLogger | ForwardLogger

Logger types supported by Core


WorkflowBundleOption

Ƭ WorkflowBundleOption: WorkflowBundleWithSourceMap | WorkflowBundlePathWithSourceMap

Variables

LogTimestamp

Const LogTimestamp: unique symbol


defaultPayloadConverter

Const defaultPayloadConverter: DefaultPayloadConverter

The default PayloadConverter used by the SDK. Supports Uint8Array and JSON serializables (so if JSON.stringify(yourArgOrRetval) works, the default payload converter will work).

To also support Protobufs, create a custom payload converter with DefaultPayloadConverter:

const myConverter = new DefaultPayloadConverter({ protobufRoot })

Functions

activityLogAttributes

activityLogAttributes(info): Record<string, unknown>

Returns a map of attributes to be set on log messages for a given Activity

Parameters

NameType
infoInfo

Returns

Record<string, unknown>


appendDefaultInterceptors

appendDefaultInterceptors(interceptors, logger?): WorkerInterceptors

Appends the default Worker logging interceptors to given interceptor arrays.

Parameters

NameTypeDescription
interceptorsWorkerInterceptors-
logger?Loggera Logger - defaults to the Runtime singleton logger.

Returns

WorkerInterceptors


bundleWorkflowCode

bundleWorkflowCode(options): Promise<WorkflowBundleWithSourceMap>

Create a bundle to pass to workflowBundle. Helpful for reducing Worker startup time in production.

When using with runReplayHistory, make sure to pass the same interceptors and payload converter used when the history was generated.

Parameters

NameType
optionsBundleOptions

Returns

Promise<WorkflowBundleWithSourceMap>


defaultSinks

defaultSinks(logger?): InjectedSinks<LoggerSinks>

Returns the defaultWorkerLogger sink which forwards logs from the Workflow sandbox to a given logger.

Parameters

NameTypeDescription
logger?Loggera Logger - defaults to the Runtime singleton logger.

Returns

InjectedSinks<LoggerSinks>


timeOfDayToBigint

timeOfDayToBigint(timeOfDay): bigint

Takes a [seconds, nanos] tuple as returned from getTimeOfDay and turns it into bigint.

Parameters

NameType
timeOfDay[number, number]

Returns

bigint


workflowLogAttributes

workflowLogAttributes(info): Record<string, unknown>

Returns a map of attributes to be set on log messages for a given Workflow

Parameters

NameType
infoWorkflowInfo

Returns

Record<string, unknown>