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.* *.deb.*
*.rpm *.rpm
*.out *.out
*.prof
*.rice-box.go *.rice-box.go
*.db *.db
*.txt *.txt

View file

@ -17,6 +17,8 @@ import (
eventbus "github.com/drone/drone/eventbus/builtin" eventbus "github.com/drone/drone/eventbus/builtin"
queue "github.com/drone/drone/queue/builtin" queue "github.com/drone/drone/queue/builtin"
runner "github.com/drone/drone/runner/builtin" runner "github.com/drone/drone/runner/builtin"
_ "net/http/pprof"
) )
var conf = flag.String("config", "drone.toml", "") 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)) raw = []byte(inject.InjectSafe(string(raw), params))
} }
c.JSON(202, build)
queue_.Publish(&queue.Work{ queue_.Publish(&queue.Work{
User: user, User: user,
Repo: repo, Repo: repo,
@ -185,8 +187,6 @@ func RunBuild(c *gin.Context) {
Netrc: netrc, Netrc: netrc,
Yaml: raw, Yaml: raw,
}) })
c.JSON(202, build)
} }
// KillBuild accepts a request to kill a running build. // KillBuild accepts a request to kill a running build.

View file

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

View file

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