// Cancel paused run according to its branches state
  //      If the run has branches, (it can be branch-paused / user-paused / no-workers-in-group) -
  // then it's a 'virtual' pause, and we should cancel the paused branches. Then, the run itself
  // will be canceled as well.
  //      If it doesn't - just cancel it straight away - extract the Run Object, set its context
  // accordingly and put into the queue.
  private void cancelPausedRun(ExecutionState executionStateToCancel) {
    final List<ExecutionState> branches =
        executionStateService.readByExecutionId(executionStateToCancel.getExecutionId());

    // If the parent is paused because one of the branches is paused, OR, it was paused by the user
    // / no-workers-in-group, but has branches that were not finished (and thus, were paused) -
    // The parent itself will return to the queue after all the branches are ended (due to this
    // cancellation), and then it'll be canceled as well.
    if (branches.size()
        > 1) { // more than 1 means that it has paused branches (branches is at least 1 - the
               // parent)
      for (ExecutionState branch : branches) {
        if (!EMPTY_BRANCH.equals(branch.getBranchId())) { // exclude the base execution
          returnCanceledRunToQueue(branch);
          executionStateService.deleteExecutionState(branch.getExecutionId(), branch.getBranchId());
        }
      }
      executionStateToCancel.setStatus(
          ExecutionStatus
              .PENDING_CANCEL); // when the parent will return to queue - should have the correct
                                // status
    } else {
      returnCanceledRunToQueue(executionStateToCancel);
    }
  }
  @Test
  public void testFindExecutionIdByStatuses() {
    ExecutionState canceledExecutionState = createExecutionState(ExecutionStatus.CANCELED);
    ExecutionState completedExecutionState = createExecutionState(ExecutionStatus.COMPLETED);
    createExecutionState(ExecutionStatus.PENDING_CANCEL);

    List<Long> executionStates =
        executionStateRepository.findExecutionIdByStatuses(
            Arrays.asList(ExecutionStatus.CANCELED, ExecutionStatus.COMPLETED));

    assertThat(executionStates)
        .containsExactly(
            canceledExecutionState.getExecutionId(), completedExecutionState.getExecutionId());
  }
  private void returnCanceledRunToQueue(ExecutionState executionStateToCancel) {
    // set the context and return the run to the queue. It will be handled on "finishFlow"
    // (QueueEventListener).
    Execution executionObj =
        executionSerializationUtil.objFromBytes(executionStateToCancel.getExecutionObject());
    if (executionObj == null) {
      logger.error(
          "Run Object is null. Execution Id = "
              + executionStateToCancel.getExecutionId()
              + "; Branch Id = "
              + executionStateToCancel.getBranchId());
      return;
    }
    executionObj.getSystemContext().setFlowTerminationType(ExecutionStatus.CANCELED);
    executionObj.setPosition(null);

    // just in case - we shouldn't need it, because the Execution is back to the queue as
    // "Terminated"
    executionStateToCancel.setStatus(ExecutionStatus.PENDING_CANCEL);
    // clean the DB field
    executionStateToCancel.setExecutionObject(null);

    // return execution to queue, as "Terminated"
    queueDispatcherService.dispatch(
        String.valueOf(executionObj.getExecutionId()),
        executionObj.getGroupName(),
        ExecStatus.TERMINATED,
        executionMessageConverter.createPayload(executionObj));
  }