-
-
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.
Merge pull request #1 from Kritner/grainReuse
Grain reuse/Stateful grains
- Loading branch information
Showing
6 changed files
with
113 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ private static async Task<int> RunMainAsync() | |
using (var client = await StartClientWithRetries()) | ||
{ | ||
await DoClientWork(client); | ||
await DoStatefulWork(client); | ||
Console.ReadKey(); | ||
} | ||
|
||
|
@@ -78,19 +79,59 @@ private static async Task<bool> RetryFilter(Exception exception) | |
|
||
private static async Task DoClientWork(IClusterClient client) | ||
{ | ||
Console.WriteLine("Hello, what should I call you?"); | ||
var name = Console.ReadLine(); | ||
// example of calling grains from the initialized client | ||
var grain = client.GetGrain<IHelloWorld>(Guid.NewGuid()); | ||
var grain2 = client.GetGrain<IHelloWorld>(Guid.NewGuid()); | ||
|
||
Console.WriteLine($"{await grain.SayHello("1")}"); | ||
Console.WriteLine($"{await grain2.SayHello("2")}"); | ||
Console.WriteLine($"{await grain.SayHello("3")}"); | ||
|
||
PrintSeparatorThing(); | ||
} | ||
|
||
private static async Task DoStatefulWork(IClusterClient client) | ||
{ | ||
var kritnerGrain = client.GetGrain<IVisitTracker>("[email protected]"); | ||
var notKritnerGrain = client.GetGrain<IVisitTracker>("[email protected]"); | ||
|
||
await PrettyPrintGrainVisits(kritnerGrain); | ||
await PrettyPrintGrainVisits(notKritnerGrain); | ||
|
||
PrintSeparatorThing(); | ||
Console.WriteLine("Ayyy some people are visiting!"); | ||
|
||
await kritnerGrain.Visit(); | ||
await kritnerGrain.Visit(); | ||
await notKritnerGrain.Visit(); | ||
|
||
PrintSeparatorThing(); | ||
|
||
if (string.IsNullOrEmpty(name)) | ||
await PrettyPrintGrainVisits(kritnerGrain); | ||
await PrettyPrintGrainVisits(notKritnerGrain); | ||
|
||
PrintSeparatorThing(); | ||
Console.Write("ayyy kritner's visiting even more!"); | ||
|
||
for (int i = 0; i < 5; i++) | ||
{ | ||
name = "anon"; | ||
await kritnerGrain.Visit(); | ||
} | ||
|
||
// 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"); | ||
PrintSeparatorThing(); | ||
|
||
await PrettyPrintGrainVisits(kritnerGrain); | ||
await PrettyPrintGrainVisits(notKritnerGrain); | ||
} | ||
|
||
private static async Task PrettyPrintGrainVisits(IVisitTracker grain) | ||
{ | ||
Console.WriteLine($"{grain.GetPrimaryKeyString()} has visited {await grain.GetNumberOfVisits()} times"); | ||
} | ||
|
||
private static void PrintSeparatorThing() | ||
{ | ||
Console.WriteLine($"{Environment.NewLine}-----{Environment.NewLine}"); | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Kritner.OrleansGettingStarted.GrainInterfaces/IVisitTracker.cs
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Orleans; | ||
using System.Threading.Tasks; | ||
|
||
namespace Kritner.OrleansGettingStarted.GrainInterfaces | ||
{ | ||
public interface IVisitTracker : IGrainWithStringKey | ||
{ | ||
Task<int> GetNumberOfVisits(); | ||
Task Visit(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Kritner.OrleansGettingStarted.Grains | ||
{ | ||
public class Constants | ||
{ | ||
public const string OrleansMemoryProvider = "OrleansMemoryProvider"; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Kritner.OrleansGettingStarted.GrainInterfaces; | ||
using Orleans; | ||
using Orleans.Providers; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Kritner.OrleansGettingStarted.Grains | ||
{ | ||
[StorageProvider(ProviderName = Constants.OrleansMemoryProvider)] | ||
public class VisitTracker : Grain<VisitTrackerState>, IVisitTracker | ||
{ | ||
public Task<int> GetNumberOfVisits() | ||
{ | ||
return Task.FromResult(State.NumberOfVisits); | ||
} | ||
|
||
public async Task Visit() | ||
{ | ||
var now = DateTime.Now; | ||
|
||
if (!State.FirstVisit.HasValue) | ||
{ | ||
State.FirstVisit = now; | ||
} | ||
|
||
State.NumberOfVisits++; | ||
State.LastVisit = now; | ||
|
||
await WriteStateAsync(); | ||
} | ||
} | ||
|
||
public class VisitTrackerState | ||
{ | ||
public DateTime? FirstVisit { get; set; } | ||
public DateTime? LastVisit { get; set; } | ||
public int NumberOfVisits { get; set; } | ||
} | ||
} |
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