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,11 +10,13 @@ type Job struct {
Name string
RunFunc func() (err error)
LastStart time.Time
LastError error
Interval time.Duration
Repeats bool
}
type JobRunner struct {
notificationChan chan Job
newJobChan chan Job
finishedJobChan chan Job
scheduleTimer <-chan time.Time
@ -24,6 +26,7 @@ type JobRunner struct {
func NewJobRunner() *JobRunner {
return &JobRunner{
notificationChan: make(chan Job),
newJobChan: make(chan Job),
finishedJobChan: make(chan Job),
pending: make(map[string]Job),
@ -39,6 +42,10 @@ func (r *JobRunner) AddJob(j Job) {
r.newJobChan <- j
}
func (r *JobRunner) NotificationChan() <-chan Job {
return r.notificationChan
}
func (r *JobRunner) Start() {
loop:
@ -104,7 +111,8 @@ loop:
go func(job Job) {
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
}(job)