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

     1  package exec
     2  
     3  import (
     4  	"context"
     5  )
     6  
     7  // OnFailureStep will run one step, and then a second step if the first step
     8  // fails (but not errors).
     9  type OnFailureStep struct {
    10  	step Step
    11  	hook Step
    12  }
    13  
    14  // OnFailure constructs an OnFailureStep factory.
    15  func OnFailure(firstStep Step, secondStep Step) OnFailureStep {
    16  	return OnFailureStep{
    17  		step: firstStep,
    18  		hook: secondStep,
    19  	}
    20  }
    21  
    22  // Run will call Run on the first step and wait for it to complete. If the
    23  // first step errors, Run returns the error. OnFailureStep is ready as soon as
    24  // the first step is ready.
    25  //
    26  // If the first step fails (that is, its Success result is false), the second
    27  // step is executed. If the second step errors, its error is returned.
    28  func (o OnFailureStep) Run(ctx context.Context, state RunState) (bool, error) {
    29  	ok, err := o.step.Run(ctx, state)
    30  	if err != nil {
    31  		return false, err
    32  	}
    33  
    34  	if !ok {
    35  		_, err := o.hook.Run(ctx, state)
    36  		if err != nil {
    37  			return false, err
    38  		}
    39  	}
    40  
    41  	return ok, nil
    42  }