diff --git a/cd/manager/common/job/models.go b/cd/manager/common/job/models.go index 1ab2136..c55f5ba 100644 --- a/cd/manager/common/job/models.go +++ b/cd/manager/common/job/models.go @@ -32,10 +32,11 @@ const ( ) const ( - JobParam_Id string = "id" - JobParam_Error string = "error" - JobParam_Start string = "start" - JobParam_Source string = "source" + JobParam_Id string = "id" + JobParam_Error string = "error" + JobParam_Elapsed string = "elapsed" + JobParam_Start string = "start" + JobParam_Source string = "source" ) const ( diff --git a/cd/manager/notifs/discord.go b/cd/manager/notifs/discord.go index 9fbf7c4..c6c19b8 100644 --- a/cd/manager/notifs/discord.go +++ b/cd/manager/notifs/discord.go @@ -31,6 +31,7 @@ const ( notifField_References string = "References" notifField_JobId string = "Job ID" notifField_RunTime string = "Time Running" + notifField_WaitTime string = "Time Waiting" notifField_Deploy string = "Deployment(s)" notifField_Anchor string = "Anchor Worker(s)" notifField_TestE2E string = "E2E Tests" @@ -151,15 +152,25 @@ func (n JobNotifs) getNotifFields(jobState job.JobState) []discord.EmbedField { Value: deployTags, }) } + // If the job just started, also add the wait time. + if jobState.Stage == job.JobStage_Started { + if elapsedTime, found := jobState.Params[job.JobParam_Elapsed].(string); found { + parsedElapsedTime, _ := time.ParseDuration(elapsedTime) + waitTime := formattedDuration(parsedElapsedTime) + if len(waitTime) > 0 { + fields = append(fields, discord.EmbedField{ + Name: notifField_WaitTime, + Value: waitTime, + }) + } + } + } if startTime, found := jobState.Params[job.JobParam_Start].(float64); found { - runTime := time.Since(time.Unix(0, int64(startTime))) - hours := int(runTime.Seconds() / 3600) - minutes := int(runTime.Seconds()/60) % 60 - seconds := int(runTime.Seconds()) % 60 - if (hours != 0) || (minutes != 0) || (seconds != 0) { + runTime := formattedDuration(time.Since(time.Unix(0, int64(startTime)))) + if len(runTime) > 0 { fields = append(fields, discord.EmbedField{ Name: notifField_RunTime, - Value: fmt.Sprintf("%dh %dm %ds", hours, minutes, seconds), + Value: runTime, }) } } @@ -295,3 +306,13 @@ func colorForStage(jobStage job.JobStage) discordColor { return discordColor_Alert } } + +func formattedDuration(duration time.Duration) string { + hours := int(duration.Seconds() / 3600) + minutes := int(duration.Seconds()/60) % 60 + seconds := int(duration.Seconds()) % 60 + if (hours != 0) || (minutes != 0) || (seconds != 0) { + return fmt.Sprintf("%dh %dm %ds", hours, minutes, seconds) + } + return "" +} diff --git a/cd/manager/utils.go b/cd/manager/utils.go index cc3c133..4feea87 100644 --- a/cd/manager/utils.go +++ b/cd/manager/utils.go @@ -58,6 +58,8 @@ func IsV5WorkerJob(jobState job.JobState) bool { // AdvanceJob will move a JobState to a new JobStage in the Database and send an appropriate notification func AdvanceJob(jobState job.JobState, jobStage job.JobStage, ts time.Time, err error, db Database, notifs Notifs) (job.JobState, error) { jobState.Stage = jobStage + // Store how much time has elapsed since the stage last changed for this job + jobState.Params[job.JobParam_Elapsed] = time.Since(jobState.Ts).String() jobState.Ts = ts if err != nil { jobState.Params[job.JobParam_Error] = err.Error()