fixed some race conditions and cpu locking

This commit is contained in:
Brad Rydzewski 2015-05-05 20:59:07 -07:00
parent 80d8d10c15
commit 091f0e9918
5 changed files with 17 additions and 7 deletions

1
.gitignore vendored
View file

@ -9,6 +9,7 @@ drone.sublime-workspace
*.deb.*
*.rpm
*.out
*.prof
*.rice-box.go
*.db
*.txt

View file

@ -17,6 +17,8 @@ import (
eventbus "github.com/drone/drone/eventbus/builtin"
queue "github.com/drone/drone/queue/builtin"
runner "github.com/drone/drone/runner/builtin"
_ "net/http/pprof"
)
var conf = flag.String("config", "drone.toml", "")

View file

@ -177,6 +177,8 @@ func RunBuild(c *gin.Context) {
raw = []byte(inject.InjectSafe(string(raw), params))
}
c.JSON(202, build)
queue_.Publish(&queue.Work{
User: user,
Repo: repo,
@ -185,8 +187,6 @@ func RunBuild(c *gin.Context) {
Netrc: netrc,
Yaml: raw,
})
c.JSON(202, build)
}
// KillBuild accepts a request to kill a running build.

View file

@ -145,6 +145,8 @@ func PostHook(c *gin.Context) {
return
}
c.JSON(200, build)
queue_.Publish(&queue.Work{
User: user,
Repo: repo,
@ -153,6 +155,4 @@ func PostHook(c *gin.Context) {
Netrc: netrc,
Yaml: raw,
})
c.JSON(200, build)
}

View file

@ -46,23 +46,29 @@ func GetRepoEvents(c *gin.Context) {
}
ticker := time.NewTicker(pingPeriod)
eventc := make(chan *eventbus.Event, 1)
eventc := make(chan *eventbus.Event)
bus.Subscribe(eventc)
defer func() {
bus.Unsubscribe(eventc)
ticker.Stop()
ws.Close()
close(eventc)
log.Debugf("closed websocket")
log.Infof("closed websocket")
}()
go func() {
for {
select {
case <-c.Writer.CloseNotify():
ws.Close()
return
case event := <-eventc:
if event != nil && event.Kind == eventbus.EventRepo && event.Name == repo.FullName {
if event == nil {
log.Infof("closed websocket")
ws.Close()
return
}
if event.Kind == eventbus.EventRepo && event.Name == repo.FullName {
ws.WriteMessage(websocket.TextMessage, event.Msg)
break
}
@ -70,6 +76,7 @@ func GetRepoEvents(c *gin.Context) {
ws.SetWriteDeadline(time.Now().Add(writeWait))
err := ws.WriteMessage(websocket.PingMessage, []byte{})
if err != nil {
log.Infof("closed websocket")
ws.Close()
return
}