src/AWS/Orleans.Reminders.DynamoDB/README.md
Microsoft Orleans Reminders for DynamoDB provides persistence for Orleans reminders using Amazon's DynamoDB. This allows your Orleans applications to schedule persistent reminders that will be triggered even after silo restarts or grain deactivation.
To use this package, install it via NuGet:
dotnet add package Microsoft.Orleans.Reminders.DynamoDB
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Orleans.Configuration;
using Orleans.Hosting;
var builder = Host.CreateApplicationBuilder(args)
.UseOrleans(siloBuilder =>
{
siloBuilder
.UseLocalhostClustering()
// Configure DynamoDB as reminder storage
.UseDynamoDBReminderService(options =>
{
options.AccessKey = "YOUR_AWS_ACCESS_KEY";
options.SecretKey = "YOUR_AWS_SECRET_KEY";
options.Region = "us-east-1";
options.TableName = "OrleansReminders";
options.CreateIfNotExists = true;
});
});
// Run the host
var host = builder.Build();
await host.StartAsync();
// Get a reference to the grain
var reminderGrain = host.Services.GetRequiredService<IGrainFactory>()
.GetGrain<IReminderGrain>("my-reminder-grain");
// Start the reminder
await reminderGrain.StartReminder("ExampleReminder");
Console.WriteLine("Reminder started!");
// Keep the host running until the application is shut down
await host.WaitForShutdownAsync();
using System;
using System.Threading.Tasks;
using Orleans;
using Orleans.Runtime;
namespace ReminderExample;
public interface IReminderGrain : IGrainWithStringKey
{
Task StartReminder(string reminderName);
Task StopReminder();
}
public class ReminderGrain : Grain, IReminderGrain, IRemindable
{
private string _reminderName = "MyReminder";
public async Task StartReminder(string reminderName)
{
_reminderName = reminderName;
// Register a persistent reminder
await RegisterOrUpdateReminder(
reminderName,
TimeSpan.FromMinutes(2), // Time to delay before the first tick (must be > 1 minute)
TimeSpan.FromMinutes(5)); // Period of the reminder (must be > 1 minute)
}
public async Task StopReminder()
{
// Find and unregister the reminder
var reminder = await GetReminder(_reminderName);
if (reminder != null)
{
await UnregisterReminder(reminder);
}
}
public Task ReceiveReminder(string reminderName, TickStatus status)
{
// This method is called when the reminder ticks
Console.WriteLine($"Reminder {reminderName} triggered at {DateTime.UtcNow}. Status: {status}");
return Task.CompletedTask;
}
}
For more comprehensive documentation, please refer to: