github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/compute/store/resolver/checker.go (about)

     1  package resolver
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/filecoin-project/bacalhau/pkg/compute/store"
     7  )
     8  
     9  type CheckStateFunction func(store.Execution) (bool, error)
    10  
    11  func CheckForTerminalStates() CheckStateFunction {
    12  	return func(execution store.Execution) (bool, error) {
    13  		if execution.State.IsTerminal() {
    14  			return true, nil
    15  		}
    16  		return false, nil
    17  	}
    18  }
    19  
    20  func CheckForState(expectedStates ...store.ExecutionState) CheckStateFunction {
    21  	return func(execution store.Execution) (bool, error) {
    22  		for _, expectedState := range expectedStates {
    23  			if execution.State == expectedState {
    24  				return true, nil
    25  			}
    26  		}
    27  		return false, nil
    28  	}
    29  }
    30  
    31  func CheckForUnexpectedState(expectedStates ...store.ExecutionState) CheckStateFunction {
    32  	return func(execution store.Execution) (bool, error) {
    33  		for _, expectedState := range expectedStates {
    34  			if execution.State == expectedState {
    35  				return false, fmt.Errorf("unexpected state: %s", execution.State)
    36  			}
    37  		}
    38  		return false, nil
    39  	}
    40  }
    41  
    42  func CheckCompleted() CheckStateFunction {
    43  	return CheckForState(store.ExecutionStateCompleted)
    44  }