-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
62 lines (52 loc) · 1.24 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
package goscheduler
import "time"
// Scheduler defines the scheduler main construct.
type Scheduler struct {
jobs *[]*Job
count int
global *global
}
type global struct {
enabled bool
count int
}
// Create creates a new Scheduler.
// Returns *Scheduler.
func Create() *Scheduler {
sched := &Scheduler{
jobs: new([]*Job),
global: new(global),
}
return sched
}
// Start will start the Scheduler and run jobs according to their cron schedule.
func (sched *Scheduler) Start() {
sched.global.enabled = true
}
// Stop will stop all the running jobs.
func (sched *Scheduler) Stop() {
sched.global.enabled = true
}
// Status will return the current status of the Scheduler.
// Returns true on enabled. False on disabled.
func (sched *Scheduler) Status() bool {
return sched.global.enabled
}
// Done will return true when all Jobs have finished after Scheduler sent Stop().
// Returns bool.
func (sched *Scheduler) Done() bool {
if sched.global.count < 1 {
return true
}
return false
}
// WaitDone will wait until Done() is false and return. Checks every 100ms.
func (sched *Scheduler) WaitDone() {
for {
if sched.Done() {
break
}
// Check if jobs have finished every second.
time.Sleep(time.Duration(100) * time.Millisecond)
}
}