-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from fieldryand/goroutine-stream-updates
Stream one execution per event
- Loading branch information
Showing
11 changed files
with
126 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,64 @@ | ||
package goflow | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Setting clientDisconnect to false makes testing possible. | ||
func (g *Goflow) stream(clientDisconnect bool) func(*gin.Context) { | ||
// 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) { | ||
chanStream := make(chan string) | ||
job := c.Query("jobname") | ||
|
||
history := make([]*execution, 0) | ||
|
||
// open a channel for live executions | ||
chanStream := make(chan *execution) | ||
|
||
// periodically push the list of job runs into the stream | ||
go func() { | ||
defer close(chanStream) | ||
// Periodically push the list of job runs into the stream | ||
for { | ||
for jobname := range g.Jobs { | ||
executions, _ := readExecutions(g.Store, jobname) | ||
marshalled, _ := marshalExecutions(jobname, executions) | ||
chanStream <- string(marshalled) | ||
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 { | ||
chanStream <- e | ||
history = append(history, e) | ||
} else if job == "" { | ||
chanStream <- e | ||
history = append(history, e) | ||
} | ||
} | ||
|
||
} | ||
} | ||
time.Sleep(time.Second * 1) | ||
} | ||
}() | ||
|
||
c.Stream(func(w io.Writer) bool { | ||
if msg, ok := <-chanStream; ok { | ||
c.SSEvent("message", msg) | ||
return clientDisconnect | ||
return keepOpen | ||
} | ||
return false | ||
}) | ||
} | ||
|
||
} | ||
|
||
// Obtain locks and put the response in the structure expected | ||
// by the streaming endpoint. | ||
func marshalExecutions(name string, executions []*execution) ([]byte, error) { | ||
var msg struct { | ||
JobName string `json:"jobName"` | ||
Executions []*execution `json:"executions"` | ||
} | ||
msg.JobName = name | ||
msg.Executions = executions | ||
result, ok := json.Marshal(msg) | ||
return result, ok | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,5 +44,5 @@ | |
</body> | ||
</html> | ||
<script> | ||
updateJobStateCircles(); | ||
indexPageEventListener(); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.