Orchestrator API¶
The Orchestrator manages worker lifecycle and coordinates task execution.
Orchestrator.create()¶
Create an orchestrator:
import { Orchestrator } from "pgconductor-js";
const orchestrator = Orchestrator.create({
conductor, // Conductor instance
tasks?, // Tasks for default worker
workers?, // Custom workers
defaultWorker? // Default worker config
});
Parameters:
conductor: Conductor instancetasks: (optional) Array of tasks using default workerworkers: (optional) Array of custom workers fromconductor.createWorker()defaultWorker: (optional) Configuration for default worker
Default worker config:
{
concurrency?: number, // Max concurrent tasks (default: 10)
pollIntervalMs?: number, // Poll interval (default: 1000)
flushIntervalMs?: number, // Flush interval (default: 2000)
fetchBatchSize?: number, // Fetch batch size (default: 10)
}
orchestrator.start()¶
Start the orchestrator:
- Initializes database schema (if needed)
- Starts all workers
- Sets up signal handlers (SIGTERM, SIGINT)
- Begins polling for executions
orchestrator.stop()¶
Stop the orchestrator gracefully:
- Stops accepting new work
- Waits for current tasks to reach checkpoint
- Releases locked executions
- Cleans up database records
- Shuts down workers
orchestrator.drain()¶
Process all queued tasks then stop:
- Starts the orchestrator
- Processes until queue is empty
- Automatically stops
- Returns when all work is done
Use case: Testing
test("processes tasks", async () => {
await orchestrator.drain();
expect(results).toEqual(expected);
});
orchestrator.stopped¶
Promise that resolves when orchestrator stops:
await orchestrator.start();
// Wait for shutdown signal
await orchestrator.stopped;
await sql.end();
orchestrator.info¶
Access orchestrator state:
orchestrator.info.id // Orchestrator ID (UUID)
orchestrator.info.isRunning // boolean
orchestrator.info.isShuttingDown // boolean
orchestrator.info.isStopped // boolean
What's Next?¶
- Worker Configuration - Tune worker performance
- Conductor API - Task creation and invocation
- Horizontal Scaling - Run multiple workers