public boolean tryTerminateQueuedInstance(
      WorkflowInstance instance, WorkflowInstance.Status status, String reason) {
    return withMetricLogError(
        () ->
            withRetryableTransaction(
                conn -> {
                  int res = terminateQueuedInstance(conn, instance, status, reason);
                  if (res == SUCCESS_WRITE_SIZE) {
                    publisher.publishOrThrow(
                        WorkflowInstanceUpdateJobEvent.create(
                            instance, status, System.currentTimeMillis()),
                        "Failed sending job events when terminating queued instance");
                    return true;
                  }
                  return false;
                }),
        "tryTerminateQueuedInstance",
        "Failed to terminate the queued workflow instance {}",
        instance.getIdentity());
  }
@Test
  public void testTryTerminateQueuedInstance() {
    boolean res =
        instanceDao.tryTerminateQueuedInstance(wfi, WorkflowInstance.Status.STOPPED, "test-reason");
    assertTrue(res);
    verify(publisher, times(1)).publishOrThrow(any(), any());
    WorkflowInstance updated =
        instanceDao.getLatestWorkflowInstanceRun(wfi.getWorkflowId(), wfi.getWorkflowInstanceId());
    assertEquals(WorkflowInstance.Status.STOPPED, updated.getStatus());
    assertEquals(
        "Workflow instance status becomes [STOPPED] due to reason [test-reason]",
        updated.getTimeline().getTimelineEvents().get(0).getMessage());
    assertNotNull(updated.getEndTime());
    assertNotNull(updated.getModifyTime());
  }