Search code examples
c#azureazure-service-fabricservice-fabric-actor

How to schedule a task in service fabric using 'Ac­tor Remin­der' mechanism?


I want to generate invoices on daily, weekly and monthly basis and want to send invoices periodically. Each customer have different invoice settings.


Solution

  • Sometime ago I wondered how to do that myself. Based on the excellent documentation I came up with a sample app, see my repo.

    Your actor should implement the IRemindable interface. To create a reminder use this in an actor method:

    await RegisterReminderAsync(
                "MyReminder", // name of the reminder
                Encoding.ASCII.GetBytes(message), // byte array with payload (message is a string in my case)
                dueTime, // When is the reminder first activated
                snoozeTime); // Interval between activations
    

    In your case set the snoozeTime to a day or to a week to have the reminder activate every period.

    When the due time is there the method ReceiveReminderAsync is called:

    public Task ReceiveReminderAsync(string reminderName, byte[] state, TimeSpan dueTime, TimeSpan period)
    {
        ActorEventSource.Current.Message($"Actor recieved reminder {reminderName}.");
    
        ...
    }
    

    You can tell by the value of ReceiveReminderAsync which reminder it is about and you can use the content of state to act on the payload.

    To dismiss a reminder use the UnregisterReminderAsync method:

    await UnregisterReminderAsync(GetReminder("MyReminder"));