2014-02-07 10:10:01 +00:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
2014-06-12 23:41:04 +00:00
|
|
|
"github.com/drone/drone/server/database"
|
|
|
|
"github.com/drone/drone/shared/model"
|
2014-06-04 21:25:38 +00:00
|
|
|
|
2014-06-04 21:44:05 +00:00
|
|
|
"github.com/drone/drone/shared/build/script"
|
2014-02-07 10:10:01 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2014-06-12 23:41:04 +00:00
|
|
|
User *model.User
|
|
|
|
Repo *model.Repo
|
|
|
|
Commit *model.Commit
|
2014-06-04 21:25:38 +00:00
|
|
|
|
|
|
|
// Build instructions from the .drone.yml
|
|
|
|
// file, unmarshalled.
|
|
|
|
Script *script.Build
|
2014-02-07 10:10:01 +00:00
|
|
|
}
|
|
|
|
|
2014-02-26 01:14:14 +00:00
|
|
|
// Start N workers with the given build runner.
|
2014-06-12 23:41:04 +00:00
|
|
|
func Start(workers int, commits database.CommitManager, runner BuildRunner) *Queue {
|
2014-02-25 00:53:28 +00:00
|
|
|
tasks := make(chan *BuildTask)
|
|
|
|
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{
|
2014-06-12 19:44:19 +00:00
|
|
|
runner: runner,
|
|
|
|
commits: commits,
|
2014-02-25 01:51:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|