func reloadJobsFromDB(cr *cron.Cron, error_jobs map[string]error, backend *dbBackend, arguments map[string]interface{}) error { jobs, e := backend.snapshot(nil) if nil != e { return errors.New("load snapshot from db failed, " + e.Error()) } versions := map[int64]version{} for _, v := range jobs { versions[v.id] = v } for _, ent := range cr.Entries() { if job, ok := ent.Job.(*JobFromDB); ok { if v, ok := versions[job.id]; ok { if !v.updated_at.Equal(job.updated_at) { reloadJobFromDB(cr, error_jobs, backend, arguments, job.id, job.name) } delete(versions, job.id) } else { log.Println("[sys] delete job -", job.name) cr.Unschedule(fmt.Sprint(job.id)) delete(error_jobs, fmt.Sprint(job.id)) } } } for id, _ := range versions { reloadJobFromDB(cr, error_jobs, backend, arguments, id, "") } return nil }
func reloadJobFromDB(cr *cron.Cron, error_jobs map[string]error, backend *dbBackend, arguments map[string]interface{}, id int64, name string) { message_prefix := "[sys] reload job -" if "" == name { message_prefix = "[sys] load new job -" } job, e := backend.find(id) if nil != e { if "" == name { log.Println(message_prefix, "[", id, "]") } else { log.Println(message_prefix, name) } return } e = afterLoad(job, arguments) if nil != e { log.Println(message_prefix, job.name) return } id_str := fmt.Sprint(id) log.Println(message_prefix, job.name) cr.Unschedule(id_str) delete(error_jobs, id_str) sch, e := Parse(job.expression) if nil != e { msg := errors.New("[" + job.name + "] schedule failed," + e.Error()) error_jobs[id_str] = msg log.Println(msg) return } cr.Schedule(id_str, sch, job) }
func Schedule(c *cron.Cron, id string, schedule cron.Schedule, cmd cron.Job) { c.Schedule(id, schedule, cmd) }