forked from pardnchiu/go-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
177 lines (150 loc) · 3.21 KB
/
instance.go
File metadata and controls
177 lines (150 loc) · 3.21 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cron
import (
"container/heap"
"context"
"fmt"
"time"
goLogger "github.com/pardnchiu/go-logger"
)
func New(c Config) (*cron, error) {
c.Log = validLoggerConfig(c)
location := time.Local
if c.Location != nil {
location = c.Location
}
logger, err := goLogger.New(c.Log)
if err != nil {
return nil, fmt.Errorf("Failed to initialize `pardnchiu/go-logger`: %w", err)
}
cron := &cron{
logger: logger,
heap: make(taskHeap, 0),
chain: taskChain{},
parser: parser{},
stop: make(chan struct{}),
add: make(chan *task),
remove: make(chan int64),
removeAll: make(chan struct{}),
location: location,
running: false,
}
return cron, nil
}
func (c *cron) Start() {
c.mutex.Lock()
defer c.mutex.Unlock()
if !c.running {
c.running = true
go func() {
now := time.Now().In(c.location)
for _, entry := range c.heap {
entry.Next = entry.Schedule.next(now)
}
heap.Init(&c.heap)
for {
var timer *time.Timer
var timerC <-chan time.Time
if len(c.heap) == 0 || c.heap[0].Next.IsZero() {
timerC = nil
} else {
timer = time.NewTimer(c.heap[0].Next.Sub(now))
timerC = timer.C
}
for {
select {
case now = <-timerC:
now = now.In(c.location)
for len(c.heap) > 0 && (c.heap[0].Next.Before(now) || c.heap[0].Next.Equal(now)) {
e := heap.Pop(&c.heap).(*task)
if !e.Enable {
continue
}
c.wait.Add(1)
go func() {
defer func() {
if r := recover(); r != nil {
c.logger.Info("Recovered from panic [task: %d]", e.ID)
}
}()
defer c.wait.Done()
e.Action()
}()
e.Prev = e.Next
e.Next = e.Schedule.next(now)
if !e.Next.IsZero() {
heap.Push(&c.heap, e)
}
}
case newEntry := <-c.add:
if timer != nil {
timer.Stop()
}
now = time.Now().In(c.location)
newEntry.Next = newEntry.Schedule.next(now)
heap.Push(&c.heap, newEntry)
case id := <-c.remove:
if timer != nil {
timer.Stop()
}
now = time.Now().In(c.location)
for i, entry := range c.heap {
if entry.ID == id {
entry.Enable = false
heap.Remove(&c.heap, i)
break
}
}
case <-c.removeAll:
if timer != nil {
timer.Stop()
}
now = time.Now().In(c.location)
// 完全清空 heap
for len(c.heap) > 0 {
heap.Pop(&c.heap)
}
case <-c.stop:
if timer != nil {
timer.Stop()
}
return
}
break
}
}
}()
}
}
func (c *cron) Stop() context.Context {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.running {
c.stop <- struct{}{}
c.running = false
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
c.wait.Wait()
cancel()
}()
return ctx
}
func validLoggerConfig(c Config) *Log {
if c.Log == nil {
c.Log = &Log{
Path: defaultLogPath,
Stdout: false,
MaxSize: defaultLogMaxSize,
}
}
if c.Log.Path == "" {
c.Log.Path = defaultLogPath
}
if c.Log.MaxSize <= 0 {
c.Log.MaxSize = defaultLogMaxSize
}
if c.Log.MaxBackup <= 0 {
c.Log.MaxBackup = defaultLogMaxBackup
}
return c.Log
}