-
Notifications
You must be signed in to change notification settings - Fork 902
feat: Add stage to build logs #577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
feat: Add stage to build logs
This adds a stage property to logs, and refactors the job logs cliui. It also adds tests to the cliui for build logs!
- Loading branch information
commit 048f29a8bb192a39d61dd805278cc30d8bd9b531
There are no files selected for viewing
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,169 @@ | ||
package cliui | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"time" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/xerrors" | ||
|
||
"github.com/coder/coder/coderd/database" | ||
"github.com/coder/coder/codersdk" | ||
) | ||
|
||
type ProvisionerJobOptions struct { | ||
Fetch func() (codersdk.ProvisionerJob, error) | ||
Cancel func() error | ||
Logs func() (<-chan codersdk.ProvisionerJobLog, error) | ||
|
||
FetchInterval time.Duration | ||
} | ||
|
||
// ProvisionerJob renders a provisioner job with interactive cancellation. | ||
func ProvisionerJob(cmd *cobra.Command, opts ProvisionerJobOptions) error { | ||
if opts.FetchInterval == 0 { | ||
opts.FetchInterval = time.Second | ||
} | ||
|
||
var ( | ||
currentStage = "Queued" | ||
currentStageStartedAt = time.Now().UTC() | ||
didLogBetweenStage = false | ||
ctx, cancelFunc = context.WithCancel(cmd.Context()) | ||
|
||
errChan = make(chan error) | ||
job codersdk.ProvisionerJob | ||
jobMutex sync.Mutex | ||
) | ||
defer cancelFunc() | ||
|
||
printStage := func() { | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), Styles.Prompt.Render("⧗")+"%s\n", Styles.Field.Render(currentStage)) | ||
} | ||
printStage() | ||
|
||
updateStage := func(stage string, startedAt time.Time) { | ||
if currentStage != "" { | ||
prefix := "" | ||
if !didLogBetweenStage { | ||
prefix = "\033[1A\r" | ||
} | ||
mark := Styles.Checkmark | ||
if job.CompletedAt != nil && job.Status != codersdk.ProvisionerJobSucceeded { | ||
mark = Styles.Crossmark | ||
} | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), prefix+mark.String()+Styles.Placeholder.Render(" %s [%dms]")+"\n", currentStage, startedAt.Sub(currentStageStartedAt).Milliseconds()) | ||
} | ||
if stage == "" { | ||
return | ||
} | ||
currentStage = stage | ||
currentStageStartedAt = startedAt | ||
didLogBetweenStage = false | ||
printStage() | ||
} | ||
|
||
updateJob := func() { | ||
var err error | ||
jobMutex.Lock() | ||
defer jobMutex.Unlock() | ||
job, err = opts.Fetch() | ||
if err != nil { | ||
errChan <- xerrors.Errorf("fetch: %w", err) | ||
return | ||
} | ||
if job.StartedAt == nil { | ||
return | ||
} | ||
if currentStage != "Queued" { | ||
// If another stage is already running, there's no need | ||
// for us to notify the user we're running! | ||
return | ||
} | ||
updateStage("Running", *job.StartedAt) | ||
} | ||
updateJob() | ||
|
||
// Handles ctrl+c to cancel a job. | ||
stopChan := make(chan os.Signal, 1) | ||
defer signal.Stop(stopChan) | ||
go func() { | ||
signal.Notify(stopChan, os.Interrupt) | ||
select { | ||
case <-ctx.Done(): | ||
return | ||
case _, ok := <-stopChan: | ||
if !ok { | ||
return | ||
} | ||
} | ||
// Stop listening for signals so another one kills it! | ||
signal.Stop(stopChan) | ||
_, _ = 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") | ||
err := opts.Cancel() | ||
if err != nil { | ||
errChan <- xerrors.Errorf("cancel: %w", err) | ||
return | ||
} | ||
updateJob() | ||
}() | ||
|
||
logs, err := opts.Logs() | ||
if err != nil { | ||
return xerrors.Errorf("logs: %w", err) | ||
} | ||
|
||
ticker := time.NewTicker(opts.FetchInterval) | ||
kylecarbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for { | ||
select { | ||
case err = <-errChan: | ||
return err | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-ticker.C: | ||
updateJob() | ||
case log, ok := <-logs: | ||
if !ok { | ||
// The logs stream will end when the job does, | ||
// so it's safe to | ||
kylecarbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
updateJob() | ||
jobMutex.Lock() | ||
if job.CompletedAt != nil { | ||
updateStage("", *job.CompletedAt) | ||
} | ||
switch job.Status { | ||
case codersdk.ProvisionerJobCanceled: | ||
jobMutex.Unlock() | ||
return Canceled | ||
case codersdk.ProvisionerJobSucceeded: | ||
jobMutex.Unlock() | ||
return nil | ||
case codersdk.ProvisionerJobFailed: | ||
} | ||
jobMutex.Unlock() | ||
return xerrors.New(job.Error) | ||
} | ||
output := "" | ||
switch log.Level { | ||
case database.LogLevelTrace, database.LogLevelDebug, database.LogLevelError: | ||
output = defaultStyles.Error.Render(log.Output) | ||
kylecarbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case database.LogLevelWarn: | ||
output = Styles.Warn.Render(log.Output) | ||
case database.LogLevelInfo: | ||
output = log.Output | ||
} | ||
if log.Stage != currentStage && log.Stage != "" { | ||
jobMutex.Lock() | ||
updateStage(log.Stage, log.CreatedAt) | ||
jobMutex.Unlock() | ||
continue | ||
} | ||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s %s\n", Styles.Placeholder.Render(" "), output) | ||
didLogBetweenStage = true | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.