How to reset Workflow In go? after a activity fail or timeout

func ResetWorkflow(ctx context.Context, workflowId string) (dbTaskID int64, merr *myerrors.CustomError) {
if TemporalClient == nil {
ClientInitOnce.Do(InitClient)
}

// 1. Findthe first failed or timeout activity
var lastWorkflowTaskCompletedID int64 = -1
iter := TemporalClient.GetWorkflowHistory(ctx, workflowId, "", false, enums.HISTORY_EVENT_FILTER_TYPE_ALL_EVENT)
for iter.HasNext() {
    event, err := iter.Next()
    if err != nil {
        merr := myerrors.NewCustomError(myerrors.ResetWorkflowError, fmt.Sprintf("iter Next 失败: %s", err.Error()))
        merr.OutputLog(ctx, resource.LoggerLogic)
        return 0, merr
    }

    //  i dont know how to do  but do this 
    if event.GetEventType() == enums.EVENT_TYPE_ACTIVITY_TASK_FAILED || event.GetEventType() == enums.EVENT_TYPE_ACTIVITY_TASK_TIMED_OUT {
        lastWorkflowTaskCompletedID = event.GetEventId() - 3
        break
    }
}

if lastWorkflowTaskCompletedID == -1 {
    merr := myerrors.NewCustomError(myerrors.ResetWorkflowError, fmt.Sprintf(" 没有找到ActivityTaskFailed事件"))
    merr.OutputLog(ctx, resource.LoggerLogic)
    return 0, merr
}
// 2. reset workflow
resetReq := &workflowservice.ResetWorkflowExecutionRequest{
    Namespace: resource.TemporalConf.Base.SOARNamespace,
    WorkflowExecution: &common.WorkflowExecution{
        WorkflowId: workflowId,
        RunId:      "", 
    },
    Reason:                    "kunkun要重试",
    WorkflowTaskFinishEventId: lastWorkflowTaskCompletedID, // reset  event id
}

_, err := TemporalClient.ResetWorkflowExecution(context.Background(), resetReq)
if err != nil {
    merr := myerrors.NewCustomError(myerrors.ResetWorkflowError, fmt.Sprintf("ResetWorkflowExecution 失败: %s", err.Error()))
    merr.OutputLog(ctx, resource.LoggerLogic)
    return 0, merr
}
dbTaskID, err = strconv.ParseInt(workflowId, 10, 64)
if err != nil {
    merr := myerrors.NewCustomError(myerrors.ErrParamParse, fmt.Sprintf("字符串转换失败: %s", err.Error()))
    merr.OutputLog(ctx, resource.LoggerLogic)
    return 0, merr
}
return dbTaskID, nil

}

![0d0ab1af430ef641b220c82f26850100|690x114](upload://bEZRnbYCjk9Ap9oVuqTnVFhCka2.png)
 i dont know how to do,pls help me

hi,

what is the error ?

I am not a Go expert but the approach is correct,

the only thing is that lastWorkflowTaskCompletedID should be the WorkflowTaskCompleted event that scheduled the failed activity task.

this will work if the activities are executed sequentially, if there are pending activities or running timers and the workflow is waiting for them to complete after the reset point, the new workflow run created after reset will hang indefinitely

Antonio