UNPKG

@hotmeshio/hotmesh

Version:

Serverless Workflow

195 lines (194 loc) 6.63 kB
import { HotMesh } from '../hotmesh'; import { MeshCallConnectParams, MeshCallCronParams, MeshCallExecParams, MeshCallFlushParams, MeshCallInstanceOptions, MeshCallInterruptParams } from '../../types/meshcall'; import { ProviderConfig, ProvidersConfig } from '../../types/provider'; /** * MeshCall connects any function as an idempotent endpoint. * Call functions from anywhere on the network connected to the * target backend (Postgres, Redis/ValKey, NATS, etc). Function * responses are cacheable and invocations can be scheduled to * run as idempotent cron jobs (this one runs nightly at midnight * and uses Postgres as the backend provider). * * @example * ```typescript * import { Client as Postgres } from 'pg'; * import { MeshCall } from '@hotmesh/meshcall'; * * MeshCall.cron({ * topic: 'my.cron.function', * connection: { * class: Postgres, * options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' } * }, * callback: async () => { * //your code here...anything goes * }, * options: { id: 'myDailyCron123', interval: '0 0 * * *' } * }); * ``` */ declare class MeshCall { /** * @private */ static workers: Map<string, HotMesh | Promise<HotMesh>>; /** * @private */ static engines: Map<string, HotMesh | Promise<HotMesh>>; /** * @private */ static connections: Map<string, any>; /** * @private */ constructor(); /** * iterates cached worker/engine instances to locate the first match * with the provided namespace and connection options * @private */ static findFirstMatching(targets: Map<string, HotMesh | Promise<HotMesh>>, namespace: string, config: ProviderConfig | ProvidersConfig, options?: MeshCallInstanceOptions): Promise<HotMesh | void>; /** * @private */ static getHotMeshClient: (namespace: string, connection: ProviderConfig | ProvidersConfig, options?: MeshCallInstanceOptions) => Promise<HotMesh>; /** * @private */ static verifyWorkflowActive(hotMesh: HotMesh, appId?: string, count?: number): Promise<boolean>; /** * @private */ static activateWorkflow(hotMesh: HotMesh, appId?: string, version?: string): Promise<void>; /** * Returns a cached worker instance or creates a new one * @private */ static getInstance(namespace: string, providerConfig: ProviderConfig | ProvidersConfig, options?: MeshCallInstanceOptions): Promise<HotMesh>; /** * connection re-use is important when making repeated calls, but * only if the connection options are an exact match. this method * hashes the connection options to ensure that the same connection */ static hashOptions(connection: ProviderConfig | ProvidersConfig): string; /** * Connects and links a worker function to the mesh * @example * ```typescript * import { Client as Postgres } from 'pg'; * import { MeshCall } from '@hotmesh/meshcall'; * * MeshCall.connect({ * topic: 'my.function', * connection: { * class: Postgres, * options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' } * }, * callback: async (arg1: any) => { * //your code here... * } * }); * ``` */ static connect(params: MeshCallConnectParams): Promise<HotMesh>; /** * Calls a function and returns the response. * * @template U - the return type of the linked worker function * * @example * ```typescript * const response = await MeshCall.exec({ * topic: 'my.function', * args: [{ my: 'args' }], * connection: { * class: Postgres, * options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' } * } * }); * ``` */ static exec<U>(params: MeshCallExecParams): Promise<U>; /** * Clears a cached function response. * * @example * ```typescript * import { Client as Postgres } from 'pg'; * import { MeshCall } from '@hotmesh/meshcall'; * * MeshCall.flush({ * topic: 'my.function', * connection: { * class: Postgres, * options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' } * }, * options: { id: 'myCachedExecFunctionId' } * }); * ``` */ static flush(params: MeshCallFlushParams): Promise<void>; /** * Creates a stream where messages can be published to ensure there is a * channel in place when the message arrives (a race condition for those * platforms without implicit topic setup). * @private */ static createStream: (hotMeshClient: HotMesh, workflowTopic: string, namespace?: string) => Promise<void>; /** * Schedules a cron job to run at a specified interval * with optional args. Provided arguments are passed to the * callback function each time the cron job runs. The `id` * option is used to uniquely identify the cron job, allowing * it to be interrupted at any time. * * @example * ```typescript * import { Client as Postgres } from 'pg'; * import { MeshCall } from '@hotmesh/meshcall'; * * MeshCall.cron({ * topic: 'my.cron.function', * args: ['arg1', 'arg2'], //optionally pass args * connection: { * class: Postgres, * options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' } * }, * callback: async (arg1: any, arg2: any) => { * //your code here... * }, * options: { id: 'myDailyCron123', interval: '0 0 * * *' } * }); * ``` */ static cron(params: MeshCallCronParams): Promise<boolean>; /** * Interrupts a running cron job. Returns `true` if the job * was successfully interrupted, or `false` if the job was not * found. * * @example * ```typescript * import { Client as Postgres } from 'pg'; * import { MeshCall } from '@hotmesh/meshcall'; * * MeshCall.interrupt({ * topic: 'my.cron.function', * connection: { * class: Postgres, * options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' } * }, * options: { id: 'myDailyCron123' } * }); * ``` */ static interrupt(params: MeshCallInterruptParams): Promise<boolean>; /** * Shuts down all meshcall instances. Call this method * from the SIGTERM handler in your application. */ static shutdown(): Promise<void>; } export { MeshCall };