2014-02-07 10:10:01 +00:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
. "github.com/drone/drone/pkg/model"
|
|
|
|
)
|
|
|
|
|
2014-02-25 00:53:28 +00:00
|
|
|
// A Queue dispatches tasks to workers.
|
|
|
|
type Queue struct {
|
|
|
|
tasks chan<- *BuildTask
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// BuildTasks represents a build that is pending
|
|
|
|
// execution.
|
|
|
|
type BuildTask struct {
|
|
|
|
Repo *Repo
|
|
|
|
Commit *Commit
|
|
|
|
Build *Build
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:14:14 +00:00
|
|
|
// Start N workers with the given build runner.
|
|
|
|
func Start(workers int, runner BuildRunner) *Queue {
|
2014-02-25 00:53:28 +00:00
|
|
|
tasks := make(chan *BuildTask)
|
2014-02-07 10:10:01 +00:00
|
|
|
|
2014-02-25 00:53:28 +00:00
|
|
|
queue := &Queue{tasks: tasks}
|
2014-02-07 10:10:01 +00:00
|
|
|
|
2014-02-25 00:53:28 +00:00
|
|
|
for i := 0; i < workers; i++ {
|
2014-02-25 01:51:25 +00:00
|
|
|
worker := worker{
|
|
|
|
runner: runner,
|
|
|
|
}
|
|
|
|
|
2014-02-25 00:53:28 +00:00
|
|
|
go worker.work(tasks)
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 00:53:28 +00:00
|
|
|
return queue
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 00:53:28 +00:00
|
|
|
// Add adds the task to the build queue.
|
|
|
|
func (q *Queue) Add(task *BuildTask) {
|
|
|
|
q.tasks <- task
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|