-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscheduler.go
163 lines (137 loc) · 4.18 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
package gds
import (
"context"
"fmt"
"github.com/hashicorp/go-hclog"
"time"
"github.com/hashicorp/go-multierror"
"github.com/integration-system/gds/cluster"
"github.com/integration-system/gds/config"
"github.com/integration-system/gds/jobs"
"github.com/integration-system/gds/provider"
"github.com/integration-system/gds/store"
"github.com/integration-system/gds/utils"
)
type Scheduler interface {
WaitCluster(context.Context) bool
Shutdown(ctx context.Context) error
RegisterExecutor(jobType string, executor JobExecutor, newJobFunc provider.NewJobFunc) Scheduler
UnregisterExecutor(jobType string)
ScheduleJob(job jobs.Job) error
GetJob(key string) (*store.JobInfo, error)
DeleteJob(key string) error
GetAllJobs() []store.JobInfo
GetJobsByType(jobType string) []store.JobInfo
}
type scheduler struct {
store *store.Store
registry executorRegistry
executor executor
typeProvider provider.TypeProvider
raftAdapter *RaftAdapter
}
func (s *scheduler) ScheduleJob(job jobs.Job) error {
var err error
s.store.VisitReadonlyState(func(state store.ReadonlyState) {
_, err = state.GetJob(job.Key())
})
if err == nil {
return store.ErrJobAlreadyExists
}
b, err := job.Marshal()
if err != nil {
return err
}
cmd := cluster.PrepareInsertJobCommand(job.Type(), b)
_, err = s.raftAdapter.ClusterClient.SyncApplyHelper(cmd, "InsertJobCommand")
return err
}
func (s *scheduler) GetJob(key string) (*store.JobInfo, error) {
var (
job *store.JobInfo
err error
)
s.store.VisitReadonlyState(func(state store.ReadonlyState) {
job, err = state.GetJob(key)
})
return job, err
}
func (s *scheduler) DeleteJob(key string) error {
var err error
s.store.VisitReadonlyState(func(state store.ReadonlyState) {
_, err = state.GetJob(key)
})
if err != nil {
return err
}
cmd := cluster.PrepareDeleteJobCommand(key)
_, err = s.raftAdapter.ClusterClient.SyncApplyHelper(cmd, "DeleteJobCommand")
return err
}
func (s *scheduler) GetAllJobs() []store.JobInfo {
var result []store.JobInfo
s.store.VisitReadonlyState(func(state store.ReadonlyState) {
result = state.GetAllJobs()
})
return result
}
func (s *scheduler) GetJobsByType(jobType string) []store.JobInfo {
var result []store.JobInfo
s.store.VisitReadonlyState(func(state store.ReadonlyState) {
result = state.GetJobsByType(jobType)
})
return result
}
func (s *scheduler) RegisterExecutor(jType string, executor JobExecutor, newJobFunc provider.NewJobFunc) Scheduler {
s.registry.Register(jType, executor)
s.typeProvider.RegisterJobProvider(jType, newJobFunc)
return s
}
func (s *scheduler) UnregisterExecutor(jType string) {
s.registry.Unregister(jType)
s.typeProvider.UnregisterJobProvider(jType)
}
func (s *scheduler) WaitCluster(ctx context.Context) bool {
return utils.Wait(ctx, func() bool {
return s.raftAdapter.ClusterClient.Leader() != ""
}, 60*time.Millisecond)
}
func (s *scheduler) Shutdown(ctx context.Context) error {
errs := new(multierror.Error)
if err := s.raftAdapter.Shutdown(ctx); err != nil {
err = fmt.Errorf("shutdown raft: %v", err)
errs = multierror.Append(errs, err)
}
if err := s.executor.Shutdown(ctx); err != nil {
errs = multierror.Append(errs, err)
}
return errs.ErrorOrNil()
}
func NewScheduler(config config.ClusterConfiguration) (Scheduler, error) {
var logger hclog.Logger
if config.Logger != nil {
logger = config.Logger
} else {
logger = hclog.Default().Named("gds")
}
executedJobsCh := make(chan cluster.JobExecuted, 100)
typeProvider := provider.NewTypeProvider()
executorRegistry := newDefaultExecutorRegistry()
executor := newDefaultRuntimeExecutor(executorRegistry, executedJobsCh, config.JobExecutionTimeout, logger)
clusterHandler := NewClusterHandler(typeProvider, executor, logger)
raftAdapter, err := NewRaftAdapter(config, clusterHandler, typeProvider, logger)
if err != nil {
return nil, err
}
s := &scheduler{
registry: executorRegistry,
executor: executor,
typeProvider: typeProvider,
raftAdapter: raftAdapter,
store: raftAdapter.RaftStore,
}
clusterHandler.cluster = raftAdapter.ClusterClient
go clusterHandler.listenLeaderCh(raftAdapter.RaftStore)
go clusterHandler.handleExecutedJobs(executedJobsCh)
return s, nil
}