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

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