How to synchronize activity to execute in order

Hello. I’m new to temporal and I writing my first application using .NET SDK

My dotnet service starts hosted temporal worker and also starts all workflows.

builder.Services.AddHostedTemporalWorker("my-queue")
    .AddScopedActivities<SetStateWorkflow.Activities>()
    .AddWorkflow<SetStateWorkflow>()

I need to synchronize execute of certain activity for each object. The object has id, let’s name it ObjectId. I’m aware that Temporal itself will not keep order of execution basing on start time. My solution is:

I have a queue for each ObjectId

  1. At workflow run I add workflow handle to a queue
  2. At activity execution I check if the current workflow is at the first element of queue, if not it waits for all preceding workflows

In theory it is a good solution, but yet I’m facing some serious issues

When service starts I need a list of running workflows along with associated ObjectId before worker starts and run activities. To get ObjectId of a workflow I use query which gives me a timeout.

I realized that probably I need worker running to execute a query so pulling workflows is not blocking application running (with app.Run()).
Now query works, but other issue came up. Now activities are executed which needs that queue, which isn’t filled up yet. I could add to activity an await for that task to be completed. But this is only startup scenario and I would check for every activity for service lifetime. I would rather prefer put worker on hold and do not start activities until the task is finished and I call to resume.

My question is: can I not start a worker and in meantime execute queries? Or maybe deffer activities execution? Or maybe there is other, better approach to this?

You should use a workflow itself to synchronize activities.

This may make more sense as a “workflow for each ObjectId”. Do not rely on local-worker-machine behaviors in Temporal since it is a distributed system and can run the same workflow or activity on multiple machines.

Thank you for your reply.

If I have two workflows, how to synchronize theirs activities using workflow iteslf?

No. I have object which changes states. These state changes must be processed in order they came. At least for single object. State changes of different objects can be processed simultaneously

I’m aware of it. We planning to use one queue per service instance which each would be responsible for a set of objects. It’s not perfect, but would do.

There are a few ways. What many do is only have the activities executed through a common workflow that can synchronize work. Another way is to use something like samples-dotnet/src/Mutex at main · temporalio/samples-dotnet · GitHub.

This is what workflows are great for - synchronizing changes coming from several places. A workflow representing an entity can make sure that things happen in a reliable, deterministic order. It is very normal for a workflow to effectively queue and process changes (or do other validation needed).