-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
43 lines (36 loc) · 951 Bytes
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package main
import (
"fmt"
"log"
"github.com/pkg/errors"
"github.com/robfig/cron/v3"
)
// Scheduler handles running jobs on a given cron Schedule.
type Scheduler struct {
cron *cron.Cron
}
// NewScheduler returns a new Scheduler instance.
func NewScheduler() *Scheduler {
return &Scheduler{cron.New()}
}
// Run the cron scheduler, or no-op if already running.
func (s *Scheduler) Run() {
s.cron.Run()
}
// JobDefinition for working with the Scheduler.
type JobDefinition struct {
Name string
Schedule string
Run func() error
}
// AddJob adds a job to the Scheduler to be run on the given spec schedule and name.
func (s *Scheduler) AddJob(job *JobDefinition) error {
if _, err := s.cron.AddFunc(job.Schedule, func() {
if err := job.Run(); err != nil {
log.Printf("%s job error: %v", job.Name, err)
}
}); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to schedule %s worker", job.Name))
}
return nil
}