gtq/scheduler.go

40 lines
865 B
Go

package gtq
import (
"sort"
)
// Scheduler is an internal goroutine which examines the Queues and
// choses which tasks to run next. Returns true if tasks were scheduled,
// or false if there's nothing left to schedule.
type Scheduler func(tq *TaskQueue) bool
// SimpleScheduler is the simplest possible implementation, which just takes
// tasks off the highest priority queue.
func SimpleScheduler(tq *TaskQueue) bool {
pc := tq.PriorityCounts()
// sort priorities
prios := make([]uint, 0, len(pc))
for k := range pc {
prios = append(prios, k)
}
sort.Sort(UIntSlice(prios))
var queued uint
for _, prio := range prios {
q, ok := tq.queues.Load(prio)
if !ok {
continue
}
for q.(*Queue).Length() > 0 {
task := q.(*Queue).Remove()
tq.nextTask <- task
queued++
}
}
if queued >= (tq.numJobs * 2) {
return true
}
return false
}