diff options
-rw-r--r-- | checkjobs.go (renamed from checkmails.go) | 38 | ||||
-rw-r--r-- | main.go | 4 | ||||
-rw-r--r-- | model/mysql/jobs.go | 5 |
3 files changed, 29 insertions, 18 deletions
diff --git a/checkmails.go b/checkjobs.go index 576c2c2..2b4af74 100644 --- a/checkmails.go +++ b/checkjobs.go @@ -10,14 +10,14 @@ import ( var checkInterval int64 -func initCheckmails() { +func initCheckjobs() { var err error if checkInterval, err = conf.GetInt("schedules", "checkInterval"); err != nil { log.Fatalf("Could not read config schedules.checkInterval: %s", err) } } -func checkmails() { +func checkjobs() { timech := make(chan time.Time) go func(ch chan time.Time) { ticker := time.NewTicker(time.Duration(checkInterval) * time.Second) @@ -29,19 +29,29 @@ func checkmails() { }(timech) for t := range timech { - jobs := dbcon.JobsBefore(t) + checkjobsOnce(t) + } +} + +func checkjobsOnce(t time.Time) { + defer func() { + if r := recover(); r != nil { + log.Printf("!! recovered from panic in checkjobsOnce: %s", r) + } + }() + + jobs := dbcon.JobsBefore(t) - for _, job := range jobs { - if sendjob(job, t) { - next := job.Chronos().NextAfter(t) - if next.IsZero() { - if err := job.Delete(); err != nil { - log.Printf("Failed deleting job %s after job was done: %s", job.ID(), err) - } - } else { - if err := job.SetNext(next); err != nil { - log.Printf("Filed setting next for job %s: %s", job.ID(), err) - } + for _, job := range jobs { + if sendjob(job, t) { + next := job.Chronos().NextAfter(t) + if next.IsZero() { + if err := job.Delete(); err != nil { + log.Printf("Failed deleting job %s after job was done: %s", job.ID(), err) + } + } else { + if err := job.SetNext(next); err != nil { + log.Printf("Filed setting next for job %s: %s", job.ID(), err) } } } @@ -79,8 +79,8 @@ func main() { log.Fatalf("Could not get net.laddr config: %s", err) } - initCheckmails() - go checkmails() + initCheckjobs() + go checkjobs() router := mux.NewRouter() router.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticpath)))) diff --git a/model/mysql/jobs.go b/model/mysql/jobs.go index 2695d50..1b9598b 100644 --- a/model/mysql/jobs.go +++ b/model/mysql/jobs.go @@ -134,7 +134,8 @@ func (j *Job) Next() time.Time { return j.next } func (j *Job) User() model.User { u, err := j.con.UserByID(j.user) if err != nil { - // TODO: Should we really panic here? If yes, we need to recover panics! + // We panic here, since the user must exist, if the job is there. + // Since http handlers and the job handler do recover from panics, this should be okay. panic(fmt.Errorf("Could not get user (%d) of Job %d: %s", j.user, j.id, err)) } @@ -185,7 +186,7 @@ func (j *Job) Delete() error { func (con *MySQLDBCon) JobsBefore(t time.Time) (jobs []model.Job) { rows, err := con.stmt[qJobsBefore].Query(t.Unix()) if err != nil { - log.Fatalf("Could not get jobs before %s: %s", t, err) // TODO: Really fatal? + log.Fatalf("Could not get jobs before %s: %s", t, err) } for rows.Next() { |