How to terminate or cancel a unlimited-retry workflow elegantly

I have a workflow using java-sdk like this

public class MyWorkflowImpl implements MyWorkflow {  
    public void startMyWorkflowImpl() {  
        try {  
            // change workflow status to success in our own mysql db(not temporal cluster db)
        } catch (Exception e) {  
		    // change workflow status to failed in our own mysql db(not temporal cluster db)

I got a question about termination/cancellation about this workflow. If the activityA runs with unlimited retry, I want to terminate/cancel the workflow manually. The tricky thing is that after terminate/cancel the workflow, I still need to run activityB to change the workflow status in the db(business service db, not temporal cluster db).

I come up with 2 options to do so:

Option1: I will write a script to cancel the workflow. When workflow is canceled, the exception could be gotten by try…catch, then activityB() will run to change status to “failed”. The drawback of this option is that I need to change some code in workflow, like I need to add heart-beat in activityA(), and the feedback of this operation will not be so timely.

Option2: I will add an endpoint to terminate the workflow, the logic of the endpoint is like terminate workflow -> change status in db.

I’m a little confused about these 2 options, any help is much appreciable!

Termination for a workflow execution done on server only, so in this case there is no possibility for a “cleanup” (as your workers are no involved in terminating the execution).

Cancellation is a request. You can see some cancellation examples here and here. Yes currently activity itself is notified of cancellation through heartbeat.
If the workflow that invoked it is canceled and completes before the currently running activity completes/fails/times out, activity will fail when trying to report its completion/failure (so no more retries will be scheduled). Also note you can set different cancellation types for your activities, sample here.

Thanks for reply! seems send a cancel request to workflow makes more sense.

I still got some small questions about cancellation.

  • If I don’t set heartbeat in activityA(), does this mean activityA is not failed because of cancellation, but failed because of failed to report to workflow?
  • If activity does not cancelled, will workflow be able to be canceled? what’s the sequence of cancellation of workflow and its activity?
  • I have to run activityB("failed"); in a cancellation scope right?

Thanks for help!