Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workflow sample sub workflow #1209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions all.sln
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BulkPublishEventExample", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WorkflowUnitTest", "examples\Workflow\WorkflowUnitTest\WorkflowUnitTest.csproj", "{8CA09061-2BEF-4506-A763-07062D2BD6AC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WorkflowSubWorkflow", "examples\Workflow\WorkflowSubWorkflow\WorkflowSubWorkflow.csproj", "{310BD727-837D-47B2-83F2-C57E61B0DC36}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -248,6 +250,10 @@ Global
{DDC41278-FB60-403A-B969-2AEBD7C2D83C}.Release|Any CPU.Build.0 = Release|Any CPU
{8CA09061-2BEF-4506-A763-07062D2BD6AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8CA09061-2BEF-4506-A763-07062D2BD6AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{310BD727-837D-47B2-83F2-C57E61B0DC36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{310BD727-837D-47B2-83F2-C57E61B0DC36}.Debug|Any CPU.Build.0 = Debug|Any CPU
{310BD727-837D-47B2-83F2-C57E61B0DC36}.Release|Any CPU.ActiveCfg = Release|Any CPU
{310BD727-837D-47B2-83F2-C57E61B0DC36}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -293,6 +299,7 @@ Global
{4A175C27-EAFE-47E7-90F6-873B37863656} = {0EF6EA64-D7C3-420D-9890-EAE8D54A57E6}
{DDC41278-FB60-403A-B969-2AEBD7C2D83C} = {0EF6EA64-D7C3-420D-9890-EAE8D54A57E6}
{8CA09061-2BEF-4506-A763-07062D2BD6AC} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
{310BD727-837D-47B2-83F2-C57E61B0DC36} = {BF3ED6BF-ADF3-4D25-8E89-02FB8D945CA9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {65220BF2-EAE1-4CB2-AA58-EBE80768CB40}
Expand Down
66 changes: 66 additions & 0 deletions examples/Workflow/WorkflowSubWorkflow/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Dapr.Client;
using Dapr.Workflow;
using WorkflowSubWorkflow.Workflows;
using Microsoft.Extensions.Hosting;

const string DaprWorkflowComponent = "dapr";

// The workflow host is a background service that connects to the sidecar over gRPC
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services =>
{
services.AddDaprWorkflow(options =>
{
options.RegisterWorkflow<DemoWorkflow>();
options.RegisterWorkflow<DemoSubWorkflow>();
});
});

// Dapr uses a random port for gRPC by default. If we don't know what that port
// is (because this app was started separate from dapr), then assume 4001.
if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DAPR_GRPC_PORT")))
{
Environment.SetEnvironmentVariable("DAPR_GRPC_PORT", "4001");
}

// Start the app - this is the point where we connect to the Dapr sidecar to
// listen for workflow work-items to execute.
using var host = builder.Build();
host.Start();


DaprClient daprClient = new DaprClientBuilder().Build();

while (!await daprClient.CheckHealthAsync())
{
Thread.Sleep(TimeSpan.FromSeconds(5));
}


using (daprClient)
{
Console.WriteLine($"Workflow Started.");
await daprClient.WaitForSidecarAsync();

string instanceId = $"demo-workflow-{Guid.NewGuid().ToString()[..8]}";

await daprClient.StartWorkflowAsync(
workflowComponent: DaprWorkflowComponent,
workflowName: nameof(DemoWorkflow),
instanceId: instanceId,
input: instanceId);

await daprClient.WaitForWorkflowCompletionAsync(
workflowComponent: DaprWorkflowComponent,
instanceId: instanceId);

GetWorkflowResponse state = await daprClient.GetWorkflowAsync(
instanceId: instanceId,
workflowComponent: DaprWorkflowComponent);
Console.WriteLine($"Workflow {instanceId} state: {state.RuntimeStatus}");

state = await daprClient.GetWorkflowAsync(
instanceId: instanceId + "-sub",
workflowComponent: DaprWorkflowComponent);
Console.WriteLine($"Workflow {instanceId} state: {state.RuntimeStatus}");

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\..\src\Dapr.Workflow\Dapr.Workflow.csproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>612,618</NoWarn>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Dapr.Workflow;

namespace WorkflowSubWorkflow.Workflows
{
public class DemoSubWorkflow : Workflow<string, bool>
{
public override async Task<bool> RunAsync(WorkflowContext context, string input)
{
Console.WriteLine($"Workflow {context.InstanceId} Started.");
Console.WriteLine($"Received input: {input}.");
await context.CreateTimer(TimeSpan.FromSeconds(5));
return true;
}
}
}
16 changes: 16 additions & 0 deletions examples/Workflow/WorkflowSubWorkflow/Workflows/DemoWorkflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Dapr.Workflow;

namespace WorkflowSubWorkflow.Workflows
{
public class DemoWorkflow : Workflow<string, bool>
{
public override async Task<bool> RunAsync(WorkflowContext context, string instanceId)
{
Console.WriteLine($"Workflow {instanceId} Started.");
string subInstanceId = instanceId + "-sub";
ChildWorkflowTaskOptions options = new ChildWorkflowTaskOptions(subInstanceId);
await context.CallChildWorkflowAsync<bool>(nameof(DemoSubWorkflow), "Hello, sub-workflow", options);
return true;
}
}
}
Loading