-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
66 lines (55 loc) · 1.27 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
package scheduler
import (
"context"
)
type DiscoverManager interface {
RecvCh() chan []*WorkerConfig
}
type Scheduler struct {
discoverManager DiscoverManager
dispatcher Dispatcher
storage Storage
cache *Cache
taskCh chan Task
stopCh chan struct{}
}
func (s *Scheduler) Run() {
for {
select {
case workers := <-s.discoverManager.RecvCh():
for _, worker := range workers {
if _, ok := s.cache.nodes[worker.name]; ok {
continue
}
s.cache.AddNode(worker)
}
case task := <-s.taskCh:
for _, n := range s.cache.nodes {
s.cache.AddTask(n.name, task)
ctx := context.TODO()
s.dispatcher.Dispatch(ctx, n.name, task)
break
}
}
}
}
func (s *Scheduler) Stop() {
close(s.stopCh)
close(s.taskCh)
}
func (s *Scheduler) AddTask(task Task) error {
s.taskCh <- task
return nil
}
func (s *Scheduler) Start(ctx context.Context, name string) error {
s.cache.UpdateTaskStatus(name, Running)
return nil
}
func (s *Scheduler) Success(ctx context.Context, name string, result []byte) error {
s.cache.UpdateTaskStatus(name, Completed)
return s.storage.Save(name, result)
}
func (s *Scheduler) Fail(ctx context.Context, name string, reason []byte) error {
s.cache.UpdateTaskStatus(name, Failed)
return s.storage.Save(name, reason)
}