public int startWithRunStrategy(
      @NotNull WorkflowInstance instance, @NotNull RunStrategy runStrategy) {
    return withMetricLogError(
        () ->
            withRetryableTransaction(
                conn -> {
                  final long nextInstanceId =
                      getLatestInstanceId(conn, instance.getWorkflowId()) + 1;
                  if (isDuplicated(conn, instance)) {
                    return 0;
                  }
                  completeInstanceInit(conn, nextInstanceId, instance);
                  int res;
                  if (instance.getStatus().isTerminal()) {
                    // Save it directly and send a terminate event
                    res = addTerminatedInstance(conn, instance);
                  } else {
                    switch (runStrategy.getRule()) {
                      case SEQUENTIAL:
                      case PARALLEL:
                      case STRICT_SEQUENTIAL:
                        res = insertInstance(conn, instance, true, null);
                        break;
                      case FIRST_ONLY:
                        res = startFirstOnlyInstance(conn, instance);
                        break;
                      case LAST_ONLY:
                        res = startLastOnlyInstance(conn, instance);
                        break;
                      default:
                        throw new MaestroInternalError(
                            "When start, run strategy [%s] is not supported.", runStrategy);
                    }
                  }
                  if (instance.getWorkflowInstanceId() == nextInstanceId) {
                    updateLatestInstanceId(conn, instance.getWorkflowId(), nextInstanceId);
                  }
                  return res;
                }),
        "startWithRunStrategy",
        "Failed to start a workflow [{}][{}] with run strategy [{}]",
        instance.getWorkflowId(),
        instance.getWorkflowUuid(),
        runStrategy);
  }
@Test
  public void testStartWithRunStrategyForDeletedWorkflow() {
    MaestroTestHelper.removeWorkflow(dataSource, TEST_WORKFLOW_ID);
    wfi.setWorkflowInstanceId(0L);
    wfi.setWorkflowRunId(0L);
    wfi.setWorkflowUuid("test-uuid");
    AssertHelper.assertThrows(
        "cannot start a deleted workflow",
        MaestroNotFoundException.class,
        "Cannot find workflow [sample-dag-test-3]",
        () -> runStrategyDao.startWithRunStrategy(wfi, Defaults.DEFAULT_RUN_STRATEGY));
  }