• English
  • Custom Adapter

    A custom adapter connects the host orchestrator to a worker backend by implementing WorkerGateway.

    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);
        },
      };
    }

    Adapters that need out-of-band cancellation should observe options.signal inside dispatch().

    Usage

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

    Production adapters should add cleanup for pending jobs, multiple workers, worker health, and durable queues if needed.

    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 Worker resource and should terminate it during process cleanup.