scheduler

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: AGPL-3.0 Imports: 24 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package scheduler runs tasks on cron schedules with a history of runs (#82, ADR-0048).

Index

Constants

View Source
const (
	TriggerSchedule = "schedule"
	TriggerManual   = "manual"
)

Triggers of runs.

View Source
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.

View Source
const (
	StatusRunning   = "running"
	StatusSucceeded = "succeeded"
	StatusFailed    = "failed"
	StatusSkipped   = "skipped"
)

Run statuses.

View Source
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
)
View Source
const BackupPattern = "nerdui-backup-*.tar.gz.age"

BackupPattern matches the archives WriteBackup writes.

Variables

Kinds lists every kind.

Functions

func ParseCron

func ParseCron(expr string) (cron.Schedule, error)

ParseCron validates a cron expression.

func WriteBackup

func WriteBackup(ctx context.Context, src backup.Source, password, dir string, keep int, now time.Time) (string, error)

WriteBackup writes an archive into dir and keeps the newest keep archives there.

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) Create

func (s *Service) Create(ctx context.Context, in Input, user string) (Schedule, error)

Create adds a schedule.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, id int64) error

Delete removes a schedule and its runs.

func (*Service) List

func (s *Service) List(ctx context.Context) ([]Schedule, error)

List returns every schedule.

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

func (s *Service) NextOf(ctx context.Context, kind, env string) *time.Time

NextOf is the earliest next run of the enabled schedules of a kind in an environment.

func (*Service) Run

func (s *Service) Run(ctx context.Context)

Run starts due schedules until ctx ends. Runs left unfinished by a previous process are marked failed first.

func (*Service) RunNow

func (s *Service) RunNow(ctx context.Context, id int64, user string) (jobs.Job, error)

RunNow starts a schedule at once, also a disabled one.

func (*Service) Runs

func (s *Service) Runs(ctx context.Context, id int64) ([]Run, error)

Runs returns the newest runs of a schedule.

func (*Service) TargetUsers

func (s *Service) TargetUsers(ctx context.Context, target int64) ([]string, error)

TargetUsers names the schedules that write to a volume backup target.

func (*Service) Tick

func (s *Service) Tick(ctx context.Context)

Tick starts the schedules that are due.

func (*Service) Update

func (s *Service) Update(ctx context.Context, id int64, in Input) (Schedule, error)

Update changes a schedule.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL