-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathstream.go
50 lines (37 loc) · 1001 Bytes
/
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
package goflow
import (
"io"
"time"
"github.com/gin-gonic/gin"
)
// Set keepOpen to false when testing--one event will be sent and
// then the channel is closed by the server.
func (g *Goflow) stream(keepOpen bool) func(*gin.Context) {
return func(c *gin.Context) {
job := c.Query("jobname")
history := make([]*execution, 0)
// periodically push the list of job runs into the stream
c.Stream(func(w io.Writer) bool {
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.ModifiedTimestamp == h.ModifiedTimestamp {
inHistory = true
}
}
if !inHistory {
if (job != "" && job == e.JobName) || job == "" {
c.SSEvent("message", e)
history = append(history, e)
}
}
}
}
time.Sleep(time.Second * 1)
return keepOpen
})
}
}