Orleans is an actor model framework — a framework used for easily creating distributed systems across a cluster of machines. In this post we’ll explore the “Reminders” feature of Orleans.
First off, the docs:
Reminders can be used in Orleans to perform tasks on a “schedule” more or less. I’m having trouble thinking of a simple example that actually makes sense, so, we’re going to go with an everything’s ok alarm:
- Enable a reminder service within the
ISiloHostBuilder— we’ll use the in memory one just for simplicity’s sake, and to not have to rely on additional infrastructure
- A new reminder grain
- Something for the reminder grain to do
I figured we could use the
FakeEmailSender introduced in:
in order to send “Fake email” everything’s ok notifications.
Starting from https://github.com/Kritner-Blogs/OrleansGettingStarted/releases/tag/v0.40, we’ll enable the in-memory reminder service by adding the following line to our ISiloHostBuilder.
The full method is:
Some other possible reminder services to use include
Let’s create our everything’s ok alarm grain! I discovered in writing this, that there is a 1 minute minimum on the amount of time between “reminds”, so unfortunately, we’ll not be going with the originally planned 3 seconds :(
In the above, we’re doing a pretty standard grain interface, with the additional (to be) implemented
IRemindable. Two methods are attached to the interface, one to start the reminder, one to stop it. Note that the
IRemindable interface requires the implementing class to implement:
As I mentioned previously, we’ll be using the
FakeEmailSender created from a previous post, as well as having our to be created grain utilize other grains (grainception)!
That could look like:
A few things of note from the above:
[StorageProvider(ProviderName = Constants.OrleansMemoryProvider)]— we’re making the grain stateful so (theoretically) the reminder will persist on shutdown. Note, it will not in our case because of using in memory storage, I think it would otherwise.
IGrainReminder _reminder = null;— holds reference to our started reminder, used for stopping the reminder.
Task ReceiveReminder(string reminderName, TickStatus status)— this is the method where we actually define what happens when the reminder occurs.
var emailSenderGrain = GrainFactory.GetGrain<IEmailSenderGrain>(Guid.Empty);— here we’re using a slightly different means of retrieving a grain, since we’re actually doing it from the
SiloHost, rather than
Client. Note that this grain being pulled also makes use of dependency injection, but its dependency is only injected into the grain that actually needs it, not this reminder grain.
as per usual, we’re going to create a new
IOrleansFunction concretion for use in our menu system; that new grain will also be added to be returned from our
As per the norm, we’ll be starting the
Client, and trying the new grain out.
In the above, you can see that our “FakeEmail” went out to the Orleans log, stating that everything’s ok.
One other cool thing we can see due to adding the Orleans Dashboard in a previous post is:
In this post we learned a little bit about another Orleans feature — Reminders! You can find the code as of this post at:
- Getting Started with Microsoft Orleans
- Microsoft Orleans — Reusing Grains and Grain State
- Using polymorphism to update Orleans Project to be ready for new Orleans Examples!
- Microsoft Orleans — Reporting Dashboard
- Microsoft Orleans — Code Generation Issue
- Microsoft Orleans — Dependency Injection
- Code as of post — v0.45
- Microsoft Orleans Docs — Reminders and Timers
- Photo by linper on Unsplash.