Skip to content

Commit

Permalink
SiloHost/Client Orleans impls
Browse files Browse the repository at this point in the history
- added logging to `Client` and `SiloHost` projects
  • Loading branch information
Kritner committed Oct 7, 2018
1 parent d244f6e commit 4cbd049
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.0" />
<PackageReference Include="Microsoft.Orleans.Client" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kritner.OrleansGettingStarted.GrainInterfaces\Kritner.OrleansGettingStarted.GrainInterfaces.csproj" />
</ItemGroup>

</Project>
92 changes: 88 additions & 4 deletions src/Kritner.OrleansGettingStarted.Client/Program.cs
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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="2.1.0" />
<PackageReference Include="Microsoft.Orleans.OrleansCodeGenerator.Build" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kritner.OrleansGettingStarted.GrainInterfaces\Kritner.OrleansGettingStarted.GrainInterfaces.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.1.0" />
<PackageReference Include="Microsoft.Orleans.Server" Version="2.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Kritner.OrleansGettingStarted.GrainInterfaces\Kritner.OrleansGettingStarted.GrainInterfaces.csproj" />
<ProjectReference Include="..\Kritner.OrleansGettingStarted.Grains\Kritner.OrleansGettingStarted.Grains.csproj" />
</ItemGroup>

</Project>
53 changes: 49 additions & 4 deletions src/Kritner.OrleansGettingStarted.SiloHost/Program.cs
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;
}
}
}

0 comments on commit 4cbd049

Please sign in to comment.