Skip to content

Commit

Permalink
return error when pipeline job fails with --follow
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsgstrabo committed Nov 29, 2023
1 parent b18133a commit 56c70af
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
49 changes: 33 additions & 16 deletions cmd/logsPipelineJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ import (
"github.com/spf13/cobra"
)

var completedJobStatuses = []string{"Succeeded", "Failed", "Stopped"}
const (
jobStatusRunning = "Running"
jobStatusFailed = "Failed"
jobStatusSucceeded = "Succeeded"
jobStatusStopped = "Stopped"

stepStatusWaiting = "Waiting"
)

var completedJobStatuses = []string{jobStatusSucceeded, jobStatusStopped, jobStatusFailed}

// logsJobCmd represents the logsJobCmd command
var logsJobCmd = &cobra.Command{
Expand Down Expand Up @@ -91,13 +100,16 @@ func getLogsJob(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, jobN
if respJob == nil {
continue
}
if slices.Contains(completedJobStatuses, respJob.Payload.Status) {
log.Print(cmd, "radix-cli", fmt.Sprintf("Job is completed with status %s\n", respJob.Payload.Status), log.Red)
return nil
if isCompletedJob(respJob.Payload.Status) {
return errorAndLogCompletedJob(respJob.Payload.Status, cmd)
}
loggedForJob := false

for i, step := range respJob.Payload.Steps {
if step.Status == stepStatusWaiting {
continue
}

// Sometimes, even though we get delta, the log is the same as previous
previousLogLines := previousLogForStep[step.Name]
stepLogsParams := pipeline_job.NewGetPipelineJobStepLogsParams()
Expand All @@ -107,7 +119,7 @@ func getLogsJob(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, jobN

jobStepLog, err := apiClient.PipelineJob.GetPipelineJobStepLogs(stepLogsParams, nil)
if err != nil {
log.Print(cmd, "radix-cli", fmt.Sprintf("Failed to jet pipeline job logs. %v", err), log.Red)
log.Print(cmd, "radix-cli", fmt.Sprintf("Failed to get pipeline job logs. %v", err), log.Red)
break
}
logLines := strings.Split(strings.Replace(jobStepLog.Payload, "\r\n", "\n", -1), "\n")
Expand All @@ -131,17 +143,8 @@ func getLogsJob(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, jobN
continue
}
jobSummary := respJob.Payload
if jobSummary.Status == "Succeeded" {
log.Print(cmd, "radix-cli", "Job complete", log.Green)
return nil
}
if jobSummary.Status == "Failed" {
log.Print(cmd, "radix-cli", "Job failed", log.Red)
return nil
}
if jobSummary.Status == "Stopped" {
log.Print(cmd, "radix-cli", "Job stopped", log.Red)
return nil
if isCompletedJob(jobSummary.Status) {
return errorAndLogCompletedJob(jobSummary.Status, cmd)
}
if jobSummary.Status == "Running" {
// Reset timeout
Expand All @@ -160,6 +163,20 @@ func getLogsJob(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, jobN
}
}

func isCompletedJob(status string) bool {
return slices.Contains(completedJobStatuses, status)
}

func errorAndLogCompletedJob(status string, cmd *cobra.Command) error {
msg := fmt.Sprintf("job completed with status %s", status)
if status == jobStatusFailed {
fmt.Fprintln(cmd.OutOrStdout())
return errors.New(msg)
}
log.Print(cmd, "radix-cli", msg, log.Red)
return nil
}

func init() {
logsCmd.AddCommand(logsJobCmd)

Expand Down
10 changes: 5 additions & 5 deletions pkg/utils/log/log_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
)

var (
Yellow = color.New(color.FgHiYellow, color.BgBlack, color.Bold).SprintFunc()
Green = color.New(color.FgHiGreen, color.BgBlack, color.Bold).SprintFunc()
Blue = color.New(color.FgHiBlue, color.BgBlack, color.Underline).SprintFunc()
Yellow = color.New(color.FgHiYellow, color.BgBlack).SprintFunc()
Green = color.New(color.FgHiGreen, color.BgBlack).SprintFunc()
Blue = color.New(color.FgHiBlue, color.BgBlack).SprintFunc()
Cyan = color.New(color.FgCyan, color.BgBlack).SprintFunc()
Red = color.New(color.FgHiRed, color.BgBlack).Add(color.Italic).SprintFunc()
Magenta = color.New(color.FgHiMagenta, color.BgBlack).Add(color.Italic).SprintFunc()
Red = color.New(color.FgHiRed, color.BgBlack).SprintFunc()
Magenta = color.New(color.FgHiMagenta, color.BgBlack).SprintFunc()

Colors = []func(a ...interface{}) string{Yellow, Green, Blue, Cyan, Red, Magenta}
)
Expand Down

0 comments on commit 56c70af

Please sign in to comment.