Documentation
¶
Index ¶
Constants ¶
const ( WorkerStatusHealthy = "healthy" WorkerStatusFailed = "failed" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HealthTracker ¶
type HealthTracker struct {
// contains filtered or unexported fields
}
HealthTracker tracks the health status of all workers. It is safe for concurrent use.
func NewHealthTracker ¶
func NewHealthTracker() *HealthTracker
NewHealthTracker creates a new HealthTracker.
func (*HealthTracker) GetStatus ¶
func (h *HealthTracker) GetStatus() map[string]interface{}
GetStatus returns the overall health status with details of all workers.
func (*HealthTracker) IsHealthy ¶
func (h *HealthTracker) IsHealthy() bool
IsHealthy returns true if all workers are healthy.
func (*HealthTracker) MarkFailed ¶
func (h *HealthTracker) MarkFailed(name string)
MarkFailed marks a worker as failed. Note: Error details are NOT stored for security reasons.
func (*HealthTracker) MarkHealthy ¶
func (h *HealthTracker) MarkHealthy(name string)
MarkHealthy marks a worker as healthy.
type Logger ¶
type Logger interface {
Info(msg string, fields ...zap.Field)
Error(msg string, fields ...zap.Field)
Debug(msg string, fields ...zap.Field)
Warn(msg string, fields ...zap.Field)
}
Logger is a minimal logging interface for structured logging with zap.
type SupervisorOption ¶
type SupervisorOption func(*WorkerSupervisor)
SupervisorOption configures a WorkerSupervisor.
func WithShutdownTimeout ¶
func WithShutdownTimeout(timeout time.Duration) SupervisorOption
WithShutdownTimeout sets the maximum time to wait for workers to shutdown gracefully. After this timeout, Run() will return even if workers haven't finished. Default is 0 (no timeout - wait indefinitely).
type Worker ¶
type Worker interface {
// Name returns a unique identifier for this worker (e.g., "http-server", "retrymq-consumer")
Name() string
// Run executes the worker and blocks until context is cancelled or error occurs.
// Returns nil or context.Canceled for graceful shutdown.
// Returns error for fatal failures.
Run(ctx context.Context) error
}
Worker represents a long-running background process. Each worker runs in its own goroutine and can be monitored for health.
Workers should: - Block in Run() until context is cancelled or a fatal error occurs - Return nil or context.Canceled for graceful shutdown - Return non-nil error only for fatal failures
type WorkerHealth ¶
type WorkerHealth struct {
Status string `json:"status"` // "healthy" or "failed"
}
WorkerHealth represents the health status of a single worker. Error details are NOT exposed for security reasons.
type WorkerSupervisor ¶
type WorkerSupervisor struct {
// contains filtered or unexported fields
}
WorkerSupervisor manages and supervises multiple workers. It tracks their health and handles graceful shutdown.
func NewWorkerSupervisor ¶
func NewWorkerSupervisor(logger Logger, opts ...SupervisorOption) *WorkerSupervisor
NewWorkerSupervisor creates a new WorkerSupervisor.
func (*WorkerSupervisor) GetHealthTracker ¶
func (r *WorkerSupervisor) GetHealthTracker() *HealthTracker
GetHealthTracker returns the health tracker for this supervisor.
func (*WorkerSupervisor) Register ¶
func (r *WorkerSupervisor) Register(w Worker)
Register adds a worker to the supervisor. Panics if a worker with the same name is already registered.
func (*WorkerSupervisor) Run ¶
func (r *WorkerSupervisor) Run(ctx context.Context) error
Run starts all registered workers and supervises them. It blocks until: - ALL workers have exited (either successfully or with errors), OR - The context is cancelled (SIGTERM/SIGINT)
When a worker fails, it marks the worker as failed but does NOT terminate other workers. This allows: - Other workers to continue serving (e.g., HTTP server stays up) - Health checks to report the failed worker status - Orchestrator to detect failure and restart the pod/container
Returns nil if context was cancelled and workers shutdown gracefully. Returns error if workers failed to shutdown within timeout (if configured).