Terminate workflows with an API call

Hi,

Is there an API that I can use to terminate selected or all workflows?

TIA :slight_smile:

1 Like

We do support this:

CLI reference

Here is the API definition for GO SDK

2 Likes

@ryland Thanks for sharing this. I see workflow-id is mentioned as required in your code, just curious to know if there’s a way to terminate all workflows at namespace level.

1 Like

Unfortunately there is not an explicit API for batch termination of workflows in general or at a namespace level. We do support batch termination (not for entire namespace directly) via the CLI, but this is actually just implemented as a workflow which individually terminates the ones you specify. I made sure to note this usage so we can track it and potentially support it in the future.

The batch terminate @ryland mentioned can be used to terminate all the workflows in the namespace.
Note that it requires ElasticSearch integration enabled.

Got you. Thank you both!

Hi

Is there a java client API to terminate or cancel a workflow?

WorkflowStub workflow = workflowClient.newUntypedWorkflowStub(workflowId, ...)

workflow.cancel();
...
workflow.terminate("test");
1 Like

Thanks Maxim, is there a reason why i do not see the cancel or terminate methods on a typed workflow stub?

Because it is your type and we cannot control methods on it. But you can always convert typed to untyped:

    MyWorkflow client =
        workflowClient.newWorkflowStub(MyWorkflow.class, options);
    WorkflowStub stub = WorkflowStub.fromTyped(client);
1 Like

Hi @maxim Sorry for the newbie followup question. If there is a a typed workflow (also it is allow_duplicate workflow), how we usually terminate it? Will it automatically be terminated when it executes the last line of workflowMethod or we need to explicitly terminate it? thanks!

Terminate is an external command like kill -9 in Linux. If workflow wants to complete it just completes by returning (or throwing an exception) from its workflow method.

2 Likes

Got it. Thanks Maxim!

Hii @maxim
I have two api’s. One of which is starting a workflow and second one wants to terminate the workflow based on the workflowId returned by the first api.
Can you please help me in that because I am stuck on this for days.

See moneytransfer example to see how to start a workflow.

To terminate workflow by id:

    WorkflowStub wfs =
        workflowClient.newUntypedWorkflowStub(
            "WorkflowId", Optional.empty(), Optional.of("MyWorkflowType"));

    wfs.terminate("Termination Reason");

Thank you very much @maxim

The same definition workflow can be started multiple times, each workflow instance of the definition will have its own workflowId & runId.

  • Individual workflow instance can be terminated by workflowId
  • Similarly the worflowExecution state can be listed by workflowId

I’d like to know the above understanding is correct.

  • Individual workflow instance can be terminated by workflowId

Yes, sample shown by Maxim in this thread.

  • Similarly the worflowExecution state can be listed by workflowId

do you mean status? If so yes, you could do for example:

WorkflowExecution execution =
        client.newUntypedWorkflowStub("myWorkflowId", Optional.empty(), Optional.empty()).getExecution();

and then use DescribeWorkflowExecutionRequest to get the status, here is sample method that takes in WorkflowExecution: https://github.com/temporalio/samples-java/blob/8d5a80b7678a25dda26e1e3c6dbed0944688420e/src/main/java/io/temporal/samples/asyncchild/Starter.java#L74

each workflow instance of the definition will have its own workflowId & runId .

Note that workflow id is a business-level id so it can be explicitly set. You cannot have more than 1 workflow execution in running state with the same workflow id, and can use setWorkflowIdReusePolicy on Workflow and ChildWorkflow options to set how specifically it should behave. If you don’t specify the workflow id when you start the workflow, it should be set to a random unique value.

Thank you.
if I start workflow with specified workflowId and schedule (cron), how does the workflowID and runID will looks like on each run on the schedule?

The workflowID remains the same for each cron execution. Runid is different as each cron execution starts a new workflow invocation. The status of the workflow when its triggered has the “Running” status, the ones already triggered have the “Continuedasnew” status.