Skip to content

Instantly share code, notes, and snippets.

@jrgcubano
Created August 9, 2017 07:17
Show Gist options
  • Save jrgcubano/233f2346e901f0ea015362d4ff5cb024 to your computer and use it in GitHub Desktop.
Save jrgcubano/233f2346e901f0ea015362d4ff5cb024 to your computer and use it in GitHub Desktop.
Cron to observable Rx, retry and circuit breaker
// via https://stackoverflow.com/questions/26597393/cron-observable-sequence
// and cloud patterns (circuit breaker, retry, etc)
https://github.com/myquay/Solve.Reliability.Rx
// convert cron to time https://github.com/HangfireIO/Cronos
public static IObservable<int> Cron(string cron, IScheduler scheduler)
{
var schedule = CrontabSchedule.Parse(cron);
return Observable.Generate(0, d => true, d => d + 1, d => d,
d => new DateTimeOffset(schedule.GetNextOccurrence(scheduler.Now.UtcDateTime)), scheduler);
}
[TestMethod]
public void TestCronInterval()
{
var scheduler = new TestScheduler();
var end = scheduler.Now.UtcDateTime.AddMinutes(10);
const string cron = "*/5 * * * *";
var i = 0;
var seconds = 0;
var sub = ObservableCron.Cron(cron, scheduler).Subscribe(x => i++);
while (i < 2)
{
seconds++;
scheduler.AdvanceBy(TimeSpan.TicksPerSecond);
}
Assert.IsTrue(seconds == 600);
Assert.AreEqual(end, scheduler.Now.UtcDateTime);
sub.Dispose();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment