@cascade-flow/worker
v0.2.33
Published
Distributed worker with step-level execution for CascadeFlow workflow orchestrator
Maintainers
Readme
@cascadeflow/worker
Distributed step-level workflow execution with queue-based processing.
Installation
The worker requires a backend implementation to persist workflow state. Install the worker along with your chosen backend:
With Filesystem Backend (Recommended for Development)
npm install @cascadeflow/worker @cascadeflow/backend-filesystemWith PostgreSQL Backend (Production)
npm install @cascadeflow/worker @cascadeflow/backend-postgresUsage
CLI
cf worker start [--mode unified|executor|scheduler] [--concurrency n]Programmatic
import { StepWorker } from "@cascade-flow/worker";
import { FileSystemBackend } from "@cascade-flow/backend-filesystem";
const worker = new StepWorker(new FileSystemBackend("./.runs"), {
mode: "unified",
concurrency: 5,
pollInterval: 5000,
});
await worker.start();Note: Worker automatically discovers workflows with support for nested step groups. Steps can be organized in arbitrary directory hierarchies (e.g., steps/data-processing/extract/fetch-data/step.ts). Step IDs in the registry will reflect the full path.
Architecture
Events-as-queue pattern with 4 concurrent loops:
- Executor - Claims & executes scheduled steps
- Scheduler - Schedules ready steps, detects completion
- Heartbeat - Proves worker liveness (5s interval)
- Reclamation - Reclaims steps from crashed workers (30s threshold)
Modes
- Unified (default) - All 4 loops in one process
- Executor - Execute + heartbeat only (needs separate scheduler)
- Scheduler - Schedule + reclamation only (needs separate executors)
Timeouts
Step execution (3-tier fallback):
defineStep({ timeoutMs })- Highest prioritysubmit({ timeout })- Workflow-wide default300000ms(5 min) - System default
Worker health monitoring:
- Stale threshold (default 30s) - Detects crashed workers via missing heartbeats
Failure Recovery
- Worker crash → Stale heartbeat → Step reclaimed → Rescheduled
- Step timeout →
StepFailedevent → Retry with delay (if configured) - Terminal failure → Scheduler stops scheduling →
WorkflowFailed
Checkpoints
Workers track checkpoint progress within steps via StepCheckpoint and StepCheckpointFailed events. This enables:
- Fine-grained progress tracking for long-running steps
- Replay of completed checkpoints on retry (skip already-done work)
- Pinpoint error locations when failures occur mid-step
API
interface StepWorkerOptions {
workerId?: string;
mode?: "unified" | "executor" | "scheduler";
concurrency?: number;
pollInterval?: number;
heartbeatInterval?: number;
staleThreshold?: number;
schedulerInterval?: number;
shutdownTimeout?: number;
workflowsDir?: string;
baseDir?: string;
}
class StepWorker {
constructor(backend?: Backend, options?: StepWorkerOptions);
async start(): Promise<void>;
async stop(): Promise<void>;
getStats(): StepWorkerStats;
}