-
Notifications
You must be signed in to change notification settings - Fork 57
/
cron.go
executable file
·95 lines (73 loc) · 1.58 KB
/
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package cron
import (
"github.com/ch3nnn/webstack-go/internal/repository/mysql/model"
"sync"
"github.com/ch3nnn/webstack-go/internal/pkg/errors"
"github.com/ch3nnn/webstack-go/internal/repository/mysql"
"github.com/ch3nnn/webstack-go/internal/repository/redis"
"github.com/jakecoffman/cron"
"go.uber.org/zap"
)
var _ Server = (*server)(nil)
type taskCount struct {
wg sync.WaitGroup
exit chan struct{}
}
func (tc *taskCount) i() {}
func (tc *taskCount) Add() {
tc.wg.Add(1)
}
func (tc *taskCount) Done() {
tc.wg.Done()
}
func (tc *taskCount) Exit() {
tc.wg.Done()
<-tc.exit
}
func (tc *taskCount) Wait() {
tc.Add()
tc.wg.Wait()
close(tc.exit)
}
type server struct {
logger *zap.Logger
db mysql.Repo
cache redis.Repo
cron *cron.Cron
taskCount *taskCount
}
type Server interface {
i()
// Start 启动 cron 服务
Start()
// Stop 停止 cron 服务
Stop()
// AddTask 增加定时任务
AddTask(task *model.CronTask)
// RemoveTask 删除定时任务
RemoveTask(taskId int64)
// AddJob 增加定时任务执行的工作内容
AddJob(task *model.CronTask) cron.FuncJob
}
func New(logger *zap.Logger, db mysql.Repo, cache redis.Repo) (Server, error) {
if logger == nil {
return nil, errors.New("logger required")
}
if db == nil {
return nil, errors.New("db required")
}
if cache == nil {
return nil, errors.New("cache required")
}
return &server{
logger: logger,
db: db,
cache: cache,
cron: cron.New(),
taskCount: &taskCount{
wg: sync.WaitGroup{},
exit: make(chan struct{}),
},
}, nil
}
func (s *server) i() {}