-
Notifications
You must be signed in to change notification settings - Fork 1
/
scheduler.go
177 lines (148 loc) · 3.74 KB
/
scheduler.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
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 tasks provides an in-process scheduler for periodic tasks
// that uses the builder pattern for configuration.
// Schedule lets you run Golang functions periodically
// at pre-determined intervals using a simple, human-friendly syntax.
package tasks
import (
"sort"
"sync"
"time"
"github.com/go-phorce/dolly/xlog"
"github.com/juju/errors"
)
var logger = xlog.NewPackageLogger("github.com/go-phorce/dolly", "tasks")
// Time location, default set by the time.Local (*time.Location)
var loc = time.Local
// SetGlobalLocation the time location for the package
func SetGlobalLocation(newLocation *time.Location) {
loc = newLocation
}
// Scheduler defines the scheduler interface
type Scheduler interface {
// Add adds a task to a pool of scheduled tasks
Add(Task) Scheduler
// Clear will delete all scheduled tasks
Clear()
// Count returns the number of registered tasks
Count() int
// IsRunning return the status
IsRunning() bool
// Start all the pending tasks
Start() error
// Stop the scheduler
Stop() error
}
// scheduler provides a task scheduler functionality
type scheduler struct {
tasks []Task
running bool
quit chan bool
lock sync.RWMutex
}
// Scheduler implements the sort.Interface{} for sorting tasks, by the time nextRun
// The Len, Swap, Less are needed for the sort.Interface{}
// Len returns the lengths of tasks array for sorting interface
func (s *scheduler) Len() int {
return len(s.tasks)
}
// Swap provides swap method for sorting interface
func (s *scheduler) Swap(i, j int) {
s.tasks[i], s.tasks[j] = s.tasks[j], s.tasks[i]
}
// Less provides less-comparisson method for sorting interface
func (s *scheduler) Less(i, j int) bool {
return s.tasks[j].NextScheduledTime().After(s.tasks[i].NextScheduledTime())
}
// NewScheduler creates a new scheduler
func NewScheduler() Scheduler {
return &scheduler{
tasks: []Task{},
running: false,
quit: make(chan bool, 1),
}
}
// Count returns the number of registered tasks
func (s *scheduler) Count() int {
return len(s.tasks)
}
// Get the current runnable tasks, which shouldRun is True
func (s *scheduler) getRunnableTasks() []Task {
s.lock.Lock()
defer s.lock.Unlock()
runnable := []Task{}
sort.Sort(s)
for _, j := range s.tasks {
if j.ShouldRun() {
runnable = append(runnable, j)
} else {
break
}
}
return runnable
}
// Get the current runnable tasks, which shouldRun is True
func (s *scheduler) getAllTasks() []Task {
s.lock.Lock()
defer s.lock.Unlock()
return s.tasks[:]
}
// Add adds a task to a pool of scheduled tasks
func (s *scheduler) Add(j Task) Scheduler {
s.lock.Lock()
defer s.lock.Unlock()
s.tasks = append(s.tasks, j)
return s
}
// runPending will run all the tasks that are scheduled to run.
func (s *scheduler) runPending() {
for _, task := range s.getRunnableTasks() {
logger.Tracef("task=%q", task.Name())
go task.Run()
}
}
// Clear will delete all scheduled tasks
func (s *scheduler) Clear() {
s.lock.Lock()
defer s.lock.Unlock()
s.tasks = []Task{}
}
// IsRunning return the status
func (s *scheduler) IsRunning() bool {
s.lock.Lock()
defer s.lock.Unlock()
return s.running
}
// Start all the pending tasks,
// and create a second ticker
func (s *scheduler) Start() error {
logger.Tracef("tasks=%d", s.Count())
s.lock.Lock()
defer s.lock.Unlock()
if s.running {
return errors.Errorf("reasoen=already_running")
}
s.running = true
ticker := time.NewTicker(1 * time.Second)
go func() {
for {
select {
case <-ticker.C:
s.runPending()
case <-s.quit:
ticker.Stop()
return
}
}
}()
return nil
}
// Stop the scheduler
func (s *scheduler) Stop() error {
s.lock.Lock()
defer s.lock.Unlock()
if !s.running {
return errors.Errorf("the scheduler is not running")
}
s.quit <- true
return nil
}