shad-go/distbuild/pkg/dist/schedule.go

69 lines
1.2 KiB
Go
Raw Normal View History

2020-03-14 10:24:44 +00:00
package dist
import (
"sync"
"gitlab.com/slon/shad-go/distbuild/pkg/build"
"gitlab.com/slon/shad-go/distbuild/pkg/proto"
)
type scheduledJob struct {
job *build.Job
2020-03-23 20:58:08 +00:00
finished *proto.JobResult
2020-03-14 10:24:44 +00:00
mu sync.Mutex
done chan struct{}
}
func newScheduledJob(job *build.Job) *scheduledJob {
return &scheduledJob{
job: job,
done: make(chan struct{}),
}
}
2020-03-23 20:58:08 +00:00
func (s *scheduledJob) finish(f *proto.JobResult) {
2020-03-14 10:24:44 +00:00
s.mu.Lock()
defer s.mu.Unlock()
if s.finished == nil {
s.finished = f
close(s.done)
}
}
func (c *Coordinator) scheduleJob(job *build.Job) *scheduledJob {
c.mu.Lock()
defer c.mu.Unlock()
if scheduled, ok := c.scheduledJobs[job.ID]; ok {
return scheduled
} else {
scheduled = newScheduledJob(job)
c.scheduledJobs[job.ID] = scheduled
c.queue = append(c.queue, scheduled)
return scheduled
}
}
func (c *Coordinator) pickJob() (*build.Job, bool) {
c.mu.Lock()
defer c.mu.Unlock()
if len(c.queue) == 0 {
return nil, false
}
job := c.queue[0].job
c.queue = c.queue[1:]
return job, true
}
func (c *Coordinator) lookupJob(id build.ID) (*scheduledJob, bool) {
c.mu.Lock()
defer c.mu.Unlock()
scheduled, ok := c.scheduledJobs[id]
return scheduled, ok
}