Documentation
¶
Overview ¶
Package daemon provides a Kubernetes-native daemon runtime with graceful lifecycle management. This package enables building long-running services with health checks, task management, and graceful shutdown handling.
Index ¶
- type Config
- type Daemon
- type Job
- type Scheduler
- func (s *Scheduler) AddJob(name string, schedule string, fn func(ctx context.Context) error) error
- func (s *Scheduler) AddJobWithInterval(name string, interval time.Duration, fn func(ctx context.Context) error) error
- func (s *Scheduler) GetNextRun(jobName string) (time.Time, bool)
- func (s *Scheduler) IsRunning() bool
- func (s *Scheduler) JobCount() int
- func (s *Scheduler) Name() string
- func (s *Scheduler) Start(ctx context.Context) error
- func (s *Scheduler) Stop(ctx context.Context) error
- type Task
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// HealthPort is the port for health check endpoints (default: 9090)
HealthPort int
// ShutdownTimeout is the maximum time to wait for graceful shutdown
ShutdownTimeout time.Duration
// Logger is the structured logger to use
Logger *slog.Logger
}
Config holds daemon configuration.
type Daemon ¶
type Daemon struct {
// contains filtered or unexported fields
}
Daemon manages the lifecycle of a long-running service.
func (*Daemon) RegisterTask ¶
RegisterTask adds a task to be managed by the daemon.
type Job ¶
type Job struct {
Name string
Schedule string // cron expression (e.g., "0 */6 * * *" for every 6 hours)
Fn func(ctx context.Context) error
}
Job represents a scheduled job.
type Scheduler ¶
type Scheduler struct {
// contains filtered or unexported fields
}
Scheduler manages periodic job execution using cron expressions.
func NewScheduler ¶
NewScheduler creates a scheduler with the given logger.
func (*Scheduler) AddJob ¶
AddJob adds a job with a cron expression. Cron expressions support 6 fields: second minute hour day month weekday Examples:
- "0 0 */6 * * *" - every 6 hours at minute 0, second 0
- "0 0 0 * * *" - daily at midnight
- "0 */30 * * * *" - every 30 minutes
- "@every 1h" - every hour (robfig/cron shorthand)
- "@daily" - once a day at midnight
func (*Scheduler) AddJobWithInterval ¶
func (s *Scheduler) AddJobWithInterval(name string, interval time.Duration, fn func(ctx context.Context) error) error
AddJobWithInterval adds a job that runs at a fixed interval. This is a convenience method that converts the interval to a cron expression.
func (*Scheduler) GetNextRun ¶
GetNextRun returns the next scheduled run time for a job.
type Task ¶
type Task interface {
// Name returns a human-readable name for logging
Name() string
// Start begins the task. It should block until ctx is cancelled.
Start(ctx context.Context) error
// Stop performs cleanup. Called after Start returns.
Stop(ctx context.Context) error
}
Task represents a background task managed by the daemon.