UNPKG

@temporalio/worker

Version:
134 lines (133 loc) 5.76 kB
import { AsyncLocalStorage } from 'node:async_hooks'; import { native } from '@temporalio/core-bridge'; import { ConnectionLike, Metadata, CallContext, WorkflowService } from '@temporalio/client'; import { NativeConnectionOptions } from './connection-options'; import { Runtime } from './runtime'; /** * A Native Connection object that delegates calls to the Rust Core binary extension. * * A Worker must use this class to connect to the server. * * This class can be used to power `@temporalio/client`'s Client objects. */ export declare class NativeConnection implements ConnectionLike { private readonly runtime; private readonly nativeClient; /** * referenceHolders is used internally by the framework, it can be accessed with `extractReferenceHolders` (below) */ private readonly referenceHolders; readonly workflowService: WorkflowService; readonly callContextStorage: AsyncLocalStorage<CallContext>; /** * nativeClient is intentionally left private, framework code can access it with `extractNativeClient` (below) */ protected constructor(runtime: Runtime, nativeClient: native.Client); /** * No-op. This class can only be created via eager connection. */ ensureConnected(): Promise<void>; private sendRequest; /** * Set a deadline for any service requests executed in `fn`'s scope. * * The deadline is a point in time after which any pending gRPC request will be considered as failed; * this will locally result in the request call throwing a {@link grpc.ServiceError|ServiceError} * with code {@link grpc.status.DEADLINE_EXCEEDED|DEADLINE_EXCEEDED}; see {@link isGrpcDeadlineError}. * * It is stronly recommended to explicitly set deadlines. If no deadline is set, then it is * possible for the client to end up waiting forever for a response. * * @param deadline a point in time after which the request will be considered as failed; either a * Date object, or a number of milliseconds since the Unix epoch (UTC). * @returns the value returned from `fn` * * @see https://grpc.io/docs/guides/deadlines/ */ withDeadline<ReturnType>(deadline: number | Date, fn: () => Promise<ReturnType>): Promise<ReturnType>; /** * Set metadata for any service requests executed in `fn`'s scope. * * The provided metadata is merged on top of any existing metadata in current scope, including metadata provided in * {@link NativeConnectionOptions.metadata}. * * @returns value returned from `fn` * * @example * * ```ts * const workflowHandle = await conn.withMetadata({ apiKey: 'secret' }, () => * conn.withMetadata({ otherKey: 'set' }, () => client.start(options))) * ); * ``` */ withMetadata<ReturnType>(metadata: Metadata, fn: () => Promise<ReturnType>): Promise<ReturnType>; /** * Set an {@link AbortSignal} that, when aborted, cancels any ongoing service requests executed in * `fn`'s scope. This will locally result in the request call throwing a {@link grpc.ServiceError|ServiceError} * with code {@link grpc.status.CANCELLED|CANCELLED}; see {@link isGrpcCancelledError}. * * This method is only a convenience wrapper around {@link NativeConnection.withAbortSignal}. * * @example * * ```ts * const ctrl = new AbortController(); * setTimeout(() => ctrl.abort(), 10_000); * // 👇 throws if incomplete by the timeout. * await conn.withAbortSignal(ctrl.signal, () => client.workflow.execute(myWorkflow, options)); * ``` * * @returns value returned from `fn` * * @see https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal */ withAbortSignal<ReturnType>(abortSignal: AbortSignal, fn: () => Promise<ReturnType>): Promise<ReturnType>; /** * @deprecated use `connect` instead */ static create(options?: NativeConnectionOptions): Promise<NativeConnection>; /** * Eagerly connect to the Temporal server and return a NativeConnection instance */ static connect(options?: NativeConnectionOptions): Promise<NativeConnection>; /** * Close this connection. * * Make sure any Workers using this connection are stopped before calling * this method or it will throw an {@link IllegalStateError} */ close(): Promise<void>; /** * Mapping of gRPC metadata (HTTP headers) to send with each request to the server. * * Use {@link NativeConnectionOptions.metadata} to set the initial metadata for client creation. */ setMetadata(metadata: Record<string, string>): Promise<void>; /** * Update the API key for this client. This is only set if `metadata` doesn't already have an * "authorization" key. * * Use {@link NativeConnectionOptions.apiKey} to set the initial metadata for client creation. */ setApiKey(apiKey: string): Promise<void>; } /** * Extract the private native client instance from a `NativeConnection` instance. * * Only meant to be used by the framework. */ export declare function extractNativeClient(conn: NativeConnection): native.Client; /** * Extract the private referenceHolders set from a `NativeConnection` instance. * * Only meant to be used by the framework. */ export declare function extractReferenceHolders(conn: NativeConnection): Set<native.Worker>; /** * Internal class used when a Worker directly instantiates a connection with no external references. * * This class is only used as a "marker" during Worker shutdown to decide whether to close the connection. */ export declare class InternalNativeConnection extends NativeConnection { }