Retry Workflow Errors

When a workflow ends in Error state, you can programmatically retry the failed workflow.. Note that there are three types of K2 errors relating to workflows, and each type of error must be retried with a different method.

Error type Retry method
Plain (normal) RetryError
IPC RepairIPCError
IPC Return RepairIPCReturnError
Retrying multiple errors in a rapid iteration can cause performance issues on your K2 server, since this can be an expensive operation, depending on a nature of the error being retried. If you are writing code to retry multiple errors, you may want to retry errors in batches of 5-10 errors at a time to avoid bogging down your K2 server with too many retry requests at the same time.

The following code sample demonstrates how to programmatically retry a failed workflow.

This sample code requires references to the assemblies:

  • SourceCode.HostClientAPI
  • SourceCode.Workflow.Management
  • System.Threading

Copy

Retry a failed workflow example

//TODO: build a connection string with the SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder
SourceCode.Hosting.Client.BaseAPI.SCConnectionStringBuilder connectionString = new SCConnectionStringBuilder();
connectionString.Host = "[k2servername]";
//alternatively build a simple connection
WorkflowManagementServer workflowServer = new WorkflowManagementServer("localhost", 5555);

try {

    workflowServer.Open();

    //first get the error profile ID. In this case, we will use the default "All" profile, your ID may be different
    int errorProfileId = workflowServer.GetErrorProfile("All").ID;
    ErrorLogs K2Errors = workflowServer.GetErrorLogs(errorProfileId); //you can also construct a criteria filter to filter the error profile further.

    //iterate over each error in the log
    foreach(ErrorLog K2Error in K2Errors) {
        //Do something with the error log entry
        Console.WriteLine(K2Error.Description);
        //to repair a general (non-IPC) error:
        workflowServer.RetryError(K2Error.ProcInstID, K2Error.ID, Thread.CurrentPrincipal.Identity.ToString());
        //repair an error by retrying it against a different version of the workflow:
        int newVersionId = 10; //get the ID of the workflow version to retry against
        workflowServer.RetryError(K2Error.ProcInstID, K2Error.ID, newVersionId, Thread.CurrentPrincipal.Identity.ToString());

        //to repair an IPC error, determine the error Type and call the appropriate method, e.g.
        if (K2Error.TypeID == 20) {
            K2Error.LoadIPCErrorDetails(workflowServer);
            workflowServer.RepairIPCError(K2Error.ID, K2Error.IPCErrorDetails.Server, K2Error.IPCErrorDetails.Port, K2Error.IPCErrorDetails.ConnectionString, K2Error.IPCErrorDetails.Process);
        }

    }
} catch(Exception ex) {
    //do something with the error
    throw ex;
} finally {
    //always close the connection
    workflowServer.Connection.Close();
}