Search code examples
c#azure-service-fabric

Run scheduled background job on stateful service in azure service fabric to update some data in Reliable Dictionary on schedule time


Is there away to run scheduled background job on stateful service in azure service fabric ? the only way I found only was timers & reminders which they run on Actors not stateful service. I'm trying to run scheduled background job to clean up some data in Reliable dictionary.


Solution

  • The recommended way to run background jobs in Service Fabric is to simply override the RunAsync operation. This works equally fine for stateful and stateless services - although, as already mentioned, Actors provide some additional functionality with its built in support for reminders and timers.

    Below is a very basic example

    public async Task RunAsync(CancellationToken cancellationToken)
    {
        // do something
        var nextWakeup = TimeSpan.FromHours(1);
        await Task.Delay(nextWakeup, cancellationToken);
    }
    

    If you require something more complex, you can for example, enhance the above to use the IReliableStateManager to manage your own collection of reminders.