Search code examples
gocadence-workflow

How to get `context.Context` from `workflow.Context`?


I'm implementing a Cadence Workflow that needs to call functions with context.Context parameters. How do I go about getting a context.Context from the workflow.Context? Is it just a matter of ctx.(*context.Context)?


Solution

  • It is not context.Context.

    You should never write any workflow code that uses context.Context at all. All the calls that needs context.Context should be written within workflow activity or local activity for determinism.

    In other words, Workflow code should only contain logic to orchestrate/manage other workflow entities like activities/childWF/Signal/etc.

    workflow.Context is a special data structure for worker to pass in workflow run-time information during workflow execution. For example, workflowID and runID. It happens to call Context just because this looks very similar with Golang style. Other than that, it has nothing directly related to context.Context.

    In Java client, there is no workflow.Context and the way that worker pass through these data is via ThreadLocal.

    If you really want to pass through some KV data from external to workflow code, you can use context propagation: https://github.com/uber-common/cadence-samples/tree/master/cmd/samples/recipes/ctxpropagation