Skip to content

Commit

Permalink
Merge pull request #360 from Scalingo/fix/359/fix-stream
Browse files Browse the repository at this point in the history
Since multiple deployment can be run on parallel and the new event is not always fetched, ensure logs are streamed after 'scalingo deploy .jar/war
  • Loading branch information
EtienneM authored Jun 5, 2018
2 parents 1ffb8cb + 00b8159 commit 41809bd
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions deployments/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,22 @@ func Stream(opts *StreamOpts) error {
return errgo.Mask(err, errgo.Any)
}

var event deployEvent
var currentDeployment *scalingo.Deployment
oldStatus := ""
// This method can focus on one given deployment and will on display events
// related to this deployment
currentDeployment := &scalingo.Deployment{
ID: opts.DeploymentID,
}
// If the method is called without any specific deployment, ie. `scalingo
// deployment-follow` all events from all deployments will be displayed
anyDeployment := currentDeployment.ID == ""

// Statuses is a map of deploymentID -> current status of the deployment
// Why are we keeping it? To be able to say when a new 'status' event arrives
// Deployment X status has changed from 'building' to 'pushing' for instance.
statuses := map[string]string{}

for {
var event deployEvent
err := websocket.JSON.Receive(conn, &event)
if err != nil {
conn.Close()
Expand All @@ -72,7 +84,7 @@ func Stream(opts *StreamOpts) error {
case "ping":
case "log":
// If we stream logs of a specific deployment and this event is not about this one
if opts.DeploymentID != "" && (currentDeployment == nil || opts.DeploymentID != currentDeployment.ID) {
if !anyDeployment && event.ID != currentDeployment.ID {
continue
}
var logData logData
Expand All @@ -84,7 +96,7 @@ func Stream(opts *StreamOpts) error {
fmt.Println("[LOG] " + strings.TrimSpace(logData.Content))
case "status":
// If we stream logs of a specific deployment and this event is not about this one
if opts.DeploymentID != "" && (currentDeployment == nil || opts.DeploymentID != currentDeployment.ID) {
if !anyDeployment && event.ID != currentDeployment.ID {
continue
}
var statusData statusData
Expand All @@ -93,25 +105,32 @@ func Stream(opts *StreamOpts) error {
config.C.Logger.Println(err)
continue
}
if oldStatus == "" {
if statuses[event.ID] == "" {
fmt.Println("[STATUS] New status: " + statusData.Content)
} else {
fmt.Println("[STATUS] New status: " + oldStatus + " → " + statusData.Content)
fmt.Println("[STATUS] New status: " + statuses[event.ID] + " → " + statusData.Content)
}
oldStatus = statusData.Content
if opts.DeploymentID != "" && scalingo.IsFinishedString(scalingo.DeploymentStatus(statusData.Content)) {
statuses[event.ID] = statusData.Content

if !anyDeployment && scalingo.IsFinishedString(scalingo.DeploymentStatus(statusData.Content)) {
return nil
}
case "new":
oldStatus = ""
var newData map[string]*scalingo.Deployment
err := json.Unmarshal(event.Data, &newData)
if err != nil {
config.C.Logger.Println(err)
continue
}
currentDeployment = newData["deployment"]
fmt.Println("[NEW] New deploy: " + currentDeployment.ID + " from " + currentDeployment.User.Username)
newDeployment := newData["deployment"]

if newDeployment.ID == currentDeployment.ID {
currentDeployment = newDeployment
}

if anyDeployment || newDeployment.ID == currentDeployment.ID {
fmt.Println("[NEW] New deploy: " + newDeployment.ID + " from " + newDeployment.User.Username)
}
}
}
}
Expand Down

0 comments on commit 41809bd

Please sign in to comment.