Search code examples
cadence-workflowtemporal-workflowuber-cadence

Iterative activity in Cadence / Temporal workflow


This can be considered a follow-up question to Invoking the same activity inside a loop in cadence workflow: How does the workflow recover in case of activity iteration ? Will is continue invoking the i-th activity (skipping the ones that were already invoked) or start again from 0 ? If so, how can the workflow be written so that activities that were invoked (0-'k') are skipped ?


Solution

  • First of all, it's important to understand the difference between workflow worker vs activity worker.

    • Workflow worker only processes your workflow code (not inside the activities) and stops at Cadence API calls to schedule an activity, to start a timer etc. Workflow workers receive "decision tasks" where they execute the workflow code from last point it paused till the next API call. When sticky workers are used, there will be a thread waiting on the last API call maintaining the local variables. If the worker didn't already have such thread, it will replay the workflow using the history first to understand where the workflow was supposed to be waiting.
    • Activity worker only executes "inside the activities" given activity parameters. Think of like a cloud function. Activity workers will receive activity tasks containing activity parameters, they will execute the activity and return its result. That's it.

    Coming back to your question: When your "workflow" worker crashes, restarts, lost or somehow a worker received a decision task that it doesn't know about its workflow; it will then request the history of that workflow from Cadence servers. It will replay the workflow until is consumes the whole history it receives then will schedule the next activity based on current local variables.

    So it will continue from the same loop iteration with the same local variables and parameters to the activity.

    Hope this answers your question