-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sample for workflow update (#40)
add sample for workflow update
- Loading branch information
1 parent
9933d90
commit 49c0c06
Showing
8 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
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
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
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,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"); | ||
} |
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,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. |
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,7 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
</Project> |
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,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); | ||
} |
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
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,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!))); | ||
}); | ||
} | ||
} |