-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerApplication.cs
More file actions
64 lines (60 loc) · 3.16 KB
/
Copy pathServerApplication.cs
File metadata and controls
64 lines (60 loc) · 3.16 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
53
54
55
56
57
58
59
60
61
62
63
64
using KeyLoad.Orleans;
namespace KeyLoad.Server;
internal static class ServerApplication
{
internal static async Task RunAsync(string[] args)
{
if (await ServerOfflineFormatUpgrade.TryRunAsync(args).ConfigureAwait(false))
{
return;
}
var app = ServerConfiguration.Build(args);
var failures = new List<Exception>();
PartitionHost? partition = null;
OrleansNode? silo = null;
INodeAdministration? administration = null;
async Task RunLifetimeAsync()
{
try
{
partition = app.Services.GetRequiredService<PartitionHost>();
administration = app.Services.GetRequiredService<INodeAdministration>();
silo = app.Services.GetRequiredService<OrleansNode>();
await partition.Coordinator.StartAsync(app.Lifetime.ApplicationStopping).ConfigureAwait(false);
await app.StartAsync(app.Lifetime.ApplicationStopping).ConfigureAwait(false);
await silo.StartAsync(app.Lifetime.ApplicationStopping).ConfigureAwait(false);
await WaitForStopAsync(app.Lifetime.ApplicationStopping).ConfigureAwait(false);
}
catch (OperationCanceledException) when (app.Lifetime.ApplicationStopping.IsCancellationRequested) { }
}
await ServerFailureObserver.ObserveAsync(RunLifetimeAsync, failures).ConfigureAwait(false);
await ShutdownAsync(app, silo, administration, partition, failures).ConfigureAwait(false);
ServerFailureObserver.ThrowIfAny(failures);
}
private static async Task WaitForStopAsync(CancellationToken stopping)
{
var signal = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
using var registration = stopping.Register(static value => ((TaskCompletionSource)value!).TrySetResult(), signal);
await signal.Task.ConfigureAwait(false);
}
private static async Task ShutdownAsync(WebApplication app, OrleansNode? silo, INodeAdministration? administration,
PartitionHost? partition, List<Exception> failures)
{
using var deadline = new CancellationTokenSource(ServerProtocol.ShutdownTimeout);
if (silo is not null)
{
await ServerFailureObserver.ObserveAsync(() => silo.StopAsync(deadline.Token), failures).ConfigureAwait(false);
await ServerFailureObserver.ObserveAsync(() => silo.StopAsync(CancellationToken.None), failures).ConfigureAwait(false);
if (!silo.HasJoinedRequestWork)
{
return;
}
}
await ServerFailureObserver.ObserveAsync(() => app.StopAsync(deadline.Token), failures).ConfigureAwait(false);
if (administration is IAsyncDisposable borrowed)
{ await ServerFailureObserver.ObserveAsync(() => borrowed.DisposeAsync().AsTask(), failures).ConfigureAwait(false); }
await ServerFailureObserver.ObserveAsync(() => app.DisposeAsync().AsTask(), failures).ConfigureAwait(false);
if (partition is not null)
{ await ServerFailureObserver.ObserveAsync(() => partition.DisposeAsync().AsTask(), failures).ConfigureAwait(false); }
}
}