• English
  • Build a Custom Adapter

    Implement WorkerGateway to connect the host orchestrator to your worker backend.

    import type {
      WorkerDispatchOptions,
      WorkerGateway,
      WorkerJob,
      WorkerJobResult,
    } from "@shipplane/orchestrator";
    
    export function createMyGateway(client: {
      dispatch(job: WorkerJob, options: WorkerDispatchOptions): Promise<WorkerJobResult>;
    }): WorkerGateway {
      return {
        async dispatch(job, options) {
          return await client.dispatch(job, options);
        },
      };
    }

    To support out-of-band cancellation, observe options.signal inside dispatch().

    Usage

    const gateway = createMyGateway(client);
    
    const orchestrator = createOrchestrator({
      store,
      workerGateway: gateway,
    });

    A production adapter may also need to clean up pending jobs, manage multiple workers, monitor worker health, and use a durable queue.

    In-memory workers can be stopped to unregister handlers and abort active jobs:

    const worker = await gateway.registerWorker({ workerId: "worker-1", handlers });
    
    try {
      const started = await orchestrator.startRun({ workflow });
      await orchestrator.waitForRun(started.state.runId);
    } finally {
      await worker.stop();
    }

    For worker/thread, worker.stop() removes the message listener inside the thread. The host still owns the Node.js Worker and should terminate it during process cleanup.