Documentation
¶
Overview ¶
Package workers provides background job management with periodic execution, scheduling, retry logic, and graceful shutdown.
Workers is designed for running background tasks like data processing, cleanup jobs, API synchronization, and scheduled reports.
Basic Usage ¶
Create a WorkerManager, register workers with their jobs, and start:
wm := workers.NewWorkerManager()
wm.RegisterWorker(workers.NewWorker("cleanup", cleanupJob))
wm.Start()
Jobs are functions that receive a context and logger:
func cleanupJob(ctx context.Context, log *slog.Logger) error {
log.Info("running cleanup")
return nil
}
Configuration ¶
Configure workers using functional options:
workers.NewWorker("sync", syncJob,
workers.WithTick(5*time.Minute),
workers.WithTimeout(30*time.Second),
workers.WithNRetries(3),
)
Scheduling ¶
Run jobs at specific times instead of intervals:
workers.WithSchedules(
workers.DailyAt(9, 0),
workers.WeeklyAt(time.Monday, 14, 30),
)
Retry and Backoff ¶
Automatic retries with configurable backoff strategies:
workers.WithNRetries(5), workers.WithRetryDelay(10*time.Second), workers.WithBackoffStrategy(workers.ExponentialBackoff)
Available strategies: ConstantBackoff, LinearBackoff, ExponentialBackoff. Custom strategies can be defined as: func(time.Duration, int) time.Duration
Graceful Shutdown ¶
The library handles SIGINT and SIGTERM signals, allowing running jobs to complete before shutdown.
Index ¶
- type BackoffStrategy
- type Schedule
- type Worker
- type WorkerJob
- type WorkerManager
- type WorkerManagerOption
- type WorkerOption
- func WithBackoffStrategy(strategy BackoffStrategy) WorkerOption
- func WithItsOwnLogger(logger *slog.Logger) WorkerOption
- func WithNRetries(n int) WorkerOption
- func WithNRuns(n int) WorkerOption
- func WithRetryDelay(delay time.Duration) WorkerOption
- func WithSchedules(schedules ...Schedule) WorkerOption
- func WithTick(tick time.Duration) WorkerOption
- func WithTimeout(timeout time.Duration) WorkerOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BackoffStrategy ¶
BackoffStrategy defines how long to wait between retry attempts. The baseDelay is the initial delay configured, and attempt is 1-based (first retry is attempt 1). Return the calculated delay duration.
var ( // ConstantBackoff returns a fixed delay regardless of attempt number. // // Example: 5s, 5s, 5s, 5s... ConstantBackoff BackoffStrategy = func(baseDelay time.Duration, attempt int) time.Duration { return baseDelay } // LinearBackoff increases delay linearly with each attempt. // Formula: delay * attempt // // Example: 5s, 10s, 15s, 20s... LinearBackoff BackoffStrategy = func(baseDelay time.Duration, attempt int) time.Duration { return baseDelay * time.Duration(attempt) } // ExponentialBackoff doubles the delay with each attempt. // Formula: delay * 2^attempt // // Example: 5s, 10s, 20s, 40s... ExponentialBackoff BackoffStrategy = func(baseDelay time.Duration, attempt int) time.Duration { return baseDelay * time.Duration(math.Pow(2, float64(attempt))) } )
Predefined backoff strategies provide common retry delay patterns. All strategies are type-safe and implement the BackoffStrategy type.
type Schedule ¶
type Schedule struct {
// contains filtered or unexported fields
}
Schedule defines when a worker should execute. Use DailyAt, WeeklyAt, or EveryNDays to create schedules. Modify with the In method to set timezones.
func DailyAt ¶
DailyAt creates a schedule that runs daily at the specified hour and minute. Hour must be between 0-23 and minute must be between 0-59.
Example:
workers.DailyAt(9, 0) // Daily at 9:00 AM workers.DailyAt(14, 30) // Daily at 2:30 PM
func EveryNDays ¶
EveryNDays creates a schedule that runs every N days at the specified time. The anchor date is set to today at midnight, so the pattern starts from today. For example, if created on Monday with n=2, it will run on Mon, Wed, Fri, Sun, etc. Hour must be between 0-23 and minute must be between 0-59.
Example:
workers.EveryNDays(2, 9, 0) // Every 2 days at 9:00 AM workers.EveryNDays(7, 14, 0) // Weekly at 2:00 PM - same as WeeklyAt(time.Now().Weekday(), hour, minute)
func WeeklyAt ¶
WeeklyAt creates a schedule that runs weekly on a specific day at the specified time. Weekday specifies which day of the week (time.Monday through time.Sunday). Hour must be between 0-23 and minute must be between 0-59.
Example:
workers.WeeklyAt(time.Monday, 9, 0) // Every Monday at 9:00 AM workers.WeeklyAt(time.Friday, 17, 30) // Every Friday at 5:30 PM
func (Schedule) In ¶
In sets the timezone for the schedule and returns the modified schedule. By default, schedules use the local timezone. Use this to specify a different timezone.
Example:
workers.DailyAt(9, 0).In(time.UTC) // Daily at 9:00 AM UTC
workers.WeeklyAt(time.Monday, 9, 0).In(time.FixedZone("EST", -5*60*60)) // Monday at 9:00 AM EST
type Worker ¶
type Worker struct {
// contains filtered or unexported fields
}
Worker manages a background job with configurable execution intervals, retry logic, timeouts, and scheduling. Workers can run periodically or at specific times defined by schedules.
type WorkerJob ¶
WorkerJob defines the function signature for job implementations. The function receives a context for cancellation and timeout handling, and a logger for structured logging within the job.
type WorkerManager ¶
type WorkerManager struct {
// contains filtered or unexported fields
}
WorkerManager coordinates multiple workers, managing their lifecycle from startup to graceful shutdown. It handles OS signals (SIGINT, SIGTERM) and recovers from worker panics to ensure system stability.
func NewWorkerManager ¶
func NewWorkerManager(options ...WorkerManagerOption) WorkerManager
NewWorkerManager creates a manager for coordinating workers. Configure with options before registering workers and calling Start.
func (*WorkerManager) RegisterWorker ¶
func (me *WorkerManager) RegisterWorker(worker Worker)
RegisterWorker adds a worker to the manager. The worker inherits the manager's logger if it doesn't have its own. Workers must be registered before Start is called.
func (WorkerManager) Start ¶
func (me WorkerManager) Start()
Start runs all registered workers and blocks until shutdown. Workers execute concurrently. The method blocks until SIGINT or SIGTERM is received, then waits for running jobs to complete before returning.
type WorkerManagerOption ¶
type WorkerManagerOption func(*WorkerManager)
WorkerManagerOption configures a WorkerManager. Use WithLogger to create options.
func WithLogger ¶
func WithLogger(logger *slog.Logger) WorkerManagerOption
WithLogger sets the default logger for the manager and all workers without their own logger.
Default: slog.Default()
type WorkerOption ¶
type WorkerOption func(*Worker)
WorkerOption configures a Worker. Use the With* functions to create options.
func WithBackoffStrategy ¶
func WithBackoffStrategy(strategy BackoffStrategy) WorkerOption
WithBackoffStrategy sets how the retry delay increases with each attempt. Use ConstantBackoff, LinearBackoff, ExponentialBackoff, or a custom function.
Default: ConstantBackoff
func WithItsOwnLogger ¶
func WithItsOwnLogger(logger *slog.Logger) WorkerOption
WithItsOwnLogger sets a worker-specific logger, overriding the WorkerManager's logger. The logger must not be nil.
Default: inherits from WorkerManager
func WithNRetries ¶
func WithNRetries(n int) WorkerOption
WithNRetries sets how many times to retry a failed job. Set to 0 to disable retries.
Default: 3 retries
func WithNRuns ¶
func WithNRuns(n int) WorkerOption
WithNRuns limits the total number of times a worker executes. After reaching the limit, the worker stops automatically. Must be greater than 0.
Default: unlimited runs
func WithRetryDelay ¶
func WithRetryDelay(delay time.Duration) WorkerOption
WithRetryDelay sets the initial delay between retry attempts. The actual delay may increase based on the backoff strategy. Must be greater than 0.
Default: 5 seconds
func WithSchedules ¶
func WithSchedules(schedules ...Schedule) WorkerOption
WithSchedules sets specific times for the worker to run, replacing tick-based execution. When schedules are configured, the worker runs only at the specified times. Multiple schedules can be combined to run at different times.
Example:
workers.WithSchedules(
workers.DailyAt(9, 0),
workers.WeeklyAt(time.Friday, 14, 30),
workers.EveryNDays(2, 2, 0),
)
func WithTick ¶
func WithTick(tick time.Duration) WorkerOption
WithTick sets the interval between job executions. Ignored when schedules are set. The tick must be greater than 0.
Default: 1 hour
func WithTimeout ¶
func WithTimeout(timeout time.Duration) WorkerOption
WithTimeout sets a maximum execution time for each job run. Jobs exceeding this duration are cancelled via context. Must be greater than 0.
Default: no timeout (jobs run indefinitely)