I’ve been working on a PoC to see if Temporal can be used in our infrastructure using .NET 8.
I’ve been able to create a worker, workflow and activities, but seem to be getting stumped on triggering another workflow from the one I had just created. The other workflow exists on another service, which also has it’s own worker and activities.
How am I able to trigger that workflow to run using the output of the workflow on my service?
You can use a Temporal client like any other client inside an activity. Here is a sample that demonstrates a DB client used from an activity, but that can just as easily be a Temporal client.
You can also use child workflows if you need the parent/child relationship.
(I was typing when @Chad_Retz answered the question, I will post this in case it helps anyway )
Hi @OhMyDeera
The other workflow exists on another service, which also has it’s own worker and activities.
I assume this means that you have two different workers listening to two different task queues (and running in different services), and each one has its activities and workflows.
How am I able to trigger that workflow to run using the output of the workflow on my service?
- I think I would use a workflow to coordinate both as child workflows. The parent-workflow starts the child workflow that runs in the first service. You will have to specify the same taskqueue name as the worker running in the first service when you start the child workflow.
await Workflow.ExecuteChildWorkflowAsync(
(ChildWorkflow1 wf) => wf.RunAsync(),
new()
{
Id = "my-workflow-id",
TaskQueue = "tq-1"
});
and then, pass the result to the next workflow running in service 2.
await Workflow.ExecuteChildWorkflowAsync(
(ChildWorkflow2 wf) => wf.RunAsync(input),
new()
{
Id = "my-workflow-id-2",
TaskQueue = "tq-2"
})
-
Another option is to continue-as-new to a different workflowType and taskqueue. (you can set the taskqueue name in the ContinueAsNewOptions
.
-
and another one is to have an activity at the end of the first workflow that schedules the second workflow (before the first completes). You will need to inject the workflowClient into the activity, see this example : samples-dotnet/src/DependencyInjection at main · temporalio/samples-dotnet · GitHub
Antonio
1 Like
Thanks both for ansering so fast!
I tried triggering a child workflow from my first workflow, but got an error saying it wasn’t registered with my worker. That makes sense since the workflow is on a seperate service with it’s own worker, so how would I register it in my service on my worker?
Currently I do something like this:
services.AddHostedTemporalWorker(
clientTargetHost: "localhost:7233",
clientNamespace: "default",
taskQueue: "test-queue")
.AddScopedActivities<MyActivities>()
.AddWorkflow<MyWorkflow>();
And then from my workflow I do the following:
await Workflow.ExecuteChildWorkflowAsync("MySecondWorkflow", [ "Test Param" ]);
What needs to change for me to be able to call MySecondWorkflow
which exists on another service?
Thank you in advance!!
If you don’t specify the taskqueue the child workflow will be scheduled in the same taskqueue as the parent workflow, you can set a different taskqueue name in the childworkflow options.
What needs to change for me to be able to call MySecondWorkflow
which exists on another service?
await Workflow.ExecuteChildWorkflowAsync(
(ChildWorkflow2 wf) => wf.RunAsync(input),
new()
{
Id = "my-workflow-id-2",
TaskQueue = "tq-2"
})
how would I register it in my service on my worker?
to register several workflows to the same workers
Antonio
1 Like
So specifying the task queue means that it will look in that queue in temporal for a worker? I had the queues in both services set to the same name. Changing them to be different and specifying the queue seemed to do the trick.
temporalio/samples-dotnet/blob/f281d33286bee85bb4740eee701895056ab4f5c4/src/Polling/PeriodicSequence/Program.cs#L34-L35
Here it adds the Workflow to the service collection, but if the workflow doesn’t exist in the service collection, how would you add it? Would you register an interface to the service collection and then use that when calling the child service?
Hi @OhMyDeera
So specifying the task queue means that it will look in that queue in temporal for a worker?
Specifying the taskqueue will schedule the child workflow in the taskqueue that you specify, in the temporal server. If you don’t specify it the taskqueue name will be the same as the parent workflow.
Here it adds the Workflow to the service collection, but if the workflow doesn’t exist in the service collection, how would you add it? Would you register an interface to the service collection and then use that when calling the child service?
You register workflow implementations to the worker, so the worker knows the code it has to run when it pulls a workflowTask. If you have time, take a look at one of this courses that explain how it works Temporal 101: Introducing the Temporal Platform | Learn Temporal
Antonio