Skip to content

Commit

Permalink
return error if pipeline job fails when using --follow (#78)
Browse files Browse the repository at this point in the history
* return error when  pipeline job fails with --follow

* rename func

* change info message logging
  • Loading branch information
nilsgstrabo authored Nov 29, 2023
1 parent b18133a commit 04b58c4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
54 changes: 36 additions & 18 deletions cmd/logsPipelineJob.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,20 @@ import (
"github.com/equinor/radix-cli/pkg/client"
"github.com/equinor/radix-cli/pkg/settings"
"github.com/equinor/radix-cli/pkg/utils/log"
"github.com/sirupsen/logrus"
"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 +101,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 errorAndLogJobStatus(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 +120,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)
logrus.Infof("Failed to get pipeline job logs. %v", err)
break
}
logLines := strings.Split(strings.Replace(jobStepLog.Payload, "\r\n", "\n", -1), "\n")
Expand All @@ -131,17 +144,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 errorAndLogJobStatus(jobSummary.Status, cmd)
}
if jobSummary.Status == "Running" {
// Reset timeout
Expand All @@ -151,15 +155,29 @@ func getLogsJob(cmd *cobra.Command, apiClient *apiclient.Radixapi, appName, jobN
getLogAttempts--
if getLogAttempts > 0 {
getLogAwaitingTime := int(time.Since(getLogStartTime))
log.Print(cmd, "radix-cli", fmt.Sprintf("Nothing logged the last %d seconds. Job summary: %v. Status: %s. Contihue waiting", getLogAwaitingTime, jobSummary, jobSummary.Status), log.GetColor(0))
logrus.Infof("Nothing logged the last %d seconds. Job summary: %v. Status: %s. Contihue waiting", getLogAwaitingTime, jobSummary, jobSummary.Status)
break
}
log.Print(cmd, "radix-cli", fmt.Sprintf("Nothing logged the last %s. Job summary: %v. Status: %s. Timeout", settings.DeltaTimeout, jobSummary, jobSummary.Status), log.GetColor(0))
logrus.Infof("Nothing logged the last %s. Job summary: %v. Status: %s. Timeout", settings.DeltaTimeout, jobSummary, jobSummary.Status)
return nil
}
}
}

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

func errorAndLogJobStatus(status string, cmd *cobra.Command) error {
fmt.Fprintln(cmd.OutOrStdout())
msg := fmt.Sprintf("job is completed with status %s", status)
if status == jobStatusFailed {
return errors.New(msg)
}
logrus.Info(msg)
return nil
}

func init() {
logsCmd.AddCommand(logsJobCmd)

Expand Down
16 changes: 8 additions & 8 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 All @@ -28,7 +28,7 @@ func GetColor(num int) func(a ...interface{}) string {
func PrintLines(cmd *cobra.Command, name string, previousLogLines, logLines []string, color func(a ...interface{}) string) {
for _, logLine := range logLines {
if !logged(logLine, previousLogLines) {
Print(cmd, name, logLine, color)
print(cmd, name, logLine, color)
}
}
}
Expand All @@ -42,7 +42,7 @@ func logged(logLine string, previousLogLines []string) bool {
return false
}

// Print Output string to standard output
func Print(cmd *cobra.Command, name, logLine string, color func(a ...interface{}) string) {
// print Output string to standard output
func print(cmd *cobra.Command, name, logLine string, color func(a ...interface{}) string) {
fmt.Fprintf(cmd.OutOrStdout(), "\r\n[%s] : %s", color(name), color(logLine))
}

0 comments on commit 04b58c4

Please sign in to comment.