Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Add ws dials for resource load and ide status #111

Merged
merged 3 commits into from
Sep 4, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add build log types
  • Loading branch information
f0ssel committed Sep 4, 2020
commit 89752dff12db022ccd13e0968c77a435892d47b5
30 changes: 25 additions & 5 deletions coder-sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package coder

import (
"context"
"fmt"
"net/http"
"time"

"cdr.dev/coder-cli/internal/x/xjson"
"golang.org/x/xerrors"
"nhooyr.io/websocket"
"nhooyr.io/websocket/wsjson"
)
Expand Down Expand Up @@ -138,25 +138,45 @@ func (c Client) DialResourceLoad(ctx context.Context, envID string) (*websocket.
return c.dialWs(ctx, "/api/environments/"+envID+"/watch-resource-load")
}

// BuildLogType describes the type of an event.
type BuildLogType string

const (
// BuildLogTypeStart signals that a new build log has begun.
BuildLogTypeStart BuildLogType = "start"
// BuildLogTypeStage is a stage-level event for an environment.
// It can be thought of as a major step in the environment's
// lifecycle.
BuildLogTypeStage BuildLogType = "stage"
// BuildLogTypeError describes an error that has occurred.
BuildLogTypeError BuildLogType = "error"
// BuildLogTypeSubstage describes a subevent that occurs as
// part of a stage. This can be the output from a user's
// personalization script, or a long running command.
BuildLogTypeSubstage BuildLogType = "substage"
// BuildLogTypeDone signals that the build has completed.
BuildLogTypeDone BuildLogType = "done"
)

type buildLogMsg struct {
Type string `json:"type"`
Type BuildLogType `json:"type"`
}

// WaitForEnvironmentReady will watch the build log and return when done
func (c Client) WaitForEnvironmentReady(ctx context.Context, env *Environment) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this need the full environment when the above function doesn't?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point.

conn, err := c.DialEnvironmentBuildLog(ctx, env.ID)
if err != nil {
return fmt.Errorf("%s: dial build log: %w", env.Name, err)
return xerrors.Errorf("%s: dial build log: %w", env.Name, err)
}

for {
msg := buildLogMsg{}
err := wsjson.Read(ctx, conn, &msg)
if err != nil {
return fmt.Errorf("%s: reading build log msg: %w", env.Name, err)
return xerrors.Errorf("%s: reading build log msg: %w", env.Name, err)
}

if msg.Type == "done" {
if msg.Type == BuildLogTypeDone {
return nil
}
}
Expand Down