-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: MregXN <[email protected]>
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
examples/Workflow/WorkflowFanOutFanIn/Activities/NotifyActivity.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Dapr.Workflow; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace WorkflowFanOutFanIn.Activities | ||
{ | ||
public class NotifyActivity : WorkflowActivity<string, object> | ||
{ | ||
readonly ILogger logger; | ||
|
||
public NotifyActivity(ILoggerFactory loggerFactory) | ||
{ | ||
this.logger = loggerFactory.CreateLogger<NotifyActivity>(); | ||
} | ||
|
||
public override Task<object> RunAsync(WorkflowActivityContext context, string message) | ||
{ | ||
this.logger.LogInformation(message); | ||
|
||
return Task.FromResult<object>(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Dapr.Client; | ||
using Dapr.Workflow; | ||
using WorkflowFanOutFanIn.Activities; | ||
using WorkflowFanOutFanIn.Workflows; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
const string DaprWorkflowComponent = "dapr"; | ||
|
||
var builder = Host.CreateDefaultBuilder(args).ConfigureServices(services => | ||
{ | ||
services.AddDaprWorkflow(options => | ||
{ | ||
options.RegisterWorkflow<DemoWorkflow>(); | ||
options.RegisterActivity<NotifyActivity>(); | ||
}); | ||
}); | ||
|
||
Console.WriteLine("SetEnvironmentVariable"); | ||
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(); | ||
|
||
Console.WriteLine("Init Client"); | ||
DaprClient daprClient = new DaprClientBuilder().Build(); | ||
|
||
Console.WriteLine("Wait for the sidecar to become available"); | ||
|
||
while (!await daprClient.CheckHealthAsync()) | ||
{ | ||
Thread.Sleep(TimeSpan.FromSeconds(5)); | ||
} | ||
|
||
using (daprClient) | ||
{ | ||
// Start the workflow | ||
Console.WriteLine("Starting workflow..."); | ||
await daprClient.WaitForSidecarAsync(); | ||
|
||
await daprClient.StartWorkflowAsync( | ||
workflowComponent: DaprWorkflowComponent, | ||
workflowName: nameof(WorkflowFanOutFanIn), | ||
instanceId: "demo-workflow1", | ||
input: "test input"); | ||
|
||
|
||
await daprClient.WaitForWorkflowCompletionAsync( | ||
workflowComponent: DaprWorkflowComponent, | ||
instanceId: "demo-workflow1"); | ||
} |
14 changes: 14 additions & 0 deletions
14
examples/Workflow/WorkflowFanOutFanIn/WorkflowFanOutFanIn.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
37 changes: 37 additions & 0 deletions
37
examples/Workflow/WorkflowFanOutFanIn/Workflows/DemoWorkflow.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Dapr.Workflow; | ||
using WorkflowFanOutFanIn.Activities; | ||
|
||
namespace WorkflowFanOutFanIn.Workflows | ||
{ | ||
public class DemoWorkflow : Workflow<string, string> | ||
{ | ||
readonly WorkflowTaskOptions defaultActivityRetryOptions = new WorkflowTaskOptions | ||
{ | ||
// NOTE: Beware that changing the number of retries is a breaking change for existing workflows. | ||
RetryPolicy = new WorkflowRetryPolicy( | ||
maxNumberOfAttempts: 3, | ||
firstRetryInterval: TimeSpan.FromSeconds(5)), | ||
}; | ||
|
||
public override async Task<string> RunAsync(WorkflowContext context, string input) | ||
{ | ||
// Console.WriteLine("Workflow Started."); | ||
// try | ||
// { | ||
// Task t1 =context.CallActivityAsync(nameof(NotifyActivity),"calling task 1 ..."); | ||
// Task t2 =context.CallActivityAsync(nameof(NotifyActivity),"calling task 2 ..."); | ||
// Task t3 =context.CallActivityAsync(nameof(NotifyActivity),"calling task 3 ..."); | ||
// await Task.WhenAll(t1, t2, t3); | ||
// } | ||
// catch (Exception ex) | ||
// { | ||
// Console.WriteLine("error!"); | ||
// Console.WriteLine(ex.Message); | ||
// Console.WriteLine("error!"); | ||
// } | ||
await context.CreateTimer(TimeSpan.FromSeconds(5)); | ||
Console.WriteLine("Workflow Completed."); | ||
return "Workflow Completed."; | ||
} | ||
} | ||
} |