Configure cron job that is executing every 15 minutes on Hangfire
Asked Answered
J

4

35

I am using Hangfire and like the software very much! But one thing I am missing is how to add a recurring job that executes every few minutes (e.g. every 15 minutes). Is there a way to achieve this?

Japan answered 10/1, 2015 at 11:19 Comment(1)
You can use this (cronmaker.com) to generate cron expressions. And then (as per the accepted answer) you can call RecurringJob.AddOrUpdate(() => Console.Write("Recurring"), cronText);Dusty
J
82

Currently I am using this approach:

RecurringJob.AddOrUpdate(() => Console.Write("Recurring"), "*/15 * * * *");

And is working like a charm.

Reference to my question in Hangfire forums: http://discuss.hangfire.io/t/how-to-create-cron-job-that-is-executing-every-15-minutes/533

Japan answered 13/1, 2015 at 8:44 Comment(6)
Thanks for sharing the solution. Didn't know Hangfire accepted those cron expressions.Easterly
For 30 min = "*/30 * * * *" ?Geehan
Yes, */30 * * * * will be for every 30 minutesJapan
Unfortunately if you want to stagger your tasks i.e. not have them all run at once at 00:00, 00:15, 00:30 using this method it will not work. I still havent found a way around thatFeverfew
Can you please help me with this question :#48423131Sgraffito
How can I specify “every 10 seconds”?Deepfry
L
7

We can also use the following code line to schedule job for every 15 minutes.

RecurringJob.AddOrUpdate(() => Console.Write("Recurring"), Cron.MinuteInterval(15));
Larocca answered 19/12, 2018 at 11:19 Comment(2)
MinuteInterval will be removed in 2.0.0Ken
how set the start time, for example, 8 am and schedule job for every 15 minutes?Peebles
E
4

Looking at Hangfire.Cron class I don't know if it's possible.

A workaround would be to create four different schedules i.e:

RecurringJob.AddOrUpdate(
    () => Console.WriteLine("Transparent!"), 
    Cron.Hourly(0));

RecurringJob.AddOrUpdate(
    () => Console.WriteLine("Transparent!"), 
    Cron.Hourly(15));

RecurringJob.AddOrUpdate(
    () => Console.WriteLine("Transparent!"), 
    Cron.Hourly(30));

RecurringJob.AddOrUpdate(
    () => Console.WriteLine("Transparent!"), 
    Cron.Hourly(45));
Easterly answered 12/1, 2015 at 23:1 Comment(0)
S
2

try the official tool for CronExpression:

https://crontab.guru/#*/15_*_*_*_*
Sinistral answered 8/7, 2021 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.