woodpecker/server/stream.go

406 lines
8.4 KiB
Go
Raw Normal View History

2016-05-02 19:21:25 +00:00
package server
2015-09-30 01:21:17 +00:00
import (
2017-03-05 07:56:08 +00:00
"context"
"fmt"
2017-07-18 16:27:20 +00:00
"io"
2017-03-16 10:14:02 +00:00
"net/http"
2015-09-30 01:21:17 +00:00
"strconv"
"time"
2015-09-30 01:21:17 +00:00
2017-03-05 07:56:08 +00:00
"github.com/cncd/logging"
"github.com/cncd/pubsub"
"github.com/drone/drone/model"
2015-09-30 01:21:17 +00:00
"github.com/drone/drone/router/middleware/session"
"github.com/drone/drone/store"
2015-09-30 01:21:17 +00:00
"github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/gorilla/websocket"
2015-09-30 01:21:17 +00:00
)
var (
// Time allowed to write the file to the client.
writeWait = 5 * time.Second
// Time allowed to read the next pong message from the client.
pongWait = 60 * time.Second
// Send pings to client with this period. Must be less than pongWait.
pingPeriod = 30 * time.Second
2017-03-16 10:14:02 +00:00
// upgrader defines the default behavior for upgrading the websocket.
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
2017-03-16 10:14:02 +00:00
)
func reader(ws *websocket.Conn) {
defer ws.Close()
ws.SetReadLimit(512)
ws.SetReadDeadline(time.Now().Add(pongWait))
ws.SetPongHandler(func(string) error {
ws.SetReadDeadline(time.Now().Add(pongWait))
return nil
})
for {
_, _, err := ws.ReadMessage()
if err != nil {
break
}
}
2015-09-30 01:21:17 +00:00
}
2017-03-05 07:56:08 +00:00
2017-03-16 10:14:02 +00:00
func LogStream(c *gin.Context) {
2017-03-05 07:56:08 +00:00
repo := session.Repo(c)
buildn, _ := strconv.Atoi(c.Param("build"))
jobn, _ := strconv.Atoi(c.Param("number"))
build, err := store.GetBuildNumber(c, repo, buildn)
if err != nil {
logrus.Debugln("stream cannot get build number.", err)
c.AbortWithError(404, err)
return
}
2017-04-02 14:13:26 +00:00
proc, err := store.FromContext(c).ProcFind(build, jobn)
2017-03-05 07:56:08 +00:00
if err != nil {
2017-04-02 14:13:26 +00:00
logrus.Debugln("stream cannot get proc number.", err)
2017-03-05 07:56:08 +00:00
c.AbortWithError(404, err)
return
}
2017-04-02 14:13:26 +00:00
if proc.State != model.StatusRunning {
2017-03-05 07:56:08 +00:00
logrus.Debugln("stream not found.")
c.AbortWithStatus(404)
return
}
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
logrus.Errorf("Cannot upgrade websocket. %s", err)
}
return
}
logrus.Debugf("Successfull upgraded websocket")
ticker := time.NewTicker(pingPeriod)
logc := make(chan []byte, 10)
ctx, cancel := context.WithCancel(
context.Background(),
)
defer func() {
cancel()
ticker.Stop()
close(logc)
logrus.Debugf("Successfully closing websocket")
}()
go func() {
// TODO remove global variable
Config.Services.Logs.Tail(ctx, fmt.Sprint(proc.ID), func(entries ...*logging.Entry) {
2017-03-05 07:56:08 +00:00
for _, entry := range entries {
select {
case <-ctx.Done():
return
default:
logc <- entry.Data
}
}
})
cancel()
}()
go func() {
for {
select {
case <-ctx.Done():
return
case buf, ok := <-logc:
if ok {
ws.SetWriteDeadline(time.Now().Add(writeWait))
ws.WriteMessage(websocket.TextMessage, buf)
}
case <-ticker.C:
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(writeWait))
if err != nil {
return
}
}
}
}()
reader(ws)
}
2017-03-16 10:14:02 +00:00
func EventStream(c *gin.Context) {
2017-03-05 07:56:08 +00:00
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
if err != nil {
if _, ok := err.(websocket.HandshakeError); !ok {
logrus.Errorf("Cannot upgrade websocket. %s", err)
}
return
}
logrus.Debugf("Successfull upgraded websocket")
user := session.User(c)
repo := map[string]bool{}
if user != nil {
2017-07-14 19:58:38 +00:00
repos, _ := store.FromContext(c).RepoList(user)
for _, r := range repos {
repo[r.FullName] = true
}
2017-03-05 07:56:08 +00:00
}
ticker := time.NewTicker(pingPeriod)
eventc := make(chan []byte, 10)
ctx, cancel := context.WithCancel(
context.Background(),
)
defer func() {
cancel()
ticker.Stop()
close(eventc)
logrus.Debugf("Successfully closing websocket")
}()
go func() {
// TODO remove this from global config
Config.Services.Pubsub.Subscribe(c, "topic/events", func(m pubsub.Message) {
2017-03-05 07:56:08 +00:00
name := m.Labels["repo"]
priv := m.Labels["private"]
if repo[name] || priv == "false" {
select {
case <-ctx.Done():
return
default:
eventc <- m.Data
}
}
})
cancel()
}()
go func() {
for {
select {
case <-ctx.Done():
return
case buf, ok := <-eventc:
if ok {
ws.SetWriteDeadline(time.Now().Add(writeWait))
ws.WriteMessage(websocket.TextMessage, buf)
}
case <-ticker.C:
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(writeWait))
if err != nil {
return
}
}
}
}()
reader(ws)
}
2017-07-18 16:27:20 +00:00
2017-07-24 19:57:07 +00:00
//
// event source streaming for compatibility with quic and http2
//
2017-07-18 16:27:20 +00:00
func EventStreamSSE(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
2017-07-24 17:23:22 +00:00
c.Header("X-Accel-Buffering", "no")
2017-07-18 16:27:20 +00:00
rw := c.Writer
flusher, ok := rw.(http.Flusher)
if !ok {
c.String(500, "Streaming not supported")
return
}
logrus.Debugf("user feed: connection opened")
user := session.User(c)
repo := map[string]bool{}
if user != nil {
repos, _ := store.FromContext(c).RepoList(user)
for _, r := range repos {
repo[r.FullName] = true
}
}
eventc := make(chan []byte, 10)
ctx, cancel := context.WithCancel(
context.Background(),
)
defer func() {
cancel()
close(eventc)
logrus.Debugf("user feed: connection closed")
}()
go func() {
// TODO remove this from global config
Config.Services.Pubsub.Subscribe(c, "topic/events", func(m pubsub.Message) {
name := m.Labels["repo"]
priv := m.Labels["private"]
if repo[name] || priv == "false" {
select {
case <-ctx.Done():
return
default:
eventc <- m.Data
}
}
})
cancel()
}()
for {
select {
case <-rw.CloseNotify():
return
case <-ctx.Done():
return
case buf, ok := <-eventc:
if ok {
io.WriteString(rw, "data: ")
rw.Write(buf)
io.WriteString(rw, "\n\n")
flusher.Flush()
}
}
}
}
2017-07-24 19:57:07 +00:00
func LogStreamSSE(c *gin.Context) {
c.Header("Content-Type", "text/event-stream")
c.Header("Cache-Control", "no-cache")
c.Header("Connection", "keep-alive")
c.Header("X-Accel-Buffering", "no")
rw := c.Writer
flusher, ok := rw.(http.Flusher)
if !ok {
c.String(500, "Streaming not supported")
return
}
// repo := session.Repo(c)
//
// // parse the build number and job sequence number from
// // the repquest parameter.
// num, _ := strconv.Atoi(c.Params.ByName("number"))
// ppid, _ := strconv.Atoi(c.Params.ByName("ppid"))
// name := c.Params.ByName("proc")
//
// build, err := store.GetBuildNumber(c, repo, num)
// if err != nil {
// c.AbortWithError(404, err)
// return
// }
//
// proc, err := store.FromContext(c).ProcChild(build, ppid, name)
// if err != nil {
// c.AbortWithError(404, err)
// return
// }
repo := session.Repo(c)
buildn, _ := strconv.Atoi(c.Param("build"))
jobn, _ := strconv.Atoi(c.Param("number"))
build, err := store.GetBuildNumber(c, repo, buildn)
if err != nil {
logrus.Debugln("stream cannot get build number.", err)
io.WriteString(rw, "event: error\ndata: build not found\n\n")
return
}
proc, err := store.FromContext(c).ProcFind(build, jobn)
if err != nil {
logrus.Debugln("stream cannot get proc number.", err)
io.WriteString(rw, "event: error\ndata: process not found\n\n")
return
}
if proc.State != model.StatusRunning {
logrus.Debugln("stream not found.")
io.WriteString(rw, "event: error\ndata: stream not found\n\n")
return
}
logc := make(chan []byte, 10)
ctx, cancel := context.WithCancel(
context.Background(),
)
logrus.Debugf("log stream: connection opened")
defer func() {
cancel()
close(logc)
logrus.Debugf("log stream: connection closed")
}()
go func() {
// TODO remove global variable
Config.Services.Logs.Tail(ctx, fmt.Sprint(proc.ID), func(entries ...*logging.Entry) {
for _, entry := range entries {
select {
case <-ctx.Done():
return
default:
logc <- entry.Data
}
}
})
io.WriteString(rw, "event: error\ndata: eof\n\n")
cancel()
}()
id := 1
last, _ := strconv.Atoi(
c.Request.Header.Get("Last-Event-ID"),
)
if last != 0 {
logrus.Debugf("log stream: reconnect: last-event-id: %d", last)
}
// retry: 10000\n
for {
select {
// after 1 hour of idle (no response) end the stream.
// this is more of a safety mechanism than anything,
// and can be removed once the code is more mature.
case <-time.After(time.Hour):
return
case <-rw.CloseNotify():
return
case <-ctx.Done():
return
case buf, ok := <-logc:
if ok {
if id > last {
io.WriteString(rw, "id: "+strconv.Itoa(id))
io.WriteString(rw, "\n")
io.WriteString(rw, "data: ")
rw.Write(buf)
io.WriteString(rw, "\n\n")
flusher.Flush()
}
id++
}
}
}
}