github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/exec/retry_step.go (about)

     1  package exec
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  // RetryStep is a step that will run the steps in order until one of them
     8  // succeeds.
     9  type RetryStep struct {
    10  	Attempts    []Step
    11  	LastAttempt Step
    12  }
    13  
    14  func Retry(attempts ...Step) Step {
    15  	return &RetryStep{
    16  		Attempts: attempts,
    17  	}
    18  }
    19  
    20  // Run iterates through each step, stopping once a step succeeds. If all steps
    21  // fail, the RetryStep will fail.
    22  func (step *RetryStep) Run(ctx context.Context, state RunState) (bool, error) {
    23  	var attemptOk bool
    24  	var attemptErr error
    25  
    26  	for _, attempt := range step.Attempts {
    27  		step.LastAttempt = attempt
    28  
    29  		attemptOk, attemptErr = attempt.Run(ctx, state)
    30  		if ctx.Err() != nil {
    31  			return false, ctx.Err()
    32  		}
    33  
    34  		if attemptErr != nil {
    35  			continue
    36  		}
    37  
    38  		if attemptOk {
    39  			break
    40  		}
    41  	}
    42  
    43  	return attemptOk, attemptErr
    44  }