-
Notifications
You must be signed in to change notification settings - Fork 25
/
stream.go
68 lines (55 loc) · 1.39 KB
/
stream.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package goflow
import (
"encoding/json"
"fmt"
"net/http"
"time"
)
// Set keepOpen to false when testing--one event will be sent and
// then the channel is closed by the server.
func (g *Goflow) handleStream(w http.ResponseWriter, r *http.Request) {
job := r.PathValue("name")
keepOpen := r.URL.Query().Get("keepopen")
flusher, err := w.(http.Flusher)
if !err {
http.Error(w, "Streaming unsupported!", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.WriteHeader(http.StatusOK)
history := make([]*Execution, 0)
for {
select {
case <-r.Context().Done():
return
default:
for jobname := range g.Jobs {
executions, _ := readExecutions(g.Store, jobname)
for _, e := range executions {
// make sure it wasn't already sent
inHistory := false
for _, h := range history {
if e.ID == h.ID && e.ModifiedTs == h.ModifiedTs {
inHistory = true
}
}
if !inHistory {
if (job != "" && job == e.JobName) || job == "" {
out, _ := json.Marshal(e)
w.Write([]byte(fmt.Sprintf("data: %s\n", out)))
w.Write([]byte("\n"))
flusher.Flush()
history = append(history, e)
}
}
}
}
if keepOpen == "false" {
return
}
time.Sleep(time.Second * 1)
}
}
}