forked from admpub/nging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.go
124 lines (107 loc) · 2.37 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Nging is a toolbox for webmasters
Copyright (C) 2018-present Wenhui Shen <swh@admpub.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package cron
import (
"sync"
"github.com/admpub/cron"
"github.com/admpub/log"
)
var (
mainCron *cron.Cron
workPool chan bool
lock sync.Mutex
PoolSize = 50 //连接池容量
)
func Initial(sizes ...int) {
var size int
if len(sizes) > 0 {
size = sizes[0]
}
if size <= 0 {
size = PoolSize
}
Close()
workPool = make(chan bool, size)
mainCron = cron.New(cron.WithSeconds())
}
func Running() bool {
return mainCron != nil && mainCron.Running()
}
func Close() {
if Running() {
mainCron.Stop()
mainCron = nil
close(workPool)
workPool = nil
historyJobsRunning = false
log.Info(`退出任务处理`)
}
}
func MainCron(mustStart bool) *cron.Cron {
if mainCron == nil {
Initial()
}
if mustStart {
if !mainCron.Running() {
mainCron.Start()
}
}
return mainCron
}
func Parse(spec string) error {
_, err := cron.Parse(spec)
return err
}
func AddJob(spec string, job *Job) bool {
lock.Lock()
defer lock.Unlock()
if GetEntryById(job.id) != nil {
return false
}
_, err := MainCron(true).AddJob(spec, job)
if err != nil {
log.Error("AddJob: ", err.Error())
return false
}
return true
}
func RemoveJob(id uint) {
MainCron(false).RemoveJob(func(e *cron.Entry) bool {
if v, ok := e.Job.(*Job); ok {
if v.id == id {
return true
}
}
return false
})
}
func GetEntryById(id uint) *cron.Entry {
entries := MainCron(false).Entries()
for _, e := range entries {
if v, ok := e.Job.(*Job); ok {
if v.id == id {
return &e
}
}
}
return nil
}
func GetEntries(size int) []cron.Entry {
ret := MainCron(false).Entries()
if len(ret) > size {
return ret[:size]
}
return ret
}