2016-03-31 08:27:53 +02:00
|
|
|
package web
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
import (
|
2016-04-28 23:10:32 +02:00
|
|
|
"bufio"
|
|
|
|
"encoding/json"
|
2015-09-30 03:21:17 +02:00
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
2016-04-28 23:10:32 +02:00
|
|
|
"github.com/drone/drone/bus"
|
|
|
|
"github.com/drone/drone/model"
|
2015-09-30 03:21:17 +02:00
|
|
|
"github.com/drone/drone/router/middleware/session"
|
2015-10-22 01:14:02 +02:00
|
|
|
"github.com/drone/drone/store"
|
2016-04-28 23:10:32 +02:00
|
|
|
"github.com/drone/drone/stream"
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/manucorporat/sse"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetRepoEvents will upgrade the connection to a Websocket and will stream
|
|
|
|
// event updates to the browser.
|
|
|
|
func GetRepoEvents(c *gin.Context) {
|
|
|
|
repo := session.Repo(c)
|
|
|
|
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
2016-04-28 23:10:32 +02:00
|
|
|
eventc := make(chan *bus.Event, 1)
|
|
|
|
bus.Subscribe(c, eventc)
|
2015-09-30 03:21:17 +02:00
|
|
|
defer func() {
|
2016-04-28 23:10:32 +02:00
|
|
|
bus.Unsubscribe(c, eventc)
|
2015-09-30 03:21:17 +02:00
|
|
|
close(eventc)
|
|
|
|
log.Infof("closed event stream")
|
|
|
|
}()
|
|
|
|
|
|
|
|
c.Stream(func(w io.Writer) bool {
|
|
|
|
select {
|
|
|
|
case event := <-eventc:
|
|
|
|
if event == nil {
|
|
|
|
log.Infof("nil event received")
|
|
|
|
return false
|
|
|
|
}
|
2016-04-28 23:10:32 +02:00
|
|
|
|
|
|
|
// TODO(bradrydzewski) This is a super hacky workaround until we improve
|
|
|
|
// the actual bus. Having a per-call database event is just plain stupid.
|
|
|
|
if event.Repo.FullName == repo.FullName {
|
|
|
|
|
|
|
|
var payload = struct {
|
|
|
|
model.Build
|
|
|
|
Jobs []*model.Job `json:"jobs"`
|
|
|
|
}{}
|
|
|
|
payload.Build = event.Build
|
|
|
|
payload.Jobs, _ = store.GetJobList(c, &event.Build)
|
|
|
|
data, _ := json.Marshal(&payload)
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
sse.Encode(w, sse.Event{
|
|
|
|
Event: "message",
|
2016-04-28 23:10:32 +02:00
|
|
|
Data: string(data),
|
2015-09-30 03:21:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
case <-c.Writer.CloseNotify():
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetStream(c *gin.Context) {
|
2015-10-22 01:14:02 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
repo := session.Repo(c)
|
|
|
|
buildn, _ := strconv.Atoi(c.Param("build"))
|
|
|
|
jobn, _ := strconv.Atoi(c.Param("number"))
|
|
|
|
|
|
|
|
c.Writer.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
build, err := store.GetBuildNumber(c, repo, buildn)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Debugln("stream cannot get build number.", err)
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
2015-10-22 01:14:02 +02:00
|
|
|
job, err := store.GetJobNumber(c, build, jobn)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Debugln("stream cannot get job number.", err)
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-04-28 23:10:32 +02:00
|
|
|
rc, err := stream.Reader(c, stream.ToKey(job.ID))
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithError(404, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-c.Writer.CloseNotify()
|
|
|
|
rc.Close()
|
|
|
|
}()
|
|
|
|
|
2016-04-28 23:10:32 +02:00
|
|
|
var line int
|
|
|
|
var scanner = bufio.NewScanner(rc)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line++
|
|
|
|
var err = sse.Encode(c.Writer, sse.Event{
|
|
|
|
Id: strconv.Itoa(line),
|
|
|
|
Event: "message",
|
|
|
|
Data: scanner.Text(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c.Writer.Flush()
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2016-04-28 23:10:32 +02:00
|
|
|
log.Debugf("Closed stream %s#%d", repo.FullName, build.Number)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|