From 226935ddeaf8e36650db10d82e0a52d777359b6e Mon Sep 17 00:00:00 2001 From: Christian Schwarz Date: Sun, 30 Apr 2017 23:30:30 +0200 Subject: [PATCH] jobrun: error handling through notification channel --- jobrun/jobrun.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/jobrun/jobrun.go b/jobrun/jobrun.go index 401f9cb..9a0b588 100644 --- a/jobrun/jobrun.go +++ b/jobrun/jobrun.go @@ -10,24 +10,27 @@ type Job struct { Name string RunFunc func() (err error) LastStart time.Time + LastError error Interval time.Duration Repeats bool } type JobRunner struct { - newJobChan chan Job - finishedJobChan chan Job - scheduleTimer <-chan time.Time - pending map[string]Job - running map[string]Job + notificationChan chan Job + newJobChan chan Job + finishedJobChan chan Job + scheduleTimer <-chan time.Time + pending map[string]Job + running map[string]Job } func NewJobRunner() *JobRunner { return &JobRunner{ - newJobChan: make(chan Job), - finishedJobChan: make(chan Job), - pending: make(map[string]Job), - running: make(map[string]Job), + notificationChan: make(chan Job), + newJobChan: make(chan Job), + finishedJobChan: make(chan Job), + pending: make(map[string]Job), + running: 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)