forked from temporalio/samples-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelloHandlerWorkflow.workflow.cs
More file actions
52 lines (46 loc) · 2.04 KB
/
HelloHandlerWorkflow.workflow.cs
File metadata and controls
52 lines (46 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace TemporalioSamples.NexusCancellation.Handler;
using Microsoft.Extensions.Logging;
using Temporalio.Exceptions;
using Temporalio.Workflows;
[Workflow]
public class HelloHandlerWorkflow
{
[WorkflowRun]
public async Task<IHelloService.HelloOutput> RunAsync(IHelloService.HelloInput input)
{
Workflow.Logger.LogInformation(
"HelloHandlerWorkflow started for {Name} in {Language}",
input.Name,
input.Language);
// Sleep for a random duration to simulate some work
var duration = TimeSpan.FromSeconds(Workflow.Random.Next(5));
try
{
await Workflow.DelayAsync(duration);
}
catch (Exception ex) when (TemporalException.IsCanceledException(ex))
{
// Simulate cleanup work after cancellation is requested.
// Use CancellationToken.None to create a "disconnected" context.
var cleanupDuration = TimeSpan.FromSeconds(Workflow.Random.Next(5));
await Workflow.DelayAsync(cleanupDuration, CancellationToken.None);
Workflow.Logger.LogInformation(
"HelloHandlerWorkflow for {Name} in {Language} was cancelled after {Duration} of work, performed {CleanupDuration} of cleanup",
input.Name,
input.Language,
duration,
cleanupDuration);
throw; // Re-throw the cancellation after cleanup
}
return input.Language switch
{
IHelloService.HelloLanguage.En => new($"Hello {input.Name} 👋"),
IHelloService.HelloLanguage.Fr => new($"Bonjour {input.Name} 👋"),
IHelloService.HelloLanguage.De => new($"Hallo {input.Name} 👋"),
IHelloService.HelloLanguage.Es => new($"¡Hola! {input.Name} 👋"),
IHelloService.HelloLanguage.Tr => new($"Merhaba {input.Name} 👋"),
_ => throw new ApplicationFailureException(
$"Unsupported language: {input.Language}", errorType: "UNSUPPORTED_LANGUAGE"),
};
}
}