remove docker/queue singletons; inject in main

This commit is contained in:
Alex Suraci 2014-02-25 17:10:04 -08:00
parent c0adf459f9
commit 9e1c4e60e5
6 changed files with 29 additions and 19 deletions

View file

@ -123,6 +123,8 @@ func vet(path string) {
}
func run(path string) {
dockerClient := docker.New()
// parse the Drone yml file
s, err := script.ParseBuildFile(path)
if err != nil {
@ -175,7 +177,7 @@ func run(path string) {
// loop through and create builders
for _, b := range builds { //script.Builds {
builder := build.New(docker.DefaultClient)
builder := build.New(dockerClient)
builder.Build = b
builder.Repo = &code
builder.Key = key

View file

@ -5,7 +5,9 @@ import (
"flag"
"log"
"net/http"
"runtime"
"strings"
"time"
"code.google.com/p/go.net/websocket"
"github.com/GeertJohan/go.rice"
@ -13,10 +15,12 @@ import (
_ "github.com/mattn/go-sqlite3"
"github.com/russross/meddler"
"github.com/drone/drone/pkg/build/docker"
"github.com/drone/drone/pkg/channel"
"github.com/drone/drone/pkg/database"
"github.com/drone/drone/pkg/database/migrate"
"github.com/drone/drone/pkg/handler"
"github.com/drone/drone/pkg/queue"
)
var (
@ -118,6 +122,11 @@ func setupStatic() {
// setup routes for serving dynamic content.
func setupHandlers() {
queueRunner := queue.NewRunner(docker.New(), 300*time.Second)
queue := queue.Start(runtime.NumCPU(), queueRunner)
hookHandler := handler.NewHookHandler(queue)
m := pat.New()
m.Get("/login", handler.ErrorHandler(handler.Login))
m.Post("/login", handler.ErrorHandler(handler.Authorize))
@ -177,7 +186,7 @@ func setupHandlers() {
m.Get("/account/admin/users", handler.AdminHandler(handler.AdminUserList))
// handlers for GitHub post-commit hooks
m.Post("/hook/github.com", handler.ErrorHandler(handler.Hook))
m.Post("/hook/github.com", handler.ErrorHandler(hookHandler.Hook))
// handlers for first-time installation
m.Get("/install", handler.ErrorHandler(handler.Install))

View file

@ -29,8 +29,6 @@ const (
// Enables verbose logging to the Terminal window
var Logging = true
var DefaultClient = New() // TEMPORARY; PLEASE CONSTRUCT/INJECT YOURSELF
// New creates an instance of the Docker Client
func New() *Client {
c := &Client{}

View file

@ -13,10 +13,19 @@ import (
"github.com/drone/go-github/github"
)
type HookHandler struct {
queue *queue.Queue
}
func NewHookHandler(queue *queue.Queue) *HookHandler {
return &HookHandler{
queue: queue,
}
}
// Processes a generic POST-RECEIVE hook and
// attempts to trigger a build.
func Hook(w http.ResponseWriter, r *http.Request) error {
func (h *HookHandler) Hook(w http.ResponseWriter, r *http.Request) error {
// handle github ping
if r.Header.Get("X-Github-Event") == "ping" {
return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK)
@ -25,7 +34,7 @@ func Hook(w http.ResponseWriter, r *http.Request) error {
// if this is a pull request route
// to a different handler
if r.Header.Get("X-Github-Event") == "pull_request" {
PullRequestHook(w, r)
h.PullRequestHook(w, r)
return nil
}
@ -160,14 +169,13 @@ func Hook(w http.ResponseWriter, r *http.Request) error {
//realtime.CommitPending(repo.UserID, repo.TeamID, repo.ID, commit.ID, repo.Private)
//realtime.BuildPending(repo.UserID, repo.TeamID, repo.ID, commit.ID, build.ID, repo.Private)
queue.Add(&queue.BuildTask{Repo: repo, Commit: commit, Build: build, Script: buildscript}) //Push(repo, commit, build, buildscript)
h.queue.Add(&queue.BuildTask{Repo: repo, Commit: commit, Build: build, Script: buildscript}) //Push(repo, commit, build, buildscript)
// OK!
return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK)
}
func PullRequestHook(w http.ResponseWriter, r *http.Request) {
func (h *HookHandler) PullRequestHook(w http.ResponseWriter, r *http.Request) {
// get the payload of the message
// this should contain a json representation of the
// repository and commit details
@ -276,7 +284,7 @@ func PullRequestHook(w http.ResponseWriter, r *http.Request) {
// notify websocket that a new build is pending
// TODO we should, for consistency, just put this inside Queue.Add()
queue.Add(&queue.BuildTask{Repo: repo, Commit: commit, Build: build, Script: buildscript})
h.queue.Add(&queue.BuildTask{Repo: repo, Commit: commit, Build: build, Script: buildscript})
// OK!
RenderText(w, http.StatusText(http.StatusOK), http.StatusOK)

View file

@ -1,11 +1,8 @@
package queue
import (
"github.com/drone/drone/pkg/build/docker"
"github.com/drone/drone/pkg/build/script"
. "github.com/drone/drone/pkg/model"
"runtime"
"time"
)
// A Queue dispatches tasks to workers.
@ -25,10 +22,6 @@ type BuildTask struct {
Script *script.Build
}
var defaultQueue = Start(runtime.NumCPU(), newRunner(docker.DefaultClient, 300*time.Second)) // TEMPORARY; INJECT PLEASE
var Add = defaultQueue.Add // TEMPORARY; INJECT PLEASE
func Start(workers int, runner Runner) *Queue {
// get the number of CPUs. Since builds
// tend to be CPU-intensive we should only

View file

@ -19,7 +19,7 @@ type runner struct {
timeout time.Duration
}
func newRunner(dockerClient *docker.Client, timeout time.Duration) *runner {
func NewRunner(dockerClient *docker.Client, timeout time.Duration) Runner {
return &runner{
dockerClient: dockerClient,
timeout: timeout,