import { HotMesh } from '../hotmesh'; import { VirtualConnectParams, VirtualContext, VirtualCronParams, VirtualExecParams, VirtualFlushParams, VirtualInstanceOptions, VirtualInterruptParams } from '../../types/virtual'; import { ProviderConfig, ProvidersConfig } from '../../types/provider'; /** * Virtual creates a virtual network of functions, connecting any * function as an idempotent, cacheable endpoint. Call functions * from anywhere on the network connected to the target backend * (Postgres, NATS, etc). 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 { Virtual } from '@hotmeshio/hotmesh'; * * Virtual.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 Virtual { /** * @private */ static workers: Map>; /** * @private */ static engines: Map>; /** * @private */ static connections: Map; /** * @private */ constructor(); /** * iterates cached worker/engine instances to locate the first match * with the provided namespace and connection options * @private */ static findFirstMatching(targets: Map>, namespace: string, config: ProviderConfig | ProvidersConfig, options?: VirtualInstanceOptions): Promise; /** * @private */ static getHotMeshClient: (namespace: string, connection: ProviderConfig | ProvidersConfig, options?: VirtualInstanceOptions) => Promise; /** * @private */ static verifyWorkflowActive(hotMesh: HotMesh, appId?: string, count?: number): Promise; /** * @private */ static activateWorkflow(hotMesh: HotMesh, appId?: string, version?: string): Promise; /** * Returns a cached worker instance or creates a new one * @private */ static getInstance(namespace: string, providerConfig: ProviderConfig | ProvidersConfig, options?: VirtualInstanceOptions): Promise; /** * 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 { Virtual } from '@hotmeshio/hotmesh'; * * Virtual.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: VirtualConnectParams): Promise; /** * Calls a function and returns the response. * * @template U - the return type of the linked worker function * * @example * ```typescript * const response = await Virtual.exec({ * topic: 'my.function', * args: [{ my: 'args' }], * connection: { * class: Postgres, * options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' } * } * }); * ``` */ static exec(params: VirtualExecParams): Promise; /** * Clears a cached function response. * * @example * ```typescript * import { Client as Postgres } from 'pg'; * import { Virtual } from '@hotmeshio/hotmesh'; * * Virtual.flush({ * topic: 'my.function', * connection: { * class: Postgres, * options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' } * }, * options: { id: 'myCachedExecFunctionId' } * }); * ``` */ static flush(params: VirtualFlushParams): Promise; /** * 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; /** * 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 { Virtual } from '@hotmeshio/hotmesh'; * * Virtual.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: VirtualCronParams): Promise; /** * 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 { Virtual } from '@hotmeshio/hotmesh'; * * Virtual.interrupt({ * topic: 'my.cron.function', * connection: { * class: Postgres, * options: { connectionString: 'postgresql://usr:pwd@localhost:5432/db' } * }, * options: { id: 'myDailyCron123' } * }); * ``` */ static interrupt(params: VirtualInterruptParams): Promise; /** * Returns the execution context for the current Virtual callback. * Must be called from inside a `Virtual.cron` or `Virtual.exec` * callback — throws if called outside that scope. * * @example * ```typescript * await Virtual.cron({ * topic: 'my.cron', * connection, * args: [], * options: { id: 'daily', interval: '0 0 * * *' }, * callback: async () => { * const ctx = Virtual.getContext(); * console.log(ctx.workflowId); // 'daily' * console.log(ctx.attempt); // 1 * }, * }); * ``` */ static getContext(): VirtualContext; /** * Shuts down all virtual instances. Call this method * from the SIGTERM handler in your application. */ static shutdown(): Promise; } export { Virtual };