Coordination Package - Event Coordination System (Spinal Cord)
Overview
The coordination package provides the central event coordination system for the entire codebase. It serves as the "spinal cord" of the system, routing events from all operations to appropriate channels (logging, audit, metrics, operational) and enabling process coordination through event subscriptions.
Architecture
Core Concept
Operation/Task/Job
β
ββββ EventCoordinator (Spinal Cord)
β
ββββ Logging Channel (EventLogger)
ββββ Audit Channel (audit_event objects)
ββββ Metrics Channel (MetricPipeline)
ββββ Operational Channel (Subscribers)
Key Components
- EventCoordinator: Central coordinator that routes events to all channels
- EventContext: Rich context objects with operation metadata and routing decisions
- OperationalEventSubscriber: Interface for subscribers that coordinate processes
- ChannelRouter: Routes events to appropriate channels based on context
Usage
Basic Usage
import "github.com/zqk-os/zqk/pkg/coordination"
// Get the global coordinator
coordinator := coordination.GetCoordinator()
// Create event context
eventCtx := &coordination.EventContext{
OperationID: "snapshot_expand_12345",
OperationType: "snapshot_expand",
Status: "complete",
// ... event data
EmitLogging: true,
EmitAudit: true,
EmitMetrics: true,
EmitOperational: true,
}
// Emit event (routes to all enabled channels)
coordinator.Emit(ctx, eventCtx)
Subscribing to Operational Events
subscriber := &MySubscriber{
id: "my-subscriber",
}
coordinator.Subscribe(subscriber)
Integration
The coordinator integrates with:
pkg/logging.EventLogger- Structured loggingpkg/storage(audit events) - Audit trailpkg/metrics.MetricPipeline- Metrics collectionpkg/mcp.EventEmitter- Operational events for MCP clientspkg/scheduler- Process coordination
Scheduler coordination kernel
The scheduler participates in the same coordination spine as the rest of the CLI:
- High-level execution lifecycle β Each job run can use
coordination.NewCoordinatorOperationCallback(pkg/scheduler/job_execution.go) soOnStart/ completion hooks align with the global operation-callback pattern used elsewhere. - Granular job events β
emitJobExecutionEventViaCoordinator(pkg/scheduler/job_execution_coordination.go) emitsscheduler_job_started,scheduler_job_completed, andscheduler_job_failedthrough the storage audit router and structured logging fields, keeping audit/metrics/logging consistent with coordinator-shaped metadata.
Together these paths give a single, observable story for βwhat the scheduler didβ without duplicating ad hoc logging at every handler.
Documentation
For architecture standards and system overview:
Design Principles
- Single Source of Truth: One event emission point, multiple channels
- Context-Aware: Events carry rich context for routing and coordination
- Non-Blocking: All channel emissions are async and non-blocking
- Thread-Safe: Concurrent access from multiple goroutines
- Extensible: Easy to add new channels or subscribers
- Observable: All events are observable programmatically
Benefits
- System-Wide Awareness: Single coordination point for all events
- Process Coordination: Subscribers can sequence operations based on events
- Consistency: All observability layers use the same event source
- Efficiency: Shared event data, no duplication
- Coordination: Causal chains can be sequenced automatically