2014-02-07 13:10:01 +03:00
|
|
|
package queue
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/drone/drone/pkg/build/script"
|
|
|
|
. "github.com/drone/drone/pkg/model"
|
2014-02-25 03:53:28 +03:00
|
|
|
"runtime"
|
2014-02-07 13:10:01 +03:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// Build instructions from the .drone.yml
|
|
|
|
// file, unmarshalled.
|
|
|
|
Script *script.Build
|
|
|
|
}
|
|
|
|
|
2014-02-25 03:53:28 +03:00
|
|
|
var defaultQueue = Start(runtime.NumCPU()) // TEMPORARY; INJECT PLEASE
|
2014-02-07 13:10:01 +03:00
|
|
|
|
2014-02-25 03:53:28 +03:00
|
|
|
var Add = defaultQueue.Add // TEMPORARY; INJECT PLEASE
|
2014-02-07 13:10:01 +03:00
|
|
|
|
2014-02-25 03:53:28 +03:00
|
|
|
func Start(workers int) *Queue {
|
|
|
|
// get the number of CPUs. Since builds
|
|
|
|
// tend to be CPU-intensive we should only
|
|
|
|
// execute 1 build per CPU.
|
|
|
|
// must be at least 1
|
|
|
|
// if ncpu < 1 {
|
|
|
|
// ncpu = 1
|
|
|
|
// }
|
2014-02-07 13:10:01 +03:00
|
|
|
|
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
|
|
|
// spawn a worker for each CPU
|
|
|
|
for i := 0; i < workers; i++ {
|
|
|
|
worker := worker{}
|
|
|
|
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
|
|
|
}
|