Skip to content

Instantly share code, notes, and snippets.

@abdullin
Created June 28, 2011 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdullin/1050752 to your computer and use it in GitHub Desktop.
Save abdullin/1050752 to your computer and use it in GitHub Desktop.
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