We can stream the deployment logs of an application, or we can stream the logs of a specific deployments. The StreamOpts.DeploymentID argument is optional.
(ctx context.Context, opts *StreamOpts)
| 40 | // deployments. |
| 41 | // The StreamOpts.DeploymentID argument is optional. |
| 42 | func Stream(ctx context.Context, opts *StreamOpts) error { |
| 43 | c, err := config.ScalingoClient(ctx) |
| 44 | if err != nil { |
| 45 | return errors.Wrapf(ctx, err, "fail to get Scalingo client") |
| 46 | } |
| 47 | |
| 48 | app, err := c.AppsShow(ctx, opts.AppName) |
| 49 | if err != nil { |
| 50 | return errors.Wrapf(ctx, err, "get app %s", opts.AppName) |
| 51 | } |
| 52 | |
| 53 | debug.Println("Opening socket to: " + app.Links.DeploymentsStream) |
| 54 | |
| 55 | conn, err := c.DeploymentStream(ctx, app.Links.DeploymentsStream) |
| 56 | if err != nil { |
| 57 | return errors.Wrap(ctx, err, "open deployment event stream") |
| 58 | } |
| 59 | |
| 60 | // This method can focus on one given deployment and will on display events |
| 61 | // related to this deployment |
| 62 | currentDeployment := &scalingo.Deployment{ |
| 63 | ID: opts.DeploymentID, |
| 64 | } |
| 65 | // If the method is called without any specific deployment, ie. `scalingo |
| 66 | // deployment-follow` all events from all deployments will be displayed |
| 67 | anyDeployment := currentDeployment.ID == "" |
| 68 | |
| 69 | // Statuses is a map of deploymentID -> current status of the deployment |
| 70 | // Why are we keeping it? To be able to say when a new 'status' event arrives |
| 71 | // Deployment X status has changed from 'building' to 'pushing' for instance. |
| 72 | statuses := map[string]string{} |
| 73 | |
| 74 | for { |
| 75 | var event deployEvent |
| 76 | err := conn.ReadJSON(&event) |
| 77 | if err != nil { |
| 78 | conn.Close() |
| 79 | if err == stdio.EOF { |
| 80 | debug.Println("Remote server broke the connection, reconnecting") |
| 81 | for err != nil { |
| 82 | conn, err = c.DeploymentStream(ctx, app.Links.DeploymentsStream) |
| 83 | time.Sleep(time.Second * 1) |
| 84 | } |
| 85 | continue |
| 86 | } else { |
| 87 | return errors.Wrap(ctx, err, "read deployment event from stream") |
| 88 | } |
| 89 | } else { |
| 90 | switch event.Type { |
| 91 | case "ping": |
| 92 | case "log": |
| 93 | // If we stream logs of a specific deployment and this event is not about this one |
| 94 | if !anyDeployment && event.ID != currentDeployment.ID { |
| 95 | continue |
| 96 | } |
| 97 | var logData logData |
| 98 | err := json.Unmarshal(event.Data, &logData) |
| 99 | if err != nil { |
no test coverage detected