Skip to content

Commit

Permalink
queue integrated with server, but not agent
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Sep 26, 2016
1 parent 524fba6 commit f2c1d46
Show file tree
Hide file tree
Showing 12 changed files with 648 additions and 513 deletions.
37 changes: 18 additions & 19 deletions bus/bus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package bus

//go:generate mockery -name Bus -output mock -case=underscore

import "golang.org/x/net/context"

// Bus represents an event bus implementation that
// allows a publisher to broadcast Event notifications
// to a list of subscribers.
Expand All @@ -21,20 +19,21 @@ type Bus interface {
Unsubscribe(chan *Event)
}

// Publish broadcasts an event to all subscribers.
func Publish(c context.Context, event *Event) {
FromContext(c).Publish(event)
}

// Subscribe adds the channel to the list of
// subscribers. Each subscriber in the list will
// receive broadcast events.
func Subscribe(c context.Context, eventc chan *Event) {
FromContext(c).Subscribe(eventc)
}

// Unsubscribe removes the channel from the
// list of subscribers.
func Unsubscribe(c context.Context, eventc chan *Event) {
FromContext(c).Unsubscribe(eventc)
}
//
// // Publish broadcasts an event to all subscribers.
// func Publish(c context.Context, event *Event) {
// FromContext(c).Publish(event)
// }
//
// // Subscribe adds the channel to the list of
// // subscribers. Each subscriber in the list will
// // receive broadcast events.
// func Subscribe(c context.Context, eventc chan *Event) {
// FromContext(c).Subscribe(eventc)
// }
//
// // Unsubscribe removes the channel from the
// // list of subscribers.
// func Unsubscribe(c context.Context, eventc chan *Event) {
// FromContext(c).Unsubscribe(eventc)
// }
1 change: 1 addition & 0 deletions drone/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func server(c *cli.Context) error {
middleware.Store(c),
middleware.Remote(c),
middleware.Agents(c),
middleware.Broker(c),
)

// start the server with tls enabled
Expand Down
183 changes: 92 additions & 91 deletions queue/queue_impl_test.go
Original file line number Diff line number Diff line change
@@ -1,93 +1,94 @@
package queue

import (
"sync"
"testing"

. "github.com/franela/goblin"
"github.com/gin-gonic/gin"
)

func TestBuild(t *testing.T) {
g := Goblin(t)
g.Describe("Queue", func() {

g.It("Should publish item", func() {
c := new(gin.Context)
q := newQueue()
ToContext(c, q)

w1 := &Work{}
w2 := &Work{}
Publish(c, w1)
Publish(c, w2)
g.Assert(len(q.items)).Equal(2)
g.Assert(len(q.itemc)).Equal(2)
})

g.It("Should remove item", func() {
c := new(gin.Context)
q := newQueue()
ToContext(c, q)

w1 := &Work{}
w2 := &Work{}
w3 := &Work{}
Publish(c, w1)
Publish(c, w2)
Publish(c, w3)
Remove(c, w2)
g.Assert(len(q.items)).Equal(2)
g.Assert(len(q.itemc)).Equal(2)

g.Assert(Pull(c)).Equal(w1)
g.Assert(Pull(c)).Equal(w3)
g.Assert(Remove(c, w2)).Equal(ErrNotFound)
})

g.It("Should pull item", func() {
c := new(gin.Context)
q := New()
ToContext(c, q)

cn := new(closeNotifier)
cn.closec = make(chan bool, 1)
w1 := &Work{}
w2 := &Work{}

Publish(c, w1)
g.Assert(Pull(c)).Equal(w1)

Publish(c, w2)
g.Assert(PullClose(c, cn)).Equal(w2)
})

g.It("Should cancel pulling item", func() {
c := new(gin.Context)
q := New()
ToContext(c, q)

cn := new(closeNotifier)
cn.closec = make(chan bool, 1)
var wg sync.WaitGroup
go func() {
wg.Add(1)
g.Assert(PullClose(c, cn) == nil).IsTrue()
wg.Done()
}()
go func() {
cn.closec <- true
}()
wg.Wait()

})
})
}

type closeNotifier struct {
closec chan bool
}

func (c *closeNotifier) CloseNotify() <-chan bool {
return c.closec
}
//
// import (
// "sync"
// "testing"
//
// . "github.com/franela/goblin"
// "github.com/gin-gonic/gin"
// )
//
// func TestBuild(t *testing.T) {
// g := Goblin(t)
// g.Describe("Queue", func() {
//
// g.It("Should publish item", func() {
// c := new(gin.Context)
// q := newQueue()
// ToContext(c, q)
//
// w1 := &Work{}
// w2 := &Work{}
// Publish(c, w1)
// Publish(c, w2)
// g.Assert(len(q.items)).Equal(2)
// g.Assert(len(q.itemc)).Equal(2)
// })
//
// g.It("Should remove item", func() {
// c := new(gin.Context)
// q := newQueue()
// ToContext(c, q)
//
// w1 := &Work{}
// w2 := &Work{}
// w3 := &Work{}
// Publish(c, w1)
// Publish(c, w2)
// Publish(c, w3)
// Remove(c, w2)
// g.Assert(len(q.items)).Equal(2)
// g.Assert(len(q.itemc)).Equal(2)
//
// g.Assert(Pull(c)).Equal(w1)
// g.Assert(Pull(c)).Equal(w3)
// g.Assert(Remove(c, w2)).Equal(ErrNotFound)
// })
//
// g.It("Should pull item", func() {
// c := new(gin.Context)
// q := New()
// ToContext(c, q)
//
// cn := new(closeNotifier)
// cn.closec = make(chan bool, 1)
// w1 := &Work{}
// w2 := &Work{}
//
// Publish(c, w1)
// g.Assert(Pull(c)).Equal(w1)
//
// Publish(c, w2)
// g.Assert(PullClose(c, cn)).Equal(w2)
// })
//
// g.It("Should cancel pulling item", func() {
// c := new(gin.Context)
// q := New()
// ToContext(c, q)
//
// cn := new(closeNotifier)
// cn.closec = make(chan bool, 1)
// var wg sync.WaitGroup
// go func() {
// wg.Add(1)
// g.Assert(PullClose(c, cn) == nil).IsTrue()
// wg.Done()
// }()
// go func() {
// cn.closec <- true
// }()
// wg.Wait()
//
// })
// })
// }
//
// type closeNotifier struct {
// closec chan bool
// }
//
// func (c *closeNotifier) CloseNotify() <-chan bool {
// return c.closec
// }
52 changes: 52 additions & 0 deletions router/middleware/broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package middleware

import (
"sync"

handlers "github.com/drone/drone/server"

"github.com/codegangsta/cli"
"github.com/drone/mq/server"
"github.com/drone/mq/stomp"

"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
)

const (
serverKey = "broker"
clientKey = "stomp.client" // mirrored from stomp/context
)

// Broker is a middleware function that initializes the broker
// and adds the broker client to the request context.
func Broker(cli *cli.Context) gin.HandlerFunc {
secret := cli.String("agent-secret")
if secret == "" {
logrus.Fatalf("failed to generate token from DRONE_SECRET")
}

broker := server.NewServer(
server.WithCredentials("x-token", secret),
)
client := broker.Client()

var once sync.Once
return func(c *gin.Context) {
c.Set(serverKey, broker)
c.Set(clientKey, client)
once.Do(func() {
// this is some really hacky stuff
// turns out I need to do some refactoring
// don't judge!
// will fix in 0.6 release
ctx := c.Copy()
client.Connect(
stomp.WithCredentials("x-token", secret),
)
client.Subscribe("/queue/updates", stomp.HandlerFunc(func(m *stomp.Message) {
go handlers.HandleUpdate(ctx, m.Copy())
}))
})
}
}
24 changes: 1 addition & 23 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,9 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
e.POST("/hook", server.PostHook)
e.POST("/api/hook", server.PostHook)

stream := e.Group("/api/stream")
{
stream.Use(session.SetRepo())
stream.Use(session.SetPerm())
stream.Use(session.MustPull)

stream.GET("/:owner/:name", server.GetRepoEvents)
stream.GET("/:owner/:name/:build/:number", server.GetStream)
}
ws := e.Group("/ws")
{
ws.GET("/broker", server.Broker)
ws.GET("/feed", server.EventStream)
ws.GET("/logs/:owner/:name/:build/:number",
session.SetRepo(),
Expand Down Expand Up @@ -152,20 +144,6 @@ func Load(middleware ...gin.HandlerFunc) http.Handler {
agents.GET("", server.GetAgents)
}

queue := e.Group("/api/queue")
{
queue.Use(session.AuthorizeAgent)
queue.POST("/pull", server.Pull)
queue.POST("/pull/:os/:arch", server.Pull)
queue.POST("/wait/:id", server.Wait)
queue.POST("/stream/:id", server.Stream)
queue.POST("/status/:id", server.Update)
queue.POST("/ping", server.Ping)

queue.POST("/logs/:id", server.PostLogs)
queue.GET("/logs/:id", server.WriteLogs)
}

// DELETE THESE
// gitlab := e.Group("/gitlab/:owner/:name")
// {
Expand Down
13 changes: 13 additions & 0 deletions server/broker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package server

import (
"net/http"

"github.com/gin-gonic/gin"
)

// Broker handles connections to the embedded message broker.
func Broker(c *gin.Context) {
broker := c.MustGet("broker").(http.Handler)
broker.ServeHTTP(c.Writer, c.Request)
}
Loading

0 comments on commit f2c1d46

Please sign in to comment.