Skip to content

Commit 13e303d

Browse files
committed
Fix comments
1 parent 048f29a commit 13e303d

File tree

3 files changed

+54
-31
lines changed

3 files changed

+54
-31
lines changed

cli/cliui/provisionerjob.go

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,34 @@ import (
88
"sync"
99
"time"
1010

11+
"github.com/google/uuid"
1112
"github.com/spf13/cobra"
1213
"golang.org/x/xerrors"
1314

1415
"github.com/coder/coder/coderd/database"
1516
"github.com/coder/coder/codersdk"
1617
)
1718

19+
func WorkspaceBuild(cmd *cobra.Command, client *codersdk.Client, build uuid.UUID, before time.Time) error {
20+
return ProvisionerJob(cmd, ProvisionerJobOptions{
21+
Fetch: func() (codersdk.ProvisionerJob, error) {
22+
build, err := client.WorkspaceBuild(cmd.Context(), build)
23+
return build.Job, err
24+
},
25+
Logs: func() (<-chan codersdk.ProvisionerJobLog, error) {
26+
return client.WorkspaceBuildLogsAfter(cmd.Context(), build, before)
27+
},
28+
})
29+
}
30+
1831
type ProvisionerJobOptions struct {
1932
Fetch func() (codersdk.ProvisionerJob, error)
2033
Cancel func() error
2134
Logs func() (<-chan codersdk.ProvisionerJobLog, error)
2235

2336
FetchInterval time.Duration
37+
// Verbose determines whether debug and trace logs will be shown.
38+
Verbose bool
2439
}
2540

2641
// ProvisionerJob renders a provisioner job with interactive cancellation.
@@ -35,7 +50,7 @@ func ProvisionerJob(cmd *cobra.Command, opts ProvisionerJobOptions) error {
3550
didLogBetweenStage = false
3651
ctx, cancelFunc = context.WithCancel(cmd.Context())
3752

38-
errChan = make(chan error)
53+
errChan = make(chan error, 1)
3954
job codersdk.ProvisionerJob
4055
jobMutex sync.Mutex
4156
)
@@ -44,7 +59,6 @@ func ProvisionerJob(cmd *cobra.Command, opts ProvisionerJobOptions) error {
4459
printStage := func() {
4560
_, _ = fmt.Fprintf(cmd.OutOrStdout(), Styles.Prompt.Render("⧗")+"%s\n", Styles.Field.Render(currentStage))
4661
}
47-
printStage()
4862

4963
updateStage := func(stage string, startedAt time.Time) {
5064
if currentStage != "" {
@@ -88,29 +102,32 @@ func ProvisionerJob(cmd *cobra.Command, opts ProvisionerJobOptions) error {
88102
}
89103
updateJob()
90104

91-
// Handles ctrl+c to cancel a job.
92-
stopChan := make(chan os.Signal, 1)
93-
defer signal.Stop(stopChan)
94-
go func() {
105+
if opts.Cancel != nil {
106+
// Handles ctrl+c to cancel a job.
107+
stopChan := make(chan os.Signal, 1)
95108
signal.Notify(stopChan, os.Interrupt)
96-
select {
97-
case <-ctx.Done():
98-
return
99-
case _, ok := <-stopChan:
100-
if !ok {
109+
go func() {
110+
defer signal.Stop(stopChan)
111+
select {
112+
case <-ctx.Done():
101113
return
114+
case _, ok := <-stopChan:
115+
if !ok {
116+
return
117+
}
102118
}
103-
}
104-
// Stop listening for signals so another one kills it!
105-
signal.Stop(stopChan)
106-
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\033[2K\r\n"+Styles.FocusedPrompt.String()+Styles.Bold.Render("Gracefully canceling... wait for exit or data loss may occur!")+"\n\n")
107-
err := opts.Cancel()
108-
if err != nil {
109-
errChan <- xerrors.Errorf("cancel: %w", err)
110-
return
111-
}
112-
updateJob()
113-
}()
119+
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "\033[2K\r\n"+Styles.FocusedPrompt.String()+Styles.Bold.Render("Gracefully canceling...")+"\n\n")
120+
err := opts.Cancel()
121+
if err != nil {
122+
errChan <- xerrors.Errorf("cancel: %w", err)
123+
return
124+
}
125+
updateJob()
126+
}()
127+
}
128+
129+
// The initial stage needs to print after the signal handler has been registered.
130+
printStage()
114131

115132
logs, err := opts.Logs()
116133
if err != nil {
@@ -128,8 +145,6 @@ func ProvisionerJob(cmd *cobra.Command, opts ProvisionerJobOptions) error {
128145
updateJob()
129146
case log, ok := <-logs:
130147
if !ok {
131-
// The logs stream will end when the job does,
132-
// so it's safe to
133148
updateJob()
134149
jobMutex.Lock()
135150
if job.CompletedAt != nil {
@@ -144,26 +159,33 @@ func ProvisionerJob(cmd *cobra.Command, opts ProvisionerJobOptions) error {
144159
return nil
145160
case codersdk.ProvisionerJobFailed:
146161
}
162+
err = xerrors.New(job.Error)
147163
jobMutex.Unlock()
148-
return xerrors.New(job.Error)
164+
return err
149165
}
150166
output := ""
151167
switch log.Level {
152-
case database.LogLevelTrace, database.LogLevelDebug, database.LogLevelError:
168+
case database.LogLevelTrace, database.LogLevelDebug:
169+
if !opts.Verbose {
170+
continue
171+
}
172+
output = Styles.Placeholder.Render(log.Output)
173+
case database.LogLevelError:
153174
output = defaultStyles.Error.Render(log.Output)
154175
case database.LogLevelWarn:
155176
output = Styles.Warn.Render(log.Output)
156177
case database.LogLevelInfo:
157178
output = log.Output
158179
}
180+
jobMutex.Lock()
159181
if log.Stage != currentStage && log.Stage != "" {
160-
jobMutex.Lock()
161182
updateStage(log.Stage, log.CreatedAt)
162183
jobMutex.Unlock()
163184
continue
164185
}
165186
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s %s\n", Styles.Placeholder.Render(" "), output)
166187
didLogBetweenStage = true
188+
jobMutex.Unlock()
167189
}
168190
}
169191
}

cli/cliui/provisionerjob_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func TestProvisionerJob(t *testing.T) {
3636
test.Job.Status = codersdk.ProvisionerJobSucceeded
3737
now = database.Now()
3838
test.Job.CompletedAt = &now
39-
test.JobMutex.Unlock()
4039
close(test.Logs)
40+
test.JobMutex.Unlock()
4141
}()
4242
test.PTY.ExpectMatch("Queued")
4343
test.Next <- struct{}{}
@@ -67,8 +67,8 @@ func TestProvisionerJob(t *testing.T) {
6767
test.Job.Status = codersdk.ProvisionerJobSucceeded
6868
now = database.Now()
6969
test.Job.CompletedAt = &now
70-
test.JobMutex.Unlock()
7170
close(test.Logs)
71+
test.JobMutex.Unlock()
7272
}()
7373
test.PTY.ExpectMatch("Queued")
7474
test.Next <- struct{}{}
@@ -79,7 +79,7 @@ func TestProvisionerJob(t *testing.T) {
7979
})
8080

8181
// This cannot be ran in parallel because it uses a signal.
82-
//nolint:paralleltest
82+
// nolint:paralleltest
8383
t.Run("Cancel", func(t *testing.T) {
8484
if runtime.GOOS == "windows" {
8585
// Sending interrupt signal isn't supported on Windows!
@@ -98,8 +98,8 @@ func TestProvisionerJob(t *testing.T) {
9898
test.Job.Status = codersdk.ProvisionerJobCanceled
9999
now := database.Now()
100100
test.Job.CompletedAt = &now
101-
test.JobMutex.Unlock()
102101
close(test.Logs)
102+
test.JobMutex.Unlock()
103103
}()
104104
test.PTY.ExpectMatch("Queued")
105105
test.Next <- struct{}{}

cmd/cliui/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func main() {
103103
go func() {
104104
defer close(logs)
105105
ticker := time.NewTicker(100 * time.Millisecond)
106+
defer ticker.Stop()
106107
count := 0
107108
for {
108109
select {

0 commit comments

Comments
 (0)