Created
June 28, 2011 08:43
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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