HangFire delayed tasks with date
Asked Answered
R

3

9

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?

Rim answered 23/4, 2015 at 13:36 Comment(0)
W
6

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.

Wilson answered 24/4, 2015 at 11:53 Comment(1)
I am also using BackgroundJob.Schedule to schedule a job, as per my knowledge it should queue a job only once at specified time. But I observe some time multiple job entries are queued for same delayed job with very less time difference. any idea why?Collateral
U
5

As the developer answerd my question here, You can simply use the date instead of day(s).

BackgroundJob.Schedule(
    () => Console.WriteLine("Reliable!"), 
    new DateTime(2015, 08, 05, 12, 00, 00));

For 05/08/2015 at 00:00.

Jerry's answer is true for RecurringJobs

RecurringJob.Schedule(
    () => Console.WriteLine("Reliable!"), 
    "00 00 05 8 *");

which will run every year on 05/08 at 00:00

Uncaused answered 6/8, 2015 at 16:7 Comment(1)
Currently there's no Schedule method in RecurringJob class.Idyllic
F
0

Use Cron.Yearly() to run it once a year at certain date-time:

// Will run on 4th of July @ 8 AM UTC every year
BackgroundJob.Schedule(
    () => Console.WriteLine("Happy 4th of July!"), 
    Cron.Yearly(7, 4, 8, 0));
Fauces answered 29/6, 2018 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.