2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-09-22 20:48:01 +02:00
|
|
|
package api
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
import (
|
2017-03-05 09:56:08 +02:00
|
|
|
"context"
|
2016-09-26 07:39:28 +02:00
|
|
|
"fmt"
|
2017-07-18 18:27:20 +02:00
|
|
|
"io"
|
2017-03-16 12:14:02 +02:00
|
|
|
"net/http"
|
2015-09-30 03:21:17 +02:00
|
|
|
"strconv"
|
2016-07-09 00:40:29 +02:00
|
|
|
"time"
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2021-10-12 09:25:13 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-09-22 20:48:01 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server"
|
2021-09-23 22:29:09 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/logging"
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 22:29:09 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/pubsub"
|
2021-09-22 22:41:32 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/router/middleware/session"
|
2021-09-23 13:33:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2015-09-30 03:21:17 +02:00
|
|
|
)
|
|
|
|
|
2017-07-24 21:57:07 +02:00
|
|
|
//
|
|
|
|
// event source streaming for compatibility with quic and http2
|
|
|
|
//
|
|
|
|
|
2017-07-18 18:27:20 +02: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 19:23:22 +02:00
|
|
|
c.Header("X-Accel-Buffering", "no")
|
2017-07-18 18:27:20 +02:00
|
|
|
|
|
|
|
rw := c.Writer
|
|
|
|
|
|
|
|
flusher, ok := rw.(http.Flusher)
|
|
|
|
if !ok {
|
|
|
|
c.String(500, "Streaming not supported")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-22 17:49:44 +02:00
|
|
|
// ping the client
|
2021-11-23 16:36:52 +02:00
|
|
|
logWriteStringErr(io.WriteString(rw, ": ping\n\n"))
|
2017-08-22 17:49:44 +02:00
|
|
|
flusher.Flush()
|
|
|
|
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Debug().Msg("user feed: connection opened")
|
2017-07-18 18:27:20 +02:00
|
|
|
|
|
|
|
user := session.User(c)
|
|
|
|
repo := map[string]bool{}
|
|
|
|
if user != nil {
|
2021-10-26 21:29:30 +02:00
|
|
|
repos, _ := store.FromContext(c).RepoList(user, false)
|
2017-07-18 18:27:20 +02:00
|
|
|
for _, r := range repos {
|
|
|
|
repo[r.FullName] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
eventc := make(chan []byte, 10)
|
|
|
|
ctx, cancel := context.WithCancel(
|
|
|
|
context.Background(),
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
close(eventc)
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Debug().Msg("user feed: connection closed")
|
2017-07-18 18:27:20 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
2021-11-23 16:36:52 +02:00
|
|
|
err := server.Config.Services.Pubsub.Subscribe(ctx, "topic/events", func(m pubsub.Message) {
|
2018-11-14 12:53:02 +02:00
|
|
|
defer func() {
|
2021-11-23 16:36:52 +02:00
|
|
|
obj := recover() // fix #2480 // TODO: check if it's still needed
|
|
|
|
log.Trace().Msgf("pubsub subscribe recover return: %v", obj)
|
2018-11-14 12:53:02 +02:00
|
|
|
}()
|
2017-07-18 18:27:20 +02:00
|
|
|
name := m.Labels["repo"]
|
|
|
|
priv := m.Labels["private"]
|
|
|
|
if repo[name] || priv == "false" {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
eventc <- m.Data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-11-23 16:36:52 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Subscribe failed")
|
|
|
|
}
|
2017-07-18 18:27:20 +02:00
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rw.CloseNotify():
|
|
|
|
return
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2017-08-22 17:49:44 +02:00
|
|
|
case <-time.After(time.Second * 30):
|
2021-11-23 16:36:52 +02:00
|
|
|
logWriteStringErr(io.WriteString(rw, ": ping\n\n"))
|
2017-08-22 17:49:44 +02:00
|
|
|
flusher.Flush()
|
2017-07-18 18:27:20 +02:00
|
|
|
case buf, ok := <-eventc:
|
|
|
|
if ok {
|
2021-11-23 16:36:52 +02:00
|
|
|
logWriteStringErr(io.WriteString(rw, "data: "))
|
|
|
|
logWriteStringErr(rw.Write(buf))
|
|
|
|
logWriteStringErr(io.WriteString(rw, "\n\n"))
|
2017-07-18 18:27:20 +02:00
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-24 21:57:07 +02: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
|
|
|
|
}
|
|
|
|
|
2021-11-23 16:36:52 +02:00
|
|
|
logWriteStringErr(io.WriteString(rw, ": ping\n\n"))
|
2017-08-22 17:49:44 +02:00
|
|
|
flusher.Flush()
|
|
|
|
|
2021-10-28 11:12:58 +02:00
|
|
|
repo := session.Repo(c)
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2021-10-28 11:12:58 +02:00
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
// // parse the pipeline number and step sequence number from
|
2022-11-09 09:12:17 +02:00
|
|
|
// // the request parameter.
|
2022-10-18 03:24:12 +02:00
|
|
|
pipelinen, _ := strconv.ParseInt(c.Param("pipeline"), 10, 64)
|
2022-10-28 17:38:53 +02:00
|
|
|
stepn, _ := strconv.Atoi(c.Param("number"))
|
2017-07-24 21:57:07 +02:00
|
|
|
|
2022-10-18 03:24:12 +02:00
|
|
|
pipeline, err := _store.GetPipelineNumber(repo, pipelinen)
|
2017-07-24 21:57:07 +02:00
|
|
|
if err != nil {
|
2022-10-18 03:24:12 +02:00
|
|
|
log.Debug().Msgf("stream cannot get pipeline number: %v", err)
|
|
|
|
logWriteStringErr(io.WriteString(rw, "event: error\ndata: pipeline not found\n\n"))
|
2017-07-24 21:57:07 +02:00
|
|
|
return
|
|
|
|
}
|
2022-10-28 17:38:53 +02:00
|
|
|
step, err := _store.StepFind(pipeline, stepn)
|
2017-07-24 21:57:07 +02:00
|
|
|
if err != nil {
|
2022-10-28 17:38:53 +02:00
|
|
|
log.Debug().Msgf("stream cannot get step number: %v", err)
|
2021-11-23 16:36:52 +02:00
|
|
|
logWriteStringErr(io.WriteString(rw, "event: error\ndata: process not found\n\n"))
|
2017-07-24 21:57:07 +02:00
|
|
|
return
|
|
|
|
}
|
2022-10-28 17:38:53 +02:00
|
|
|
if step.State != model.StatusRunning {
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Debug().Msg("stream not found.")
|
2021-11-23 16:36:52 +02:00
|
|
|
logWriteStringErr(io.WriteString(rw, "event: error\ndata: stream not found\n\n"))
|
2017-07-24 21:57:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
logc := make(chan []byte, 10)
|
|
|
|
ctx, cancel := context.WithCancel(
|
|
|
|
context.Background(),
|
|
|
|
)
|
|
|
|
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Debug().Msgf("log stream: connection opened")
|
2017-07-24 21:57:07 +02:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
close(logc)
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Debug().Msgf("log stream: connection closed")
|
2017-07-24 21:57:07 +02:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
// TODO remove global variable
|
2022-10-28 17:38:53 +02:00
|
|
|
err := server.Config.Services.Logs.Tail(ctx, fmt.Sprint(step.ID), func(entries ...*logging.Entry) {
|
2018-10-19 07:33:56 +02:00
|
|
|
defer func() {
|
2021-11-23 16:36:52 +02:00
|
|
|
obj := recover() // fix #2480 // TODO: check if it's still needed
|
|
|
|
log.Trace().Msgf("pubsub subscribe recover return: %v", obj)
|
2018-10-19 07:33:56 +02:00
|
|
|
}()
|
2017-07-24 21:57:07 +02:00
|
|
|
for _, entry := range entries {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
logc <- entry.Data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2021-11-23 16:36:52 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("tail of logs failed")
|
|
|
|
}
|
2017-07-24 21:57:07 +02:00
|
|
|
|
2021-11-23 16:36:52 +02:00
|
|
|
logWriteStringErr(io.WriteString(rw, "event: error\ndata: eof\n\n"))
|
2017-07-24 21:57:07 +02:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
id := 1
|
|
|
|
last, _ := strconv.Atoi(
|
|
|
|
c.Request.Header.Get("Last-Event-ID"),
|
|
|
|
)
|
|
|
|
if last != 0 {
|
2021-10-12 09:25:13 +02:00
|
|
|
log.Debug().Msgf("log stream: reconnect: last-event-id: %d", last)
|
2017-07-24 21:57:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2017-08-22 17:49:44 +02:00
|
|
|
case <-time.After(time.Second * 30):
|
2021-11-23 16:36:52 +02:00
|
|
|
logWriteStringErr(io.WriteString(rw, ": ping\n\n"))
|
2017-08-22 17:49:44 +02:00
|
|
|
flusher.Flush()
|
2017-07-24 21:57:07 +02:00
|
|
|
case buf, ok := <-logc:
|
|
|
|
if ok {
|
|
|
|
if id > last {
|
2021-11-23 16:36:52 +02:00
|
|
|
logWriteStringErr(io.WriteString(rw, "id: "+strconv.Itoa(id)))
|
|
|
|
logWriteStringErr(io.WriteString(rw, "\n"))
|
|
|
|
logWriteStringErr(io.WriteString(rw, "data: "))
|
|
|
|
logWriteStringErr(rw.Write(buf))
|
|
|
|
logWriteStringErr(io.WriteString(rw, "\n\n"))
|
2017-07-24 21:57:07 +02:00
|
|
|
flusher.Flush()
|
|
|
|
}
|
|
|
|
id++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-23 16:36:52 +02:00
|
|
|
|
|
|
|
func logWriteStringErr(_ int, err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Caller(1).Msg("fail to write string")
|
|
|
|
}
|
|
|
|
}
|