← repoman internals

internal/scheduler

import "git.griefed.de/griefed/repoman/internal/scheduler"
package scheduler // import "git.griefed.de/griefed/repoman/internal/scheduler"

Package scheduler wraps robfig/cron to fire migration runs on a cron schedule.
It keeps repoman's daemon goroutine model clean: Scheduler.Run(ctx) blocks until
the context is cancelled, at which point the cron runner is stopped and all
pending jobs drain.

Design choices:
  - One cron.Cron instance, rebuilt from scratch whenever the config list
    changes. This avoids the complexity of dynamic job add/remove and the
    potential for stale state.
  - Rebuild is triggered on a 60-second poll tick; fine-grained change detection
    is not needed because cron schedules change rarely and a one-minute lag is
    acceptable.
  - The scheduler talks to the run queue via run.QueueRepository, not directly
    to the worker. This keeps it decoupled from the worker's lifecycle and
    allows tests to verify queue entries without running the full worker.

FUNCTIONS

func NextRun(expr string, from time.Time) *time.Time
    NextRun returns the next scheduled run time for a config expression, or nil
    if the expression is invalid or empty. It parses through the same cronParser
    the live runner uses, so a non-nil result guarantees the scheduler would
    actually accept and fire the expression.


TYPES

type Scheduler struct {
	Configs migrationconfig.Repository
	Queue   run.QueueRepository
	Now     func() time.Time

	// RebuildInterval is how often the scheduler reloads configs and
	// rebuilds the cron runner. Default 60 s.
	RebuildInterval time.Duration

	// Has unexported fields.
}
    Scheduler polls for enabled configs with cron expressions and enqueues runs
    at the right time.

func NewScheduler(
	configs migrationconfig.Repository,
	queue run.QueueRepository,
) *Scheduler
    NewScheduler constructs a Scheduler.

func (s *Scheduler) Run(ctx context.Context)
    Run starts the scheduler loop. Blocks until ctx is cancelled.