From da350989d3fe153adca132d4ef8e400f397ad509 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Fri, 24 Apr 2015 14:25:03 -0700 Subject: [PATCH] fixed minor queue glitches --- datastore/bolt/build.go | 2 +- drone.go | 15 +++++++++++++++ queue/queue.go | 27 --------------------------- server/builds.go | 23 ++++++++++++++++++++++- server/hooks.go | 30 ++++++++++++++++++++---------- server/queue.go | 12 +++++++++++- 6 files changed, 69 insertions(+), 40 deletions(-) diff --git a/datastore/bolt/build.go b/datastore/bolt/build.go index 3d20a1be..1a466eb8 100644 --- a/datastore/bolt/build.go +++ b/datastore/bolt/build.go @@ -204,7 +204,7 @@ func (db *DB) SetBuildTask(repo string, build int, task *common.Task) error { return err } build_.Updated = time.Now().UTC().Unix() - build_.Tasks[task.Number] = task // TODO check index to prevent nil pointer / panic + build_.Tasks[task.Number-1] = task // TODO check index to prevent nil pointer / panic return update(t, bucketBuild, key, build_) }) } diff --git a/drone.go b/drone.go index e8b331a9..782adb6c 100644 --- a/drone.go +++ b/drone.go @@ -6,10 +6,13 @@ import ( "github.com/gin-gonic/gin" "github.com/drone/drone/datastore/bolt" + "github.com/drone/drone/eventbus" "github.com/drone/drone/remote/github" "github.com/drone/drone/server" "github.com/drone/drone/server/session" "github.com/drone/drone/settings" + + queue "github.com/drone/drone/queue/builtin" ) var path = flag.String("config", "drone.toml", "") @@ -31,8 +34,10 @@ func main() { api := r.Group("/api") api.Use(server.SetHeaders()) + api.Use(server.SetBus(eventbus.New())) api.Use(server.SetDatastore(ds)) api.Use(server.SetRemote(remote)) + api.Use(server.SetQueue(queue.New())) api.Use(server.SetSettings(settings)) api.Use(server.SetUser(session)) @@ -99,6 +104,16 @@ func main() { hooks.POST("", server.PostHook) } + queue := api.Group("/queue") + { + queue.Use(server.SetRepo()) + queue.GET("", server.GetQueue) + queue.POST("/pull", server.PollBuild) + queue.POST("/push/:owner/:name", server.PushBuild) + queue.POST("/push/:owner/:name/:build", server.PushTask) + queue.POST("/push/:owner/:name/:build/:task/logs", server.PushLogs) + } + auth := r.Group("/authorize") { auth.Use(server.SetHeaders()) diff --git a/queue/queue.go b/queue/queue.go index e0832d60..73df7a70 100644 --- a/queue/queue.go +++ b/queue/queue.go @@ -26,30 +26,3 @@ type Queue interface { // queue, in proper sequence. Items() []*Work } - -// type Manager interface { -// // Register registers a worker that has signed -// // up to accept work. -// Register(*Worker) - -// // Unregister unregisters a worker that should no -// // longer be accepting work. -// Unregister(*Worker) - -// // Assign assigns work to a worker. -// Assign(*Work, *Worker) - -// // Unassign unassigns work from a worker. -// Unassign(*Work, *Worker) - -// // Work returns a list of all work that is -// // currently in progress. -// Work() []*Work - -// // Worker retrieves a worker by name. -// Worker(string) *Worker - -// // Workers returns a slice containing all workers -// // registered with the manager. -// Workers() []*Worker -// } diff --git a/server/builds.go b/server/builds.go index 8535737a..ac84bdb8 100644 --- a/server/builds.go +++ b/server/builds.go @@ -5,6 +5,7 @@ import ( "strconv" "github.com/drone/drone/common" + "github.com/drone/drone/queue" "github.com/gin-gonic/gin" "github.com/gin-gonic/gin/binding" ) @@ -102,6 +103,7 @@ func PostBuildStatus(c *gin.Context) { // func RunBuild(c *gin.Context) { store := ToDatastore(c) + queue_ := ToQueue(c) repo := ToRepo(c) num, err := strconv.Atoi(c.Params.ByName("number")) if err != nil { @@ -114,6 +116,18 @@ func RunBuild(c *gin.Context) { return } + keys, err := store.RepoKeypair(repo.FullName) + if err != nil { + c.Fail(404, err) + return + } + + user, err := store.User(repo.User.Login) + if err != nil { + c.Fail(404, err) + return + } + // must not restart a running build if build.State == common.StatePending || build.State == common.StateRunning { c.Fail(409, err) @@ -143,7 +157,14 @@ func RunBuild(c *gin.Context) { // raw = []byte(inject.InjectSafe(string(raw), params)) // } - // TODO push build to queue + queue_.Publish(&queue.Work{ + User: user, + Repo: repo, + Build: build, + Keys: keys, + Netrc: &common.Netrc{}, //TODO create netrc + Yaml: nil, // TODO fetch yaml + }) c.JSON(202, build) } diff --git a/server/hooks.go b/server/hooks.go index df7a9ae2..250ab093 100644 --- a/server/hooks.go +++ b/server/hooks.go @@ -7,7 +7,7 @@ import ( "github.com/drone/drone/common" "github.com/drone/drone/parser/inject" "github.com/drone/drone/parser/matrix" - // "github.com/bradrydzewski/drone/worker" + "github.com/drone/drone/queue" "github.com/gin-gonic/gin" ) @@ -19,6 +19,7 @@ import ( func PostHook(c *gin.Context) { remote := ToRemote(c) store := ToDatastore(c) + queue_ := ToQueue(c) hook, err := remote.Hook(c.Request) if err != nil { @@ -107,19 +108,13 @@ func PostHook(c *gin.Context) { Environment: axis, }) } - - err = store.SetBuild(repo.FullName, build) + keys, err := store.RepoKeypair(repo.FullName) if err != nil { - c.Fail(500, err) + log.Errorf("failure to fetch keypair for %s. %s", repo.FullName, err) + c.Fail(404, err) return } - // w := worker.Work{ - // User: user, - // Repo: repo, - // Build: build, - // } - // verify the branches can be built vs skipped // s, _ := script.ParseBuild(string(yml)) // if len(hook.PullRequest) == 0 && !s.MatchBranch(hook.Branch) { @@ -127,5 +122,20 @@ func PostHook(c *gin.Context) { // return // } + err = store.SetBuild(repo.FullName, build) + if err != nil { + c.Fail(500, err) + return + } + + queue_.Publish(&queue.Work{ + User: user, + Repo: repo, + Build: build, + Keys: keys, + Netrc: &common.Netrc{}, // TODO + Yaml: raw, + }) + c.JSON(200, build) } diff --git a/server/queue.go b/server/queue.go index b87c0641..c8315e39 100644 --- a/server/queue.go +++ b/server/queue.go @@ -19,7 +19,7 @@ import ( // GET /queue/pull func PollBuild(c *gin.Context) { queue := ToQueue(c) - work := queue.PullAck() + work := queue.Pull() c.JSON(200, work) } @@ -37,6 +37,10 @@ func PushBuild(c *gin.Context) { c.Fail(404, err) return } + build.Duration = in.Duration + build.Started = in.Started + build.Finished = in.Finished + build.State = in.State err = store.SetBuildState(repo.FullName, build) if err != nil { c.Fail(500, err) @@ -106,3 +110,9 @@ func PushLogs(c *gin.Context) { } c.Writer.WriteHeader(200) } + +func GetQueue(c *gin.Context) { + queue := ToQueue(c) + items := queue.Items() + c.JSON(200, items) +}