go.temporal.io/server@v1.23.0/common/persistence/workflow_state_status_validator.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package persistence
    26  
    27  import (
    28  	"fmt"
    29  
    30  	enumspb "go.temporal.io/api/enums/v1"
    31  	"go.temporal.io/api/serviceerror"
    32  
    33  	enumsspb "go.temporal.io/server/api/enums/v1"
    34  )
    35  
    36  var (
    37  	validWorkflowStates = map[enumsspb.WorkflowExecutionState]struct{}{
    38  		enumsspb.WORKFLOW_EXECUTION_STATE_CREATED:   {},
    39  		enumsspb.WORKFLOW_EXECUTION_STATE_RUNNING:   {},
    40  		enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED: {},
    41  		enumsspb.WORKFLOW_EXECUTION_STATE_ZOMBIE:    {},
    42  		enumsspb.WORKFLOW_EXECUTION_STATE_CORRUPTED: {},
    43  	}
    44  
    45  	validWorkflowStatuses = map[enumspb.WorkflowExecutionStatus]struct{}{
    46  		enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING:          {},
    47  		enumspb.WORKFLOW_EXECUTION_STATUS_COMPLETED:        {},
    48  		enumspb.WORKFLOW_EXECUTION_STATUS_FAILED:           {},
    49  		enumspb.WORKFLOW_EXECUTION_STATUS_CANCELED:         {},
    50  		enumspb.WORKFLOW_EXECUTION_STATUS_TERMINATED:       {},
    51  		enumspb.WORKFLOW_EXECUTION_STATUS_CONTINUED_AS_NEW: {},
    52  		enumspb.WORKFLOW_EXECUTION_STATUS_TIMED_OUT:        {},
    53  	}
    54  )
    55  
    56  // ValidateCreateWorkflowStateStatus validate workflow state and close status
    57  func ValidateCreateWorkflowStateStatus(
    58  	state enumsspb.WorkflowExecutionState,
    59  	status enumspb.WorkflowExecutionStatus,
    60  ) error {
    61  
    62  	if err := validateWorkflowState(state); err != nil {
    63  		return err
    64  	}
    65  	if err := validateWorkflowStatus(status); err != nil {
    66  		return err
    67  	}
    68  
    69  	// validate workflow state & status
    70  	if (state == enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED && status == enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING) ||
    71  		(state != enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED && status != enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING) {
    72  		return serviceerror.NewInternal(fmt.Sprintf("Create workflow with invalid state: %v or status: %v", state, status))
    73  	}
    74  	return nil
    75  }
    76  
    77  // ValidateUpdateWorkflowStateStatus validate workflow state and status
    78  func ValidateUpdateWorkflowStateStatus(
    79  	state enumsspb.WorkflowExecutionState,
    80  	status enumspb.WorkflowExecutionStatus,
    81  ) error {
    82  
    83  	if err := validateWorkflowState(state); err != nil {
    84  		return err
    85  	}
    86  	if err := validateWorkflowStatus(status); err != nil {
    87  		return err
    88  	}
    89  
    90  	// validate workflow state & status
    91  	if (state == enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED && status == enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING) ||
    92  		(state != enumsspb.WORKFLOW_EXECUTION_STATE_COMPLETED && status != enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING) {
    93  		return serviceerror.NewInternal(fmt.Sprintf("Update workflow with invalid state: %v or status: %v", state, status))
    94  	}
    95  	return nil
    96  }
    97  
    98  // validateWorkflowState validate workflow state
    99  func validateWorkflowState(
   100  	state enumsspb.WorkflowExecutionState,
   101  ) error {
   102  
   103  	if _, ok := validWorkflowStates[state]; !ok {
   104  		return serviceerror.NewInternal(fmt.Sprintf("Invalid workflow state: %v", state))
   105  	}
   106  
   107  	return nil
   108  }
   109  
   110  // validateWorkflowStatus validate workflow status
   111  func validateWorkflowStatus(
   112  	status enumspb.WorkflowExecutionStatus,
   113  ) error {
   114  
   115  	if _, ok := validWorkflowStatuses[status]; !ok {
   116  		return serviceerror.NewInternal(fmt.Sprintf("Invalid workflow status: %v", status))
   117  	}
   118  
   119  	return nil
   120  }