-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathProgram.cs
95 lines (83 loc) · 3.45 KB
/
Program.cs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) Jacob Viau. All rights reserved.
// Licensed under the APACHE 2.0. See LICENSE file in the project root for full license information.
using DurableTask.Core;
using DurableTask.DependencyInjection;
using DurableTask.Emulator;
using DurableTask.Hosting;
using DurableTask.Hosting.Options;
using DurableTask.Samples.Generics;
using DurableTask.Samples.Greetings;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace DurableTask.Samples;
/// <summary>
/// The samples program.
/// </summary>
public class Program
{
/// <summary>
/// The entry point.
/// </summary>
/// <param name="args">The supplied arguments, if any.</param>
/// <returns>A task that completes when this program is finished running.</returns>
public static Task Main(string[] args)
{
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(builder => builder.AddUserSecrets<Program>())
.ConfigureServices(services =>
{
services.Configure<TaskHubOptions>(opt =>
{
opt.CreateIfNotExists = true;
});
services.AddSingleton<IConsole, ConsoleWrapper>();
services.AddHostedService<TaskEnqueuer>();
services.AddSingleton(UseLocalEmulator());
})
.ConfigureTaskHubWorker((context, builder) =>
{
builder.AddClient();
builder.UseOrchestrationMiddleware<SampleMiddleware>();
builder.UseActivityMiddleware<SampleMiddleware>();
builder
.AddOrchestration<GreetingsOrchestration>()
.AddOrchestration<GenericOrchestrationRunner>();
builder.AddActivitiesFromAssembly<Program>();
})
.UseConsoleLifetime()
.Build();
return host.RunAsync();
}
private static IOrchestrationService UseLocalEmulator()
=> new LocalOrchestrationService();
private class TaskEnqueuer : BackgroundService
{
private readonly TaskHubClient _client;
private readonly IConsole _console;
private readonly string _instanceId = Guid.NewGuid().ToString();
public TaskEnqueuer(TaskHubClient client, IConsole console)
{
_client = client ?? throw new ArgumentNullException(nameof(client));
_console = console ?? throw new ArgumentNullException(nameof(console));
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
OrchestrationInstance instance = await _client.CreateOrchestrationInstanceAsync(
NameVersionHelper.GetDefaultName(typeof(GenericOrchestrationRunner)),
NameVersionHelper.GetDefaultVersion(typeof(GenericOrchestrationRunner)),
_instanceId,
null,
new Dictionary<string, string>()
{
["CorrelationId"] = Guid.NewGuid().ToString(),
});
OrchestrationState result = await _client.WaitForOrchestrationAsync(
instance, TimeSpan.FromSeconds(60), stoppingToken);
_console.WriteLine();
_console.WriteLine($"Orchestration finished.");
_console.WriteLine($"Run stats: {result.Status}");
_console.WriteLine("Press Ctrl+C to exit");
}
}
}