This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
173 lines (142 loc) · 3.54 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
package scheduler
import (
"context"
"github.com/d1slike/go-sched/internal"
"github.com/d1slike/go-sched/jobs"
"github.com/d1slike/go-sched/stores"
"github.com/d1slike/go-sched/triggers"
)
type Option func(s *scheduler)
type Scheduler interface {
Start()
Shutdown(ctx context.Context) error
RegisterExecutor(jType string, executor JobExecutor) Scheduler
UnregisterExecutor(jType string)
ScheduleJob(job jobs.MutableJob, trigger triggers.MutableTrigger) error
GetJob(jKey string) (jobs.ImmutableJob, error)
GetTrigger(tKey string) (triggers.ImmutableTrigger, error)
DeleteJob(jKey string) (bool, error)
DeleteTrigger(tKey string) (bool, error)
GetJobs() ([]jobs.ImmutableJob, error)
GetTriggers() ([]triggers.ImmutableTrigger, error)
UpdateJob(job jobs.MutableJob) error
}
type scheduler struct {
name string
store stores.Store
registry executorRegistry
executor executor
timers Timers
}
func (s *scheduler) GetJob(jKey string) (jobs.ImmutableJob, error) {
return s.store.GetJob(s.name, jKey)
}
func (s *scheduler) GetTrigger(tKey string) (triggers.ImmutableTrigger, error) {
return s.store.GetTrigger(s.name, tKey)
}
func (s *scheduler) DeleteJob(jKey string) (bool, error) {
ok, err := s.store.DeleteJob(s.name, jKey)
if err != nil {
return false, err
}
if ok {
keys, err := s.store.DeleteTriggersByJobKey(s.name, jKey)
if err != nil {
return false, err
}
s.executor.CancelTriggers(keys...)
}
return ok, nil
}
func (s *scheduler) DeleteTrigger(tKey string) (bool, error) {
ok, err := s.store.DeleteTrigger(s.name, tKey)
if err != nil {
return false, err
}
if ok {
s.executor.CancelTriggers(tKey)
}
return ok, nil
}
func (s *scheduler) GetJobs() ([]jobs.ImmutableJob, error) {
return s.store.GetJobs(s.name)
}
func (s *scheduler) GetTriggers() ([]triggers.ImmutableTrigger, error) {
return s.store.GetTriggers(s.name)
}
func (s *scheduler) UnregisterExecutor(jType string) {
s.registry.Unregister(jType)
}
func (s *scheduler) Start() {
s.executor.Start()
}
func (s *scheduler) Shutdown(ctx context.Context) error {
return s.executor.Shutdown(ctx)
}
func (s *scheduler) RegisterExecutor(jType string, executor JobExecutor) Scheduler {
s.registry.Register(jType, executor)
return s
}
func (s *scheduler) ScheduleJob(job jobs.MutableJob, tri triggers.MutableTrigger) error {
j, err := job.ToImmutable()
if err != nil {
return err
}
t, err := tri.ToImmutable()
if err != nil {
return err
}
t = internal.ModifyTrigger(t, func(tr *internal.Trigger) {
tr.TjobKey = j.Key()
tr.Tstate = triggers.StateScheduled
})
if err := s.store.InsertJob(s.name, j); err != nil {
return err
}
if err := s.store.InsertTrigger(s.name, t); err != nil {
return err
}
return nil
}
func (s *scheduler) UpdateJob(job jobs.MutableJob) error {
j, err := job.ToImmutable()
if err != nil {
return err
}
return s.store.UpdateJob(s.name, j)
}
func NewScheduler(name string, opts ...Option) Scheduler {
s := &scheduler{
name: name,
registry: newDefaultExecutorRegistry(),
timers: NewDefaultTimers(),
}
for _, o := range opts {
o(s)
}
if s.store == nil {
s.store = stores.NewInMemoryStore()
}
s.executor = newDefaultRuntimeExecutor(
s.name,
s.store,
s.registry,
s.timers,
)
return s
}
func WithStore(store stores.Store) Option {
return func(s *scheduler) {
s.store = store
}
}
func WithExecutors(m map[string]JobExecutor) Option {
return func(s *scheduler) {
s.registry.RegisterAll(m)
}
}
func WithTimers(timers Timers) Option {
return func(s *scheduler) {
s.timers = SetDefault(timers)
}
}