-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
50 lines (42 loc) · 888 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
50
package gofks
import (
"bytes"
"github.com/bhmy-shm/gofks/core/cache/buffer"
"github.com/bhmy-shm/gofks/core/logx"
"github.com/robfig/cron/v3"
"log"
"sync"
)
type (
CronInter interface {
Get() *cron.Cron
Close()
}
cronTask struct {
taskCron *cron.Cron //定时任务
onceCron sync.Once
}
)
func NewCronTask() CronInter {
return &cronTask{
taskCron: nil,
}
}
func (ct *cronTask) Get() *cron.Cron {
ct.onceCron.Do(func() {
ct.taskCron = cron.New(cron.WithSeconds(), cron.WithLogger(bufferLog()))
})
return ct.taskCron
}
func (ct *cronTask) Close() {
ct.taskCron.Stop()
}
func bufferLog() cron.Logger {
sw := &buffer.SyncWriter{}
return cron.PrintfLogger(log.New(sw, "gofks-cron:", log.LstdFlags))
}
func myWriteLog() cron.Logger {
var buf bytes.Buffer
sw := logx.NewWriter(&buf)
return cron.PrintfLogger(log.New(sw, "gofks-cron:", log.LstdFlags))
}