Introduction

Hangfire is good, but it’s missing a simple CRON Expression converter. The main goal is to get an app setting value from the config file and convert it to a CRON Expression. And at the end, that expression can be used by Hangfire.

App Settings

We will need three kind of settings in our config file to express the different type of recurrences.

Daily: The job will be executed every day at x hours. (TimeSpan)

Period: The job will be executed every x hours and/or x minutes. (TimeSpan)

CronExpression: Allow us to use a custom CRON Expression (string)

The key format is defined as follow: JobName.RecurrenceType

Domain

I created a small class to manipulate easily the CRON Expression. We will enrich that code later when we will play with seconds.

The switch

It’s time to convert our app settings to an object CronInfo. In the first part we identify which kind of CRON Expression the setting is. When it’s done, we call one of our future converter ConverterFromXXXX

The Converters

In the period recurrence converter, we have only 3 possibilities. You can specify the hours and/or the minutes. So we create the CRON Expression like this x */x * * *.

The second option is to use only the minute. In that case we should see something like this */x * * * *

As you can see we can’t allow the seconds. Hangfire don’t support recurrence less than 1 minute. This is why the last case is always */1 * * * * But if you are interested to know how to manage a task that is executed every x seconds, please have a look at this other post.

Be careful, we can’t use the seconds in the daily recurrence. We only take the hours and minutes.

We can only create a CRON Expression with a string value.

Happy coding! 🙂