Restart workflow from the beginning

Hi,

I’m trying to provide an option to our users that should restart a workflow.

This workflow implements a process during which the user needs to enter various data points in different steps. The inputs trigger asynchronous processes that might take a while. Unfortunately the current reality is that a lot can go wrong in these asynchronous processes. In order to be able to deal with unforeseen errors we would like to provide the nuclear option of “discard everything and just try again from the start” to the user.

First I thought that resetWorkflowExecution might be the way, but then I realised that it replays the history, which I do not want. My goal is that the workflow is in the same state as it was when it was initially started.

My only other idea is to start a new workflow with the same initial input, cancel the current workflow and redirect the user to the new workflow (the workflow id is part of the URL). Is there a better way to do this?

2 Likes

My goal is that the workflow is in the same state as it was when it was initially started.

You could achieve this with reset, by resetting to the first WorkflowTaskScheduled, or WorkflowTaskStarted events in your workflow history. The initial workflow inputs are recorded in the very first WorkflowExecutionStarted event so they would be reapplied on the workflow execution created by reset.

Which SDK are you using? Maybe instead of the “nuclear” option you could send a cancellation client request for the execution which would allow your to handle the it gracefully (and run compensation activities if needed).
Some samples below:

Java: sample1, sample2
Go: sample
TS: sample

Thanks for your answer!

I tried resetting to the first WorkflowTaskStarted Event. But it seemed to me that somehow the signals sent to the original execution are replayed onto the restarted execution, which is not what I want. Maybe I’ll try this out again in a simpler POC.

The “cancellation” option to me was the “nuclear” option, as it requires me to manually cancel the current execution and handle the restarting and everything myself.

Hey! I’ve put together a POC project:

When I reset the workflow to the first WorkflowTaskScheduled it replays the previously sent signal onto the new workflow execution.

1 Like

But it seemed to me that somehow the signals sent to the original execution are replayed onto the restarted execution, which is not what I want.

ResetWorkflowExecutionRequest takes in a ResetReapplyType which you set to none, this should cause recorded signals not to be re-applied.

If you are resetting via tctl you can set it:

tctl wf reset -w <wid> -r <runid> ..... --reset_reapply_type None

Ahh nice, with that it works! Thank’s very much!

Could you maybe point me into the right direction of achieving this with the typescript SDK?

Figured out how to do it with the typescript SDK, posting it here for documentation purposes:

import { temporal } from '@temporalio/proto';
const resetWorkflowExecutionRequest = temporal.api.workflowservice.v1.ResetWorkflowExecutionRequest.create({
    workflowExecution: {
        workflowId: "workflow-id",
        runId: "run-id"
    },
    resetReapplyType: temporal.api.enums.v1.ResetReapplyType.RESET_REAPPLY_TYPE_NONE,
    workflowTaskFinishEventId: new Long(3) // The event id to reset to, can be found via connection.workflowService.getWorkflowExecutionHistory
    // ...rest of attributes as needed
})
await client.workflowService.resetWorkflowExecution(
    resetWorkflowExecutionRequest,
);
2 Likes

Hello,
Can anyone please help me to provide a solution in java to restart workflow from the beginning? Would be very much helpful for us.

Would it be safe to cancel the workflow within one of its activities? Or even from within its own workflow execution?

solution in java to restart workflow from the beginning

You should be able to use ResetWorkflowExecution and reset to first workflow task. Let us know where you are getting stuck in the impl.

Would it be safe to cancel the workflow within one of its activities? Or even from within its own workflow execution?

Hi do you mind opening a new thread for this question? Not sure its related to reset or am I wrong?

1 Like