A Timely Post – Introducing Palmer and TempusReader
I may have found the Peanut Butter to my Jelly. Maybe not, but I accidentally stumbled upon a project called Palmer. This library lets one define an amount of time to do some sort of activity. Maybe a call that fails frequently, so you want to ping it many times before just giving up. I’m not using it in anything currently, but I’d like to some day.
I noticed that it uses some TimeSpans and I’ve been meaning to incorporate my pet project TempusReader into something of use. I combined the two and they worked just great together! I set up a tiny example like this, just to get going with them:
class Program | |
{ | |
private static int attempts = 0; | |
static void Main(string[] args) | |
{ | |
Retry.On<Exception>().For("2.5 seconds".InTime()).With(context => | |
{ | |
Program.SendRequest(); | |
}); | |
} | |
public static string SendRequest() | |
{ | |
attempts++; | |
Console.WriteLine("attempt #{0}", attempts); | |
Thread.Sleep(456); | |
throw new Exception("Oh snap!"); | |
} | |
} |
Obviously the example isn’t real world, but you get the point. The code is actually all Palmer, the only part that is TempusReader is the .InTime() extension method. Alternatively, I could have used other methods to specify the TimeSpan:
new Time("1 hr, 5 mins and 8 seconds") "50 milliseconds".InTime()
This takes a string and converts it to a TempusReader.Time object, which can be implicitly cast to a TimeSpan for use in the .For() method.
Both these projects are on NuGet (the current version of Palmer was giving me issues):
Install-Package Palmer -Version 0.1.4723.40614
Install-Package TempusReader
Visit the GitHub pages for both Palmer and TempusReader to learn more about how to use them!