-
-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathProgram.cs
More file actions
91 lines (76 loc) · 2.83 KB
/
Program.cs
File metadata and controls
91 lines (76 loc) · 2.83 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
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
// -----------------------------------------------------------------------
// <copyright file="Program.cs" company="Asynkron AB">
// Copyright (C) 2015-2024 Asynkron AB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using System.Runtime.Loader;
using System.Threading.Tasks;
using System.Threading;
using ClusterHelloWorld.Messages;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Proto;
using Proto.Cluster;
using Proto.Cluster.Kubernetes;
using Proto.Cluster.PartitionActivator;
using Proto.Remote;
using Proto.Remote.GrpcNet;
using Proto.Remote;
using static System.Threading.Tasks.Task;
using ProtosReflection = ClusterHelloWorld.Messages.ProtosReflection;
// Hook SIGTERM to a cancel token to know when k8s is shutting us down
// hostBuilder should be used in production
var cts = new CancellationTokenSource();
AssemblyLoadContext.Default.Unloading += ctx => cts.Cancel();
Log.SetLoggerFactory(
LoggerFactory.Create(l => l.AddConsole(options =>
{
//options.FormatterName = "json"; // Use the JSON formatter
}).SetMinimumLevel(LogLevel.Debug)
.AddFilter("Proto.Cluster.Gossip", LogLevel.Information)
.AddFilter("Proto.Context.ActorContext", LogLevel.Information)));
var kubernetesProvider = new KubernetesProvider();
var advertisedHost = await kubernetesProvider.GetPodFqdn();
var system = new ActorSystem(new ActorSystemConfig().WithDeveloperSupervisionLogging(true))
.WithRemote(RemoteConfig
.BindToAllInterfaces(advertisedHost: advertisedHost, port: 4020)
.WithProtoMessages(ProtosReflection.Descriptor))
.WithCluster(ClusterConfig
.Setup("MyCluster",
kubernetesProvider,
new PartitionActivatorLookup())
.WithClusterKind(
HelloGrainActor.GetClusterKind((ctx, identity) => new HelloGrain(ctx, identity.Identity)))
);
system.EventStream.Subscribe<ClusterTopology>(
e => { Console.WriteLine($"{DateTime.Now:O} My members {e.TopologyHash}"); }
);
await system
.Cluster()
.StartMemberAsync();
Console.WriteLine("Started...");
Console.CancelKeyPress += (_, e) =>
{
e.Cancel = true; // prevent the process from terminating.
cts.Cancel();
};
await Delay(Timeout.Infinite, cts.Token);
Console.WriteLine("Shutting Down...");
public class HelloGrain : HelloGrainBase
{
private readonly string _identity;
public HelloGrain(IContext ctx, string identity) : base(ctx)
{
_identity = identity;
}
public override Task<HelloResponse> SayHello(HelloRequest request)
{
Console.WriteLine("Got request!!");
var res = new HelloResponse
{
Message = $"Hello from typed grain {_identity} | {DateTime.UtcNow:O}"
};
return FromResult(res);
}
}