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