Skip to content

Commit

Permalink
add sample for workflow update (#40)
Browse files Browse the repository at this point in the history
add sample for workflow update
  • Loading branch information
antmendoza authored Mar 6, 2024
1 parent 9933d90 commit 49c0c06
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Prerequisites:
* [Schedules](src/Schedules) - How to schedule workflows to be run at specific times in the future.
* [WorkerSpecificTaskQueues](src/WorkerSpecificTaskQueues) - Use a unique task queue per Worker to have certain Activities only run on that specific Worker.
* [WorkerVersioning](src/WorkerVersioning) - How to use the Worker Versioning feature to more easily deploy changes to Workflow & other code.
* [WorkflowUpdate](src/WorkflowUpdate) - How to use the Workflow Update feature while blocking in update method for concurrent updates.

## Development

Expand Down
8 changes: 8 additions & 0 deletions TemporalioSamples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TemporalioSamples.Mutex", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TemporalioSamples.Saga", "src\Saga\TemporalioSamples.Saga.csproj", "{B79F07F7-3429-4C58-84C3-08587F748B2D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemporalioSamples.WorkflowUpdate", "src\WorkflowUpdate\TemporalioSamples.WorkflowUpdate.csproj", "{B3DB7B8C-7BD3-4A53-A809-AB6279B1A630}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -137,6 +139,10 @@ Global
{B79F07F7-3429-4C58-84C3-08587F748B2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B79F07F7-3429-4C58-84C3-08587F748B2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B79F07F7-3429-4C58-84C3-08587F748B2D}.Release|Any CPU.Build.0 = Release|Any CPU
{B3DB7B8C-7BD3-4A53-A809-AB6279B1A630}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3DB7B8C-7BD3-4A53-A809-AB6279B1A630}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3DB7B8C-7BD3-4A53-A809-AB6279B1A630}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3DB7B8C-7BD3-4A53-A809-AB6279B1A630}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -164,5 +170,7 @@ Global
{CA3FD1BC-C918-4B15-96F6-D6DDA125E63C} = {1A647B41-53D0-4638-AE5A-6630BAAE45FC}
{3168FB2D-D821-433A-A761-309E0474DE48} = {1A647B41-53D0-4638-AE5A-6630BAAE45FC}
{B79F07F7-3429-4C58-84C3-08587F748B2D} = {1A647B41-53D0-4638-AE5A-6630BAAE45FC}
{B3DB7B8C-7BD3-4A53-A809-AB6279B1A630} = {1A647B41-53D0-4638-AE5A-6630BAAE45FC}

EndGlobalSection
EndGlobal
74 changes: 74 additions & 0 deletions src/WorkflowUpdate/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Microsoft.Extensions.Logging;
using Temporalio.Client;
using Temporalio.Worker;
using TemporalioSamples.WorkflowUpdate;

// Create a client to localhost on default namespace
var client = await TemporalClient.ConnectAsync(new("localhost:7233")
{
LoggerFactory = LoggerFactory.Create(builder =>
builder.
AddSimpleConsole(options => options.TimestampFormat = "[HH:mm:ss] ").
SetMinimumLevel(LogLevel.Information)),
});

async Task RunWorkerAsync()
{
// Cancellation token cancelled on ctrl+c
using var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += (_, eventArgs) =>
{
tokenSource.Cancel();
eventArgs.Cancel = true;
};

// Run worker until cancelled
Console.WriteLine("Running worker");
using var worker = new TemporalWorker(
client,
new TemporalWorkerOptions(taskQueue: "workflow-update-queue").
AddWorkflow<WorkflowUpdate>());
try
{
await worker.ExecuteAsync(tokenSource.Token);
}
catch (OperationCanceledException)
{
Console.WriteLine("Worker cancelled");
}
}

async Task ExecuteWorkflowAsync()
{
Console.WriteLine("Executing workflow");

var handle = await client.StartWorkflowAsync(
(WorkflowUpdate wf) => wf.RunAsync(),
new(id: $"workflow-update-{Guid.NewGuid()}", taskQueue: "workflow-update-queue"));

await handle.ExecuteUpdateAsync(wf =>
wf.SubmitScreenAsync(new WorkflowUpdate.UiRequest(
$"requestId-{Guid.NewGuid()}",
WorkflowUpdate.ScreenId.Screen1)));

await handle.ExecuteUpdateAsync(wf =>
wf.SubmitScreenAsync(new WorkflowUpdate.UiRequest(
$"requestId-{Guid.NewGuid()}",
WorkflowUpdate.ScreenId.Screen2)));

// Workflow completes
await handle.GetResultAsync();
Console.WriteLine("Workflow completes");
}

switch (args.ElementAtOrDefault(0))
{
case "worker":
await RunWorkerAsync();
break;
case "workflow":
await ExecuteWorkflowAsync();
break;
default:
throw new ArgumentException("Must pass 'worker' or 'workflow' as the single argument");
}
19 changes: 19 additions & 0 deletions src/WorkflowUpdate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Workflow Update

This workflow represents a UI Wizard. We use [Workflow Update](https://docs.temporal.io/workflows#update)
to mutate the workflow state (submit some data) and wait for the workflow update method to return the next screen
the client has to navigate to.

The update validator is used to reject null arguments (rejected updates are not included in workflow history).


To run, first see [README.md](../../README.md) for prerequisites. Then, run the following from this directory
in a separate terminal to start the worker:

dotnet run worker

Then in another terminal, run the workflow from this directory:

dotnet run workflow

This will show logs in the worker window of the workflow running.
7 changes: 7 additions & 0 deletions src/WorkflowUpdate/TemporalioSamples.WorkflowUpdate.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>

</Project>
65 changes: 65 additions & 0 deletions src/WorkflowUpdate/WorkflowUpdate.workflow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
namespace TemporalioSamples.WorkflowUpdate;

using Temporalio.Workflows;

[Workflow]
public class WorkflowUpdate
{
private bool updateInProgress;
private ScreenId currentScreen = ScreenId.Screen1;

[WorkflowRun]
public async Task RunAsync() => await Workflow.WaitConditionAsync(() => currentScreen == ScreenId.End);

[WorkflowUpdateValidator(nameof(SubmitScreenAsync))]
public void ValidatorSubmitScreen(UiRequest request)
{
if (request == null)
{
throw new ArgumentException("Input can not be null");
}
}

[WorkflowUpdate]
public async Task<ScreenId> SubmitScreenAsync(UiRequest request)
{
// Ensure we process the requests one by one
await Workflow.WaitConditionAsync(() => !updateInProgress);
updateInProgress = true;

// Activities can be scheduled here
SetNextScreen(request);
updateInProgress = false;
return currentScreen;
}

private void SetNextScreen(UiRequest currentRequest)
{
currentScreen = currentRequest.ScreenId switch
{
ScreenId.Screen1 => ScreenId.Screen2,
ScreenId.Screen2 => ScreenId.End,
_ => currentScreen,
};
}

public enum ScreenId
{
/// <summary>
/// Screen1.
/// </summary>
Screen1,

/// <summary>
/// Screen2.
/// </summary>
Screen2,

/// <summary>
/// End.
/// </summary>
End,
}

public record UiRequest(string RequestId, ScreenId ScreenId);
}
1 change: 1 addition & 0 deletions tests/TemporalioSamples.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ProjectReference Include="..\src\ActivityWorker\TemporalioSamples.ActivityWorker.csproj" />
<ProjectReference Include="..\src\Encryption\Codec\TemporalioSamples.Encryption.Codec.csproj" />
<ProjectReference Include="..\src\Encryption\CodecServer\TemporalioSamples.Encryption.CodecServer.csproj" />
<ProjectReference Include="..\src\WorkflowUpdate\TemporalioSamples.WorkflowUpdate.csproj" />
</ItemGroup>

</Project>
62 changes: 62 additions & 0 deletions tests/WorkflowUpdate/WorkflowUpdateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using Temporalio.Exceptions;

namespace TemporalioSamples.Tests.WorkflowUpdate;

using Temporalio.Testing;
using Temporalio.Worker;
using Xunit;
using Xunit.Abstractions;

public class WorkflowUpdateTests : TestBase
{
public WorkflowUpdateTests(ITestOutputHelper output)
: base(output)
{
}

[Fact]
public async Task SimpleRun_Succeed()
{
await using var env = await WorkflowEnvironment.StartLocalAsync();
using var worker = new TemporalWorker(
env.Client,
new TemporalWorkerOptions("my-task-queue").
AddWorkflow<TemporalioSamples.WorkflowUpdate.WorkflowUpdate>());
await worker.ExecuteAsync(async () =>
{
var handle = await env.Client.StartWorkflowAsync(
(TemporalioSamples.WorkflowUpdate.WorkflowUpdate wf) => wf.RunAsync(),
new(id: $"workflow-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!));

Assert.Equal(
TemporalioSamples.WorkflowUpdate.WorkflowUpdate.ScreenId.Screen2,
await handle.ExecuteUpdateAsync((wf) => wf.SubmitScreenAsync(new TemporalioSamples.WorkflowUpdate.WorkflowUpdate.UiRequest($"requestId-{Guid.NewGuid()}", TemporalioSamples.WorkflowUpdate.WorkflowUpdate.ScreenId.Screen1))));

Assert.Equal(
TemporalioSamples.WorkflowUpdate.WorkflowUpdate.ScreenId.End,
await handle.ExecuteUpdateAsync((wf) => wf.SubmitScreenAsync(new TemporalioSamples.WorkflowUpdate.WorkflowUpdate.UiRequest($"requestId-{Guid.NewGuid()}", TemporalioSamples.WorkflowUpdate.WorkflowUpdate.ScreenId.Screen2))));

// Workflow completes
await handle.GetResultAsync();
});
}

[Fact]
public async Task Reject_Update()
{
await using var env = await WorkflowEnvironment.StartLocalAsync();
using var worker = new TemporalWorker(
env.Client,
new TemporalWorkerOptions("my-task-queue").
AddWorkflow<TemporalioSamples.WorkflowUpdate.WorkflowUpdate>());
await worker.ExecuteAsync(async () =>
{
var handle = await env.Client.StartWorkflowAsync(
(TemporalioSamples.WorkflowUpdate.WorkflowUpdate wf) => wf.RunAsync(),
new(id: $"workflow-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!));

await Assert.ThrowsAsync<WorkflowUpdateFailedException>(() =>
handle.ExecuteUpdateAsync(wf => wf.SubmitScreenAsync(null!)));
});
}
}

0 comments on commit 49c0c06

Please sign in to comment.