The workflow module provides a set of static extension methods that can be called from within a workflow function. In this example, the waitFor extension method is called to add collation to the workflow, only continuing once both outside signals have been received.

//waitForWorkflow.ts
import { MeshFlow } from '@hotmeshio/hotmesh';

export async function waitForExample(): Promise<[boolean, number]> {
const [s1, s2] = await Promise.all([
Meshflow.workflow.waitFor<boolean>('my-sig-nal-1'),
Meshflow.workflow.waitFor<number>('my-sig-nal-2')
]);
//do something with the signal payloads (s1, s2)
return [s1, s2];
}

Properties

executeChild: (<T>(options: WorkflowOptions) => Promise<T>) = WorkflowService.execChild

Type declaration

    • <T>(options): Promise<T>
    • Spawns a child workflow and awaits the return.

      Type Parameters

      • T

        the result type

      Parameters

      Returns Promise<T>

      • the result of the child workflow
      const result = await MeshFlow.workflow.execChild<typeof resultType>({ ...options });
      

Methods

  • Emits an event to the event bus provider (e.g., NATS, Redis, etc.) The topic name will be structured as follows, prefixed with the quorum (q) namespace: hmsh:<namespace>:q:.

    For example, if you provide my.dog as the topic, it will be published at hmsh:myapp:q:my.dog.

    If using NATS as the pubsub provider, the delimiter will be . instead of :.

    If using Postgres, there is a 63 character limit on topic names, so be mindful of the length of the topic. If your topic exceeds 63 characters, it will be hashed by HotMesh to ensure it isn't truncated by Postgres.

    Parameters

    • events: StringAnyType
    • config: {
          once: boolean;
      } = ...
      • once: boolean

    Returns Promise<boolean>

    import { workflow } from '@hotmeshio/hotmesh';

    export async function emitExample(): Promise<boolean> {
    const events = {
    'my.dog': { 'anything': 'goes' },
    'my.dog.cat': { 'anything': 'goes' },
    };
    return await workflow.emit(events);
    }
  • Convenience method for adding custom user data to the backend worflow record. Runs exactly once during workflow execution.

    Parameters

    Returns Promise<boolean>

    import { workflow } from '@hotmeshio/hotmesh';

    export async function enrichExample(): Promise<boolean> {
    const fields = {
    key1: 'value1',
    key2: 'value2',
    };
    return await workflow.enrich(fields);
    }
  • Spawns a child workflow and awaits the return.

    Type Parameters

    • T

      the result type

    Parameters

    Returns Promise<T>

    • the result of the child workflow
    const result = await MeshFlow.workflow.execChild<typeof resultType>({ ...options });
    
  • Spawns a hook from either the main thread or a hook thread with the provided options; worflowId/TaskQueue/Name are optional and will default to the current workflowId/WorkflowTopic if not provided

    Parameters

    Returns Promise<string>

  • Returns a random number between 0 and 1. This number is deterministic and will never vary for a given seed. This is useful for randomizing pathways in a workflow that can be safely replayed.

    Returns number

    • a random number between 0 and 1
  • Returns a search session for use when reading/writing to the workflow HASH. The search session provides access to methods like get, mget, set, del, and incr.

    Returns Promise<Search>

    • a search session
  • Sends signal data into any other paused thread (which is currently awaiting the signal)

    Parameters

    • signalId: string

      the signal id

    • data: Record<any, any>

      the signal data

    Returns Promise<string>

    • the stream id
  • Sleeps the workflow for a duration. As the function is reentrant, upon reentry, the function will traverse prior execution paths up until the sleep command and then resume execution thereafter.

    Parameters

    • duration: string

      See the ms package for syntax examples: '1 minute', '2 hours', '3 days'

    Returns Promise<number>

    • resolved duration in seconds
  • Spawns a child workflow and returns the child Job ID. This method guarantees the spawned child has reserved the Job ID, returning a 'DuplicateJobError' error if not. Otherwise, this is a fire-and-forget method.

    Parameters

    Returns Promise<string>

    • the childJobId
    const childJobId = await MeshFlow.workflow.startChild({ ...options });
    
  • Executes a trace, outputting the provided attributes to the Open Telemetry sink. Executes exactly once during workflow execution.

    It is safe to add this method into any actively running workflow function as long as the trace call would be the last replayable method (by execution order) in the function.

    Parameters

    • attributes: StringScalarType
    • config: {
          once: boolean;
      } = ...
      • once: boolean

    Returns Promise<boolean>

    import { workflow } from '@hotmeshio/hotmesh';

    export async function traceExample(): Promise<boolean> {
    const attributes = {
    key1: 'value1',
    key2: 'value2',
    };
    return await workflow.trace(attributes);
    }
  • Pauses the workflow until signalId is received.

    Type Parameters

    • T

      the result type

    Parameters

    • signalId: string

      a unique, shareable guid (e.g, 'abc123')

    Returns Promise<T>

    const result = await MeshFlow.workflow.waitFor<typeof resultType>('abc123');