/
schedules.go
71 lines (63 loc) · 1.63 KB
/
schedules.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package server
import (
"github.com/askasoft/pango-xdemo/app"
"github.com/askasoft/pango-xdemo/app/jobs"
"github.com/askasoft/pango-xdemo/app/tasks"
"github.com/askasoft/pango/cog"
"github.com/askasoft/pango/log"
"github.com/askasoft/pango/sch"
)
var schedules = cog.NewLinkedHashMap[string, func()](
cog.KV("tmpClean", tasks.CleanTemporaryFiles),
cog.KV("jobStart", jobs.Start),
cog.KV("jobReappend", jobs.Reappend),
cog.KV("jobClean", jobs.CleanOutdatedJobs),
)
func initScheduler() {
sch.Default().Logger = log.GetLogger("SCH")
for it := schedules.Iterator(); it.Next(); {
name := it.Key()
callback := it.Value()
cron := app.INI.GetString("task", name)
if cron == "" {
sch.Schedule(name, sch.ZeroTrigger, callback)
} else {
ct := &sch.CronTrigger{}
if err := ct.Parse(cron); err != nil {
log.Fatalf("Invalid task '%s' cron: %v", name, err) //nolint: all
app.Exit(app.ExitErrSCH)
}
log.Infof("Schedule Task %s: %s", name, cron)
sch.Schedule(name, ct, callback)
}
}
}
func reScheduler() {
for _, name := range schedules.Keys() {
cron := app.INI.GetString("task", name)
task, ok := sch.GetTask(name)
if !ok {
log.Errorf("Failed to find task %s", name)
continue
}
if cron == "" {
task.Stop()
} else {
redo := true
if ct, ok := task.Trigger.(*sch.CronTrigger); ok {
redo = (ct.Cron() != cron)
}
if redo {
ct := &sch.CronTrigger{}
if err := ct.Parse(cron); err != nil {
log.Errorf("Invalid task '%s' cron: %v", name, err)
} else {
log.Infof("Reschedule Task %s: %s", name, cron)
task.Trigger = ct
task.Stop()
task.Start()
}
}
}
}
}