Search code examples
datetaskhangfire

HangFire delayed tasks with date


How can I add date to the HangFire task? For example, this code adds 7 days:

BackgroundJob.Schedule(
    () => Console.WriteLine("Reliable!"), 
    TimeSpan.FromDays(7));

But what if I need to run task in specific date?


Solution

  • If the year doesn't matter, you can use cron expression for this purpose. Most default cron implementations (like NCrontab used by Hangfire) don't include the year field.

    BackgroundJob.Schedule(
        () => Console.WriteLine("Reliable!"), 
        "30 4 27 6 *");
    

    This job will be executed at 4.30am on the 27th of June every year.