github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/state/forcedoperation.go (about)

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package state
     5  
     6  import "time"
     7  
     8  // ForcedOperation that allows accumulation of operational errors and
     9  // can be forced.
    10  type ForcedOperation struct {
    11  	// Force controls whether or not the removal of a unit
    12  	// will be forced, i.e. ignore operational errors.
    13  	Force bool
    14  
    15  	// Errors contains errors encountered while applying this operation.
    16  	// Generally, these are non-fatal errors that have been encountered
    17  	// during, say, force. They may not have prevented the operation from being
    18  	// aborted but the user might still want to know about them.
    19  	Errors []error
    20  
    21  	// MaxWait specifies the amount of time that each step in relation destroy process
    22  	// will wait before forcing the next step to kick-off. This parameter
    23  	// only makes sense in combination with 'force' set to 'true'.
    24  	MaxWait time.Duration
    25  }
    26  
    27  // AddError adds an error to the collection of errors for this operation.
    28  func (op *ForcedOperation) AddError(one ...error) {
    29  	op.Errors = append(op.Errors, one...)
    30  }
    31  
    32  // FatalError returns true if the err is not nil and Force is false.
    33  // If the error is not nil, it's added to the slice of errors for the
    34  // operation.
    35  func (op *ForcedOperation) FatalError(err error) bool {
    36  	if err != nil {
    37  		if !op.Force {
    38  			return true
    39  		}
    40  		op.Errors = append(op.Errors, err)
    41  	}
    42  	return false
    43  }
    44  
    45  // LastError returns last added error for this operation.
    46  func (op *ForcedOperation) LastError() error {
    47  	if len(op.Errors) == 0 {
    48  		return nil
    49  	}
    50  	return op.Errors[len(op.Errors)-1]
    51  }