Skip to content

Commit

Permalink
feat: job config
Browse files Browse the repository at this point in the history
Signed-off-by: jolheiser <[email protected]>
  • Loading branch information
jolheiser committed Oct 12, 2023
1 parent d1ab2b5 commit 8c93795
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
8 changes: 8 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ type LFSConfig struct {
SSHEnabled bool `env:"SSH_ENABLED" yaml:"ssh_enabled"`
}

// JobsConfig is the configuration for cron jobs
type JobsConfig struct {
MirrorPull string `env:"MIRROR_PULL" yaml:"mirror_pull"`
}

// Config is the configuration for Soft Serve.
type Config struct {
// Name is the name of the server.
Expand All @@ -131,6 +136,9 @@ type Config struct {
// LFS is the configuration for Git LFS.
LFS LFSConfig `envPrefix:"LFS_" yaml:"lfs"`

// Jobs is the configuration for cron jobs
Jobs JobsConfig `envPrefix:"JOBS_" yaml:"jobs"`

// InitialAdminKeys is a list of public keys that will be added to the list of admins.
InitialAdminKeys []string `env:"INITIAL_ADMIN_KEYS" envSeparator:"\n" yaml:"initial_admin_keys"`

Expand Down
15 changes: 10 additions & 5 deletions server/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ import (

// Job is a job that can be registered with the scheduler.
type Job struct {
ID int
Spec string
Func func(context.Context) func()
ID int
Runner Runner
}

// Runner is a job runner
type Runner interface {
Spec(context.Context) string
Func(context.Context) func()
}

var (
Expand All @@ -18,10 +23,10 @@ var (
)

// Register registers a job.
func Register(name, spec string, fn func(context.Context) func()) {
func Register(name string, runner Runner) {
mtx.Lock()
defer mtx.Unlock()
jobs[name] = &Job{Spec: spec, Func: fn}
jobs[name] = &Job{Runner: runner}
}

// List returns a map of registered jobs.
Expand Down
17 changes: 14 additions & 3 deletions server/jobs/mirror.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,22 @@ import (
)

func init() {
Register("mirror-pull", "@every 10m", mirrorPull)
Register("mirror-pull", mirrorPull{})
}

// mirrorPull runs the (pull) mirror job task.
func mirrorPull(ctx context.Context) func() {
type mirrorPull struct{}

// Spec derives the spec used for pull mirrors and implements Runner
func (m mirrorPull) Spec(ctx context.Context) string {
cfg := config.FromContext(ctx)
if cfg.Jobs.MirrorPull != "" {
return cfg.Jobs.MirrorPull
}
return "@every 10m"
}

// Func runs the (pull) mirror job task and implements Runner
func (m mirrorPull) Func(ctx context.Context) func() {
cfg := config.FromContext(ctx)
logger := log.FromContext(ctx).WithPrefix("jobs.mirror")
b := backend.FromContext(ctx)
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewServer(ctx context.Context) (*Server, error) {
// Add cron jobs.
sched := cron.NewScheduler(ctx)
for n, j := range jobs.List() {
id, err := sched.AddFunc(j.Spec, j.Func(ctx))
id, err := sched.AddFunc(j.Runner.Spec(ctx), j.Runner.Func(ctx))
if err != nil {
logger.Warn("error adding cron job", "job", n, "err", err)
}
Expand Down

0 comments on commit 8c93795

Please sign in to comment.