-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- added logging to `Client` and `SiloHost` projects
- Loading branch information
Showing
5 changed files
with
152 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,96 @@ | ||
using System; | ||
using Kritner.OrleansGettingStarted.GrainInterfaces; | ||
using Microsoft.Extensions.Logging; | ||
using Orleans; | ||
using Orleans.Configuration; | ||
using Orleans.Runtime; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Kritner.OrleansGettingStarted.Client | ||
{ | ||
class Program | ||
public class Program | ||
{ | ||
static void Main(string[] args) | ||
const int initializeAttemptsBeforeFailing = 5; | ||
private static int attempt = 0; | ||
|
||
static int Main(string[] args) | ||
{ | ||
Console.WriteLine("Hello World!"); | ||
return RunMainAsync().Result; | ||
} | ||
|
||
private static async Task<int> RunMainAsync() | ||
{ | ||
try | ||
{ | ||
using (var client = await StartClientWithRetries()) | ||
{ | ||
await DoClientWork(client); | ||
Console.ReadKey(); | ||
} | ||
|
||
return 0; | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
Console.ReadKey(); | ||
return 1; | ||
} | ||
} | ||
|
||
private static async Task<IClusterClient> StartClientWithRetries() | ||
{ | ||
attempt = 0; | ||
IClusterClient client; | ||
client = new ClientBuilder() | ||
.UseLocalhostClustering() | ||
.Configure<ClusterOptions>(options => | ||
{ | ||
options.ClusterId = "dev"; | ||
options.ServiceId = "HelloWorldApp"; | ||
}) | ||
//.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(IHelloWorld).Assembly).WithReferences()) | ||
// I don't want the chatter of logging from the client for now. | ||
//.ConfigureLogging(logging => logging.AddConsole()) | ||
.Build(); | ||
|
||
await client.Connect(RetryFilter); | ||
Console.WriteLine("Client successfully connect to silo host"); | ||
return client; | ||
} | ||
|
||
private static async Task<bool> RetryFilter(Exception exception) | ||
{ | ||
if (exception.GetType() != typeof(SiloUnavailableException)) | ||
{ | ||
Console.WriteLine($"Cluster client failed to connect to cluster with unexpected error. Exception: {exception}"); | ||
return false; | ||
} | ||
attempt++; | ||
Console.WriteLine($"Cluster client attempt {attempt} of {initializeAttemptsBeforeFailing} failed to connect to cluster. Exception: {exception}"); | ||
if (attempt > initializeAttemptsBeforeFailing) | ||
{ | ||
return false; | ||
} | ||
await Task.Delay(TimeSpan.FromSeconds(4)); | ||
return true; | ||
} | ||
|
||
private static async Task DoClientWork(IClusterClient client) | ||
{ | ||
Console.WriteLine("Hello, what should I call you?"); | ||
var name = Console.ReadLine(); | ||
|
||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
name = "anon"; | ||
} | ||
|
||
// example of calling grains from the initialized client | ||
var grain = client.GetGrain<IHelloWorld>(Guid.NewGuid()); | ||
|
||
var response = await grain.SayHello(name); | ||
Console.WriteLine($"\n\n{response}\n\n"); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,57 @@ | ||
using System; | ||
using Kritner.OrleansGettingStarted.Grains; | ||
using Microsoft.Extensions.Logging; | ||
using Orleans; | ||
using Orleans.Configuration; | ||
using Orleans.Hosting; | ||
using System; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
|
||
namespace Kritner.OrleansGettingStarted.SiloHost | ||
{ | ||
class Program | ||
public class Program | ||
{ | ||
static void Main(string[] args) | ||
public static int Main(string[] args) | ||
{ | ||
Console.WriteLine("Hello World!"); | ||
return RunMainAsync().Result; | ||
} | ||
|
||
private static async Task<int> RunMainAsync() | ||
{ | ||
try | ||
{ | ||
var host = await StartSilo(); | ||
Console.WriteLine("Press Enter to terminate..."); | ||
Console.ReadLine(); | ||
|
||
await host.StopAsync(); | ||
|
||
return 0; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
return 1; | ||
} | ||
} | ||
|
||
private static async Task<ISiloHost> StartSilo() | ||
{ | ||
// define the cluster configuration | ||
var builder = new SiloHostBuilder() | ||
.UseLocalhostClustering() | ||
.Configure<ClusterOptions>(options => | ||
{ | ||
options.ClusterId = "dev"; | ||
options.ServiceId = "HelloWorldApp"; | ||
}) | ||
.Configure<EndpointOptions>(options => options.AdvertisedIPAddress = IPAddress.Loopback) | ||
//.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(HelloWorld).Assembly).WithReferences()) | ||
.ConfigureLogging(logging => logging.AddConsole()); | ||
|
||
var host = builder.Build(); | ||
await host.StartAsync(); | ||
return host; | ||
} | ||
} | ||
} |