Documentation
¶
Overview ¶
Package scheduler runs tasks on cron schedules with a history of runs (#82, ADR-0048).
Index ¶
- Constants
- Variables
- func ParseCron(expr string) (cron.Schedule, error)
- func WriteBackup(ctx context.Context, src backup.Source, password, dir string, keep int, ...) (string, error)
- type ActionStarter
- type Environments
- type ImageChecker
- type Input
- type JobRunner
- type Options
- type Run
- type Schedule
- type Service
- func (s *Service) Create(ctx context.Context, in Input, user string) (Schedule, error)
- func (s *Service) Delete(ctx context.Context, id int64) error
- func (s *Service) List(ctx context.Context) ([]Schedule, error)
- func (s *Service) MigrateImageChecks(ctx context.Context, settings Settings, envs []string, ...) error
- func (s *Service) NextOf(ctx context.Context, kind, env string) *time.Time
- func (s *Service) Run(ctx context.Context)
- func (s *Service) RunNow(ctx context.Context, id int64, user string) (jobs.Job, error)
- func (s *Service) Runs(ctx context.Context, id int64) ([]Run, error)
- func (s *Service) TargetUsers(ctx context.Context, target int64) ([]string, error)
- func (s *Service) Tick(ctx context.Context)
- func (s *Service) Update(ctx context.Context, id int64, in Input) (Schedule, error)
- type Settings
- type Store
Constants ¶
const ( TriggerSchedule = "schedule" TriggerManual = "manual" )
Triggers of runs.
const ( KindPrune = "prune" KindBackup = "backup" KindStackRedeploy = "stack.redeploy" KindStackRestart = "stack.restart" KindContainerRedeploy = "container.redeploy" KindContainerRestart = "container.restart" KindImageUpdates = "image-updates.check" KindVolumeBackup = "volume.backup" )
Kinds of scheduled tasks.
const ( StatusRunning = "running" StatusSucceeded = "succeeded" StatusFailed = "failed" StatusSkipped = "skipped" )
Run statuses.
const ( // User is the job user of scheduled runs. User = "nerdui (scheduler)" // KeepRuns is how many runs of a schedule are kept. KeepRuns = 50 // DefaultBackupKeep is how many scheduled backups are kept by default. DefaultBackupKeep = 7 )
const BackupPattern = "nerdui-backup-*.tar.gz.age"
BackupPattern matches the archives WriteBackup writes.
Variables ¶
var Kinds = []string{KindPrune, KindBackup, KindStackRedeploy, KindStackRestart, KindContainerRedeploy, KindContainerRestart, KindImageUpdates, KindVolumeBackup}
Kinds lists every kind.
Functions ¶
Types ¶
type ActionStarter ¶
type ActionStarter interface {
StartAction(ctx context.Context, a webhooks.Action) (jobs.Job, error)
}
ActionStarter restarts or redeploys stacks and containers; *webhooks.Service implements it.
type Environments ¶
type Environments interface {
Get(id string) (*environment.Environment, bool)
}
Environments finds environments; *environment.Registry implements it.
type ImageChecker ¶
type ImageChecker func(ctx context.Context, env *environment.Environment) error
ImageChecker checks an environment's images for updates; the updates package implements it.
type Input ¶
type Input struct {
Name string `json:"name"`
Kind string `json:"kind"`
Environment string `json:"environment"`
Namespace string `json:"namespace"`
Target string `json:"target"`
Options Options `json:"options"`
// Password of backups; empty keeps the stored one.
Password string `json:"password"`
Cron string `json:"cron"`
Enabled bool `json:"enabled"`
}
Input creates or changes a schedule. Keep in sync with api/openapi.yaml.
type JobRunner ¶
type JobRunner interface {
Start(spec jobs.Spec, fn jobs.Func) (jobs.Job, error)
Get(id string) (jobs.Job, bool)
}
JobRunner starts and looks up jobs; *jobs.Manager implements it.
type Options ¶
type Options struct {
// Prune: what to remove, as the Prune dialog.
Containers bool `json:"containers,omitempty"`
Images bool `json:"images,omitempty"`
AllImages bool `json:"allImages,omitempty"`
Networks bool `json:"networks,omitempty"`
BuildCache bool `json:"buildCache,omitempty"`
// Backup: the directory on the host and how many archives to keep there.
Directory string `json:"directory,omitempty"`
Keep int `json:"keep,omitempty"`
// Volume backup (#59): the backup target, and whether containers using the volume are
// stopped while it is read. Keep is shared with backups.
TargetID int64 `json:"targetId,omitempty"`
StopContainers bool `json:"stopContainers,omitempty"`
}
Options are kind-specific settings. Keep in sync with api/openapi.yaml.
type Run ¶
type Run struct {
ID int64 `json:"id"`
Trigger string `json:"trigger"`
StartedAt time.Time `json:"startedAt"`
FinishedAt *time.Time `json:"finishedAt,omitempty"`
Status string `json:"status"`
Message string `json:"message,omitempty"`
JobID string `json:"jobId,omitempty"`
}
Run is a run of a schedule. Keep in sync with api/openapi.yaml.
type Schedule ¶
type Schedule struct {
ID int64 `json:"id"`
Name string `json:"name"`
Kind string `json:"kind"`
Environment string `json:"environment,omitempty"`
Namespace string `json:"namespace,omitempty"`
Target string `json:"target,omitempty"`
Options Options `json:"options"`
// HasPassword is set for backups with a stored password.
HasPassword bool `json:"hasPassword,omitempty"`
Cron string `json:"cron"`
Enabled bool `json:"enabled"`
CreatedBy string `json:"createdBy,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
NextRunAt *time.Time `json:"nextRunAt,omitempty"`
LastRunAt *time.Time `json:"lastRunAt,omitempty"`
LastStatus string `json:"lastStatus,omitempty"`
LastMessage string `json:"lastMessage,omitempty"`
}
Schedule is a schedule without its secret. Keep in sync with api/openapi.yaml.
type Service ¶
type Service struct {
Store Store
Jobs JobRunner
Environments Environments
Actions ActionStarter // nil: stack and container kinds fail
Backup *backup.Source // nil: backups fail
Volumes *volbackup.Service // nil: volume backups fail
CheckImages ImageChecker // nil: image checks fail
Logger *slog.Logger
Location *time.Location // of cron expressions; nil is the server's local zone
// Now, Every and Poll override the clock, the check interval and how often a followed job
// is looked up (tests).
Now func() time.Time
Every time.Duration
Poll time.Duration
// contains filtered or unexported fields
}
Service manages schedules and runs them.
func (*Service) MigrateImageChecks ¶
func (s *Service) MigrateImageChecks(ctx context.Context, settings Settings, envs []string, interval func(ctx context.Context, env string) (time.Duration, error)) error
MigrateImageChecks creates, once per environment, the schedule of image update checks from the interval set before the scheduler existed (#82, ADR-0048).
func (*Service) NextOf ¶
NextOf is the earliest next run of the enabled schedules of a kind in an environment.
func (*Service) Run ¶
Run starts due schedules until ctx ends. Runs left unfinished by a previous process are marked failed first.
func (*Service) TargetUsers ¶
TargetUsers names the schedules that write to a volume backup target.
type Settings ¶
type Settings interface {
GetSetting(ctx context.Context, key string) (string, error)
SetSetting(ctx context.Context, arg db.SetSettingParams) error
}
Settings stores nerdui settings; *db.Queries implements it.
type Store ¶
type Store interface {
ListSchedules(ctx context.Context) ([]db.Schedule, error)
GetSchedule(ctx context.Context, id int64) (db.Schedule, error)
CreateSchedule(ctx context.Context, arg db.CreateScheduleParams) (db.Schedule, error)
UpdateSchedule(ctx context.Context, arg db.UpdateScheduleParams) (db.Schedule, error)
DeleteSchedule(ctx context.Context, id int64) (int64, error)
SetScheduleLastRun(ctx context.Context, arg db.SetScheduleLastRunParams) error
InsertScheduleRun(ctx context.Context, arg db.InsertScheduleRunParams) (int64, error)
FinishScheduleRun(ctx context.Context, arg db.FinishScheduleRunParams) error
SetScheduleRunJob(ctx context.Context, arg db.SetScheduleRunJobParams) error
ListScheduleRuns(ctx context.Context, arg db.ListScheduleRunsParams) ([]db.ScheduleRun, error)
PruneScheduleRuns(ctx context.Context, arg db.PruneScheduleRunsParams) error
FailUnfinishedScheduleRuns(ctx context.Context, now int64) error
}
Store is the database of schedules; *db.Queries of the write connection implements it.