forked from play-with-docker/play-with-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
324 lines (291 loc) · 8.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package scheduler
import (
"context"
"fmt"
"log"
"regexp"
"sync"
"time"
"github.com/play-with-docker/play-with-docker/event"
"github.com/play-with-docker/play-with-docker/pwd"
"github.com/play-with-docker/play-with-docker/pwd/types"
"github.com/play-with-docker/play-with-docker/storage"
)
type Task interface {
Name() string
Run(ctx context.Context, instance *types.Instance) error
}
type SchedulerApi interface {
Start() error
Stop()
}
type scheduledSession struct {
session *types.Session
cancel context.CancelFunc
}
type scheduledInstance struct {
instance *types.Instance
playgroundId string
ticker *time.Ticker
cancel context.CancelFunc
fails int
}
type scheduler struct {
scheduledSessions map[string]*scheduledSession
scheduledInstances map[string]*scheduledInstance
tasks map[string]Task
playgrounds map[string]*types.Playground
playgroundTasks map[string][]Task
started bool
ticker *time.Ticker
storage storage.StorageApi
event event.EventApi
pwd pwd.PWDApi
mx sync.Mutex
}
func NewScheduler(tasks []Task, s storage.StorageApi, e event.EventApi, p pwd.PWDApi) (*scheduler, error) {
sch := &scheduler{storage: s, event: e, pwd: p}
sch.tasks = make(map[string]Task)
sch.scheduledSessions = make(map[string]*scheduledSession)
sch.scheduledInstances = make(map[string]*scheduledInstance)
sch.playgrounds = make(map[string]*types.Playground)
sch.playgroundTasks = make(map[string][]Task)
for _, task := range tasks {
if err := sch.addTask(task); err != nil {
return nil, err
}
}
return sch, nil
}
func (s *scheduler) updatePlaygrounds() {
s.mx.Lock()
defer s.mx.Unlock()
log.Printf("Updating playgrounds configuration\n")
for playgroundId, _ := range s.playgrounds {
playground, err := s.storage.PlaygroundGet(playgroundId)
if err != nil {
log.Printf("Could not find playground %s\n", playgroundId)
continue
}
s.playgrounds[playgroundId] = playground
matchedTasks := s.getMatchedTasks(playground)
s.playgroundTasks[playground.Id] = matchedTasks
}
}
func (s *scheduler) schedulePlaygroundsUpdate() {
s.updatePlaygrounds()
s.ticker = time.NewTicker(time.Minute * 5)
go func() {
for range s.ticker.C {
s.updatePlaygrounds()
}
}()
}
func (s *scheduler) getMatchedTasks(playground *types.Playground) []Task {
matchedTasks := []Task{}
for _, expr := range playground.Tasks {
for _, task := range s.tasks {
if expr == task.Name() {
matchedTasks = append(matchedTasks, task)
continue
}
matched, err := regexp.MatchString(expr, task.Name())
if err != nil {
continue
}
if matched {
matchedTasks = append(matchedTasks, task)
continue
}
}
}
return matchedTasks
}
func (s *scheduler) getTasks(playgroundId string) []Task {
s.mx.Lock()
defer s.mx.Unlock()
return s.playgroundTasks[playgroundId]
}
func (s *scheduler) processSession(ctx context.Context, ss *scheduledSession) {
defer s.unscheduleSession(ss.session)
select {
case <-time.After(time.Until(ss.session.ExpiresAt)):
// Session has expired. Need to close the session.
s.pwd.SessionClose(ss.session)
return
case <-ctx.Done():
return
}
}
func (s *scheduler) processInstance(ctx context.Context, si *scheduledInstance) {
defer s.unscheduleInstance(si.instance)
for {
select {
case <-ctx.Done():
log.Printf("Processing tasks for instance %s has been canceled.\n", si.instance.Name)
return
default:
select {
case <-si.ticker.C:
// First check if instance still exists
_, err := s.storage.InstanceGet(si.instance.Name)
if err != nil {
if storage.NotFound(err) {
// Instance doesn't exists anymore. Unschedule.
log.Printf("Instance %s doesn't exists in storage.\n", si.instance.Name)
return
}
log.Printf("Error retrieving instance %s from storage. Got: %v\n", si.instance.Name, err)
continue
}
for _, task := range s.getTasks(si.playgroundId) {
err := task.Run(ctx, si.instance)
if err != nil {
log.Printf("Error running task %s on instance %s. Got: %v\n", task.Name(), si.instance.Name, err)
}
}
}
}
}
}
func (s *scheduler) addTask(task Task) error {
if _, found := s.tasks[task.Name()]; found {
return fmt.Errorf("Task [%s] was already added", task.Name())
}
s.tasks[task.Name()] = task
return nil
}
func (s *scheduler) unscheduleSession(session *types.Session) {
ss, found := s.scheduledSessions[session.Id]
if !found {
return
}
ss.cancel()
delete(s.scheduledSessions, ss.session.Id)
log.Printf("Unscheduled session %s\n", session.Id)
}
func (s *scheduler) scheduleSession(session *types.Session) {
if _, found := s.scheduledSessions[session.Id]; found {
log.Printf("Session %s is already scheduled. Ignoring.\n", session.Id)
return
}
ss := &scheduledSession{session: session}
s.scheduledSessions[session.Id] = ss
ctx, cancel := context.WithCancel(context.Background())
ss.cancel = cancel
go s.processSession(ctx, ss)
log.Printf("Scheduled session %s\n", session.Id)
}
func (s *scheduler) unscheduleInstance(instance *types.Instance) {
si, found := s.scheduledInstances[instance.Name]
if !found {
return
}
si.cancel()
si.ticker.Stop()
delete(s.scheduledInstances, si.instance.Name)
log.Printf("Unscheduled instance %s\n", instance.Name)
}
func (s *scheduler) scheduleInstance(instance *types.Instance, playgroundId string) {
if _, found := s.scheduledInstances[instance.Name]; found {
log.Printf("Instance %s is already scheduled. Ignoring.\n", instance.Name)
return
}
si := &scheduledInstance{instance: instance, playgroundId: playgroundId}
s.scheduledInstances[instance.Name] = si
ctx, cancel := context.WithCancel(context.Background())
si.cancel = cancel
si.ticker = time.NewTicker(time.Second)
go s.processInstance(ctx, si)
log.Printf("Scheduled instance %s\n", instance.Name)
}
func (s *scheduler) Stop() {
s.ticker.Stop()
for _, ss := range s.scheduledSessions {
s.unscheduleSession(ss.session)
}
for _, si := range s.scheduledInstances {
s.unscheduleInstance(si.instance)
}
s.started = false
}
func (s *scheduler) Start() error {
sessions, err := s.storage.SessionGetAll()
if err != nil {
return err
}
for _, session := range sessions {
s.scheduleSession(session)
if _, found := s.playgrounds[session.PlaygroundId]; !found {
playground, err := s.storage.PlaygroundGet(session.PlaygroundId)
if err != nil {
return err
}
s.playgrounds[playground.Id] = playground
}
instances, err := s.storage.InstanceFindBySessionId(session.Id)
if err != nil {
return err
}
for _, instance := range instances {
s.scheduleInstance(instance, session.PlaygroundId)
}
}
// Refresh playground conf every 5 minutes
s.schedulePlaygroundsUpdate()
s.event.On(event.SESSION_NEW, func(sessionId string, args ...interface{}) {
s.mx.Lock()
defer s.mx.Unlock()
log.Printf("EVENT: Session New %s\n", sessionId)
session, err := s.storage.SessionGet(sessionId)
if err != nil {
log.Printf("Session [%s] was not found in storage. Got %s\n", sessionId, err)
return
}
if _, found := s.playgrounds[session.PlaygroundId]; !found {
playground, err := s.storage.PlaygroundGet(session.PlaygroundId)
if err != nil {
log.Printf("Could not find playground %s\n", session.PlaygroundId)
return
}
s.playgrounds[playground.Id] = playground
}
s.scheduleSession(session)
})
s.event.On(event.SESSION_END, func(sessionId string, args ...interface{}) {
log.Printf("EVENT: Session End %s\n", sessionId)
session := &types.Session{Id: sessionId}
s.unscheduleSession(session)
})
s.event.On(event.INSTANCE_NEW, func(sessionId string, args ...interface{}) {
instanceName := args[0].(string)
log.Printf("EVENT: Instance New %s\n", instanceName)
instance, err := s.storage.InstanceGet(instanceName)
if err != nil {
log.Printf("Instance [%s] was not found in storage. Got %s\n", instanceName, err)
return
}
session, err := s.storage.SessionGet(instance.SessionId)
if err != nil {
log.Printf("Session [%s] was not found in storage. Got %s\n", instance.SessionId, err)
return
}
s.scheduleInstance(instance, session.PlaygroundId)
})
s.event.On(event.INSTANCE_DELETE, func(sessionId string, args ...interface{}) {
instanceName := args[0].(string)
log.Printf("EVENT: Instance Delete %s\n", instanceName)
instance := &types.Instance{Name: instanceName}
s.unscheduleInstance(instance)
})
s.event.On(event.PLAYGROUND_NEW, func(playgroundId string, args ...interface{}) {
s.mx.Lock()
log.Printf("EVENT: Playground New %s\n", playgroundId)
// Don't defer lock as updatePlaygrounds will lock again
s.mx.Unlock()
// We just update all playgrounds we manage to be safe. This is pretty fast anyway and this event should be fairly rare
s.updatePlaygrounds()
})
s.started = true
return nil
}