Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample of a timer service for powering up sagas (for Lokad.CQRS v2.0)
// the gist is discussed in: http://groups.google.com/group/lokad/browse_thread/thread/4d39dc299037ea09
// This is a sample of a timer service for powering up sagas (for Lokad.CQRS v2.0)
// register it in CqrsHostBuilder like this:
// hostBuilder.Advanced.ConfigureContainer(cb => cb.RegisterType<TimerService>().As<IEngineProcess>());
public sealed class TimerService : IEngineProcess
{
readonly IMessageSender _sender;
public TimerService(IMessageSender sender)
{
_sender = sender;
_lastChecked = DateTime.UtcNow;
}
public void Dispose()
{
}
public void Initialize()
{
}
DateTime _lastChecked;
public Task Start(CancellationToken token)
{
return Task.Factory.StartNew(() =>
{
while (!token.IsCancellationRequested)
{
var current = DateTime.UtcNow;
if (_lastChecked.Date != current.Date)
{
try
{
// we've passed a midnight
_sender.SendOne(new MidnightPassed());
_lastChecked = current;
}
catch(Exception ex)
{
// if sender fails due to azure connectivity - no problem
// we'll keep on trying
Trace.WriteLine(ex);
}
}
// sleep for X minutes or till engine is stopped.
token.WaitHandle.WaitOne(TimeSpan.FromMinutes(5));
}
}, token);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment