-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduler.go
More file actions
282 lines (236 loc) · 5.25 KB
/
scheduler.go
File metadata and controls
282 lines (236 loc) · 5.25 KB
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
package main
import (
"container/heap"
"context"
"fmt"
"sync"
"time"
)
// JobQueue is a priority queue of jobs
type JobQueue []*Job
func (jq JobQueue) Len() int { return len(jq) }
func (jq JobQueue) Less(i, j int) bool {
// Higher priority comes first
return jq[i].Priority > jq[j].Priority
}
func (jq JobQueue) Swap(i, j int) {
jq[i], jq[j] = jq[j], jq[i]
}
func (jq *JobQueue) Push(x interface{}) {
*jq = append(*jq, x.(*Job))
}
func (jq *JobQueue) Pop() interface{} {
old := *jq
n := len(old)
job := old[n-1]
*jq = old[0 : n-1]
return job
}
// Scheduler manages job execution with resource monitoring
type Scheduler struct {
mu sync.RWMutex
queue *JobQueue
activeJobs map[string]*Job
completedJobs []*Job
maxWorkers int
activeWorkers int
monitor *ResourceMonitor
jobCh chan *Job
stopCh chan struct{}
stopped bool
checkInterval time.Duration
}
// NewScheduler creates a new job scheduler
func NewScheduler(maxWorkers int, monitor *ResourceMonitor) *Scheduler {
jq := &JobQueue{}
heap.Init(jq)
return &Scheduler{
queue: jq,
activeJobs: make(map[string]*Job),
completedJobs: make([]*Job, 0),
maxWorkers: maxWorkers,
monitor: monitor,
jobCh: make(chan *Job, maxWorkers),
stopCh: make(chan struct{}),
checkInterval: 500 * time.Millisecond,
}
}
// Start starts the scheduler
func (s *Scheduler) Start() {
go s.dispatchLoop()
for i := 0; i < s.maxWorkers; i++ {
go s.workerLoop()
}
}
// Stop stops the scheduler
func (s *Scheduler) Stop() {
s.mu.Lock()
if !s.stopped {
close(s.stopCh)
s.stopped = true
}
s.mu.Unlock()
}
// AddJob adds a job to the queue
func (s *Scheduler) AddJob(job *Job) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.stopped {
return fmt.Errorf("scheduler is stopped")
}
heap.Push(s.queue, job)
return nil
}
// CancelJob cancels a job by ID
func (s *Scheduler) CancelJob(jobID string) error {
s.mu.Lock()
defer s.mu.Unlock()
// Check if job is active
if job, exists := s.activeJobs[jobID]; exists {
job.Cancel()
return nil
}
// Check if job is in queue
for i := 0; i < s.queue.Len(); i++ {
if (*s.queue)[i].ID == jobID {
job := (*s.queue)[i]
job.Cancel()
heap.Remove(s.queue, i)
s.completedJobs = append(s.completedJobs, job)
return nil
}
}
return fmt.Errorf("job not found: %s", jobID)
}
// GetJob returns a job by ID
func (s *Scheduler) GetJob(jobID string) (*Job, error) {
s.mu.RLock()
defer s.mu.RUnlock()
// Check active jobs
if job, exists := s.activeJobs[jobID]; exists {
return job, nil
}
// Check queue
for i := 0; i < s.queue.Len(); i++ {
if (*s.queue)[i].ID == jobID {
return (*s.queue)[i], nil
}
}
// Check completed jobs
for _, job := range s.completedJobs {
if job.ID == jobID {
return job, nil
}
}
return nil, fmt.Errorf("job not found: %s", jobID)
}
// ListJobs returns all jobs
func (s *Scheduler) ListJobs() []*Job {
s.mu.RLock()
defer s.mu.RUnlock()
jobs := make([]*Job, 0)
// Add queued jobs
for i := 0; i < s.queue.Len(); i++ {
jobs = append(jobs, (*s.queue)[i])
}
// Add active jobs
for _, job := range s.activeJobs {
jobs = append(jobs, job)
}
// Add completed jobs (last 50)
start := 0
if len(s.completedJobs) > 50 {
start = len(s.completedJobs) - 50
}
jobs = append(jobs, s.completedJobs[start:]...)
return jobs
}
// GetStats returns scheduler statistics
func (s *Scheduler) GetStats() map[string]interface{} {
s.mu.RLock()
defer s.mu.RUnlock()
return map[string]interface{}{
"queued_jobs": s.queue.Len(),
"active_jobs": len(s.activeJobs),
"completed_jobs": len(s.completedJobs),
"max_workers": s.maxWorkers,
"active_workers": s.activeWorkers,
}
}
func (s *Scheduler) dispatchLoop() {
ticker := time.NewTicker(s.checkInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
s.tryDispatchJob()
case <-s.stopCh:
return
}
}
}
func (s *Scheduler) tryDispatchJob() {
s.mu.Lock()
// Check if we have capacity
if s.activeWorkers >= s.maxWorkers || s.queue.Len() == 0 {
s.mu.Unlock()
return
}
// Get the highest priority job
job := heap.Pop(s.queue).(*Job)
// Check if system resources allow running this job
if !s.monitor.CanRunJob(job.Constraints) {
// Put the job back in the queue
heap.Push(s.queue, job)
s.mu.Unlock()
return
}
// Dispatch the job
s.activeJobs[job.ID] = job
s.activeWorkers++
s.mu.Unlock()
// Send job to worker
select {
case s.jobCh <- job:
case <-s.stopCh:
return
}
}
func (s *Scheduler) workerLoop() {
for {
select {
case job := <-s.jobCh:
s.executeJob(job)
case <-s.stopCh:
return
}
}
}
func (s *Scheduler) executeJob(job *Job) {
defer func() {
s.mu.Lock()
delete(s.activeJobs, job.ID)
s.completedJobs = append(s.completedJobs, job)
s.activeWorkers--
s.mu.Unlock()
}()
// Update job status
job.Status = JobStatusRunning
now := time.Now()
job.StartedAt = &now
// Execute the job
err := job.Task(job.ctx)
// Update job status
completedAt := time.Now()
job.CompletedAt = &completedAt
if err != nil {
if job.ctx.Err() == context.Canceled {
job.Status = JobStatusCancelled
} else {
job.Status = JobStatusFailed
job.Error = err
}
} else {
job.Status = JobStatusCompleted
}
}