jobrun: error handling through notification channel

This commit is contained in:
Christian Schwarz 2017-04-30 23:30:30 +02:00
parent 9fdd1ea909
commit 226935ddea

View File

@ -10,24 +10,27 @@ type Job struct {
Name string Name string
RunFunc func() (err error) RunFunc func() (err error)
LastStart time.Time LastStart time.Time
LastError error
Interval time.Duration Interval time.Duration
Repeats bool Repeats bool
} }
type JobRunner struct { type JobRunner struct {
newJobChan chan Job notificationChan chan Job
finishedJobChan chan Job newJobChan chan Job
scheduleTimer <-chan time.Time finishedJobChan chan Job
pending map[string]Job scheduleTimer <-chan time.Time
running map[string]Job pending map[string]Job
running map[string]Job
} }
func NewJobRunner() *JobRunner { func NewJobRunner() *JobRunner {
return &JobRunner{ return &JobRunner{
newJobChan: make(chan Job), notificationChan: make(chan Job),
finishedJobChan: make(chan Job), newJobChan: make(chan Job),
pending: make(map[string]Job), finishedJobChan: make(chan Job),
running: make(map[string]Job), pending: make(map[string]Job),
running: make(map[string]Job),
} }
} }
@ -39,6 +42,10 @@ func (r *JobRunner) AddJob(j Job) {
r.newJobChan <- j r.newJobChan <- j
} }
func (r *JobRunner) NotificationChan() <-chan Job {
return r.notificationChan
}
func (r *JobRunner) Start() { func (r *JobRunner) Start() {
loop: loop:
@ -104,7 +111,8 @@ loop:
go func(job Job) { go func(job Job) {
if err := job.RunFunc(); err != nil { if err := job.RunFunc(); err != nil {
panic(fmt.Sprintf("%#v", err)) // TODO better policy, store in job + notification channel? job.LastError = err
r.notificationChan <- job
} }
r.finishedJobChan <- job r.finishedJobChan <- job
}(job) }(job)