Sharing values between workflow and activities

Hey there,
I have a workflow with multiple activities. I want to update the context of the workflow with some values (flags) in the first activity and I want to access these flags in the subsequent activities in the same workflow without passing them as parameters. The values fetched should be local to the workflow run, coz different workflow runs of the same workflow type will have different flag values.

Could you please suggest a way forward for me? Thanks.

If the activity depends on these flags, why do you not want to pass them to the activity?

Just to add, one thing to look into into could be sessions api with Go SDK to assure that multiple activities are executed on the same worker process / host, see file processing sample .

This would allow you for example to have one activity write a result to file system / local store / memory of that worker host and know it will be accessible to the rest of activities that run on the same host, just idea.

Thanks dor the replies.
The reason why I don’t want to pass them to the activities is that it’s messy. There’s a lot of business logic code and I want to try and separate this.
I was trying to add the flags to the workflow context and access them in the activity context. No success.
I’ll try the file sharing method that you have suggested.

You can implement your own ContextPropagator to propagate any needed values from workflow to activity context.

Thanks for the response mate. My question is would updating the context in one workflow affect the values in other contexts? In the examples I referred for context propagators, values are passed from the worker to workflow to activity via context propagators. What I want is an activity to fetch the flags and other activities to use the flags within the same workflow.

You can implement a context propagator to propagate value of “flags” key. Then the first activity would load the flags and then call context.WithValue(ctx, "flags", flags). After that, all activities are going to have this value propagated to their contexts.

Thank you very much for the explanation. I’ll do this. :relaxed:

1 Like

Is it available for java sdk ?

Hi @atullimaye19

it is available through org.slf4j.MDC , see this test code sdk-java/ContextPropagationTest.java at master · temporalio/sdk-java · GitHub

@maxim /@antonio.perez can the same context thing could be achieved in typescript SDK, if yes how can we do it ?

Hello @A_V ,

please see Logging and Sinks in TypeScript SDK | Legacy documentation for Temporal SDKs

Let me know if it helps.

Hi bro, I have a workflow with multiple activities, all activities need check a common data struct, How can I store the common data struct, all activities can get the common data struct

Make these activities methods of a struct. See the greetings sample.


this must be create new worker ,but I want to exec activity in workflow definition at the time, every time to exec workflow, the common data is not same

Temporal activities are like serivces. They have an API. Why do you need a global common structure shared by all of them?

This depend on our business scenario, In our business process that all activities need to use the global common structure to calculate result, but every time we execute a workflow use a different value of global common structure. so I need a method to store the different value of global common structure to share by all of activities at every execution of the workflow .Can you give me some methods? thanks

You pass this structure to every activity invocation and keep it inside the workflow for persistence.

Another approach is to cache this structure in memory of a worker process and route all the workflow activities to that specific process. If the process dies, then the whole sequence of activities needs to be executed in a different process.

i think this design is not appropriate, you can build a connection between workflow.Context and context.Context, that is to say ,the data that set in workflow.Context can be acquire by context.Context in activity.
the another way is to set common data before the execution of activity, For example below

a := &Activities{Data:data}
workflow.ExecuteActivity(ctx, a.Activity1)

You are still proposing passing this data to activity. But instead of having an explicit interface you pass it implicitly. I don’t understand why you are against cleanly defined interfaces and what to pass hidden arguments.

a := &Activities{Data:data}
workflow.ExecuteActivity(ctx, a.Activity1)

This is not supported.