-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcron.go
49 lines (42 loc) · 1003 Bytes
/
cron.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
// Copyright 2021 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package main
import (
"fmt"
"math/rand"
"github.com/kataras/golog"
"github.com/robfig/cron/v3"
)
var (
cronJobs = cron.New() //cron.New(cron.WithSeconds())
)
func NewTimer(timer *Timer, schedule cron.Schedule) {
if timer.Active {
timer.entry = cronJobs.Schedule(schedule, timer)
}
}
func RemoveTimer(timer *Timer) {
if !timer.Active {
return
}
cronJobs.Remove(timer.entry)
}
func RunCron() {
if _, err := cronJobs.AddFunc(fmt.Sprintf(`%d * * * *`, rand.Intn(60)), AutoCheckUpdate); err != nil {
golog.Error(err)
}
for tkey, timer := range storage.Timers {
if !timer.Active {
continue
}
schedule, err := cron.ParseStandard(timer.Cron)
if err != nil {
timer.Active = false
} else {
timer.entry = cronJobs.Schedule(schedule, storage.Timers[tkey])
}
storage.Timers[tkey] = timer
}
cronJobs.Start()
}