-
Notifications
You must be signed in to change notification settings - Fork 85
/
utils.go
47 lines (39 loc) · 1.51 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package store
import (
"context"
"github.com/rs/zerolog/log"
)
// GetActiveExecution returns the active execution for a given job.
// In case of a bug where we have more than a single active execution, the latest one is returned
func GetActiveExecution(ctx context.Context, s ExecutionStore, jobID string) (LocalExecutionState, error) {
executions, err := s.GetExecutions(ctx, jobID)
if err != nil {
return LocalExecutionState{}, err
}
var activeExecution LocalExecutionState
var activeExecutionsCount int
for _, execution := range executions {
if execution.State.IsActive() {
activeExecutionsCount++
if activeExecutionsCount != 1 || execution.UpdateTime.After(activeExecution.UpdateTime) {
activeExecution = execution
}
}
}
if activeExecutionsCount > 1 {
log.Ctx(ctx).Warn().Msgf(
"Found %d active executions for job %s. Selecting the latest one", activeExecutionsCount, jobID)
}
return activeExecution, nil
}
func ValidateNewExecution(localExecutionState LocalExecutionState) error {
// state must be either created, or bid accepted if the execution is pre-approved
if localExecutionState.State != ExecutionStateCreated && localExecutionState.State != ExecutionStateBidAccepted {
return NewErrInvalidExecutionState(
localExecutionState.Execution.ID, localExecutionState.State, ExecutionStateCreated, ExecutionStateBidAccepted)
}
if localExecutionState.Version != 1 {
return NewErrInvalidExecutionVersion(localExecutionState.Execution.ID, localExecutionState.Version, 1)
}
return nil
}