I am trying to configure a recurring task in my MVC application starting at 00:00 every day. I know this can be achieved by RecurringJob.AddOrUpdate(() => Console.Write("Easy!"), Cron.Daily);
. Now my question is does hangfire cron work on basis of UTC time or the local time at server. If it works on local time, is there a way to run that on UTC time? I have to do this because this will be a day end process and all the scheduled tasks are scheduled based on the Universal time in my database.
Default time zone is UTC. But switch to local time by default will introduce breaking changes, so it will be performed in version 2.0. Windows Time Zone IDs are being used to store time zones. So there may be some issues when your Hangfire infrastructure contains both Windows and Linux.
To execute a job in local time zone you can use TimeZoneInfo. Local . Show activity on this post. To run in a specific timezone instead of UTC, look up the TimeZoneInfo you want.
The Cron class contains different methods and overloads to run jobs on a minute, hourly, daily, weekly, monthly and yearly basis. You can also use CRON expressions to specify a more complex schedule: RecurringJob. AddOrUpdate("powerfuljob", () => Console.
To execute a job in UTC
time zone, you can provide TimeZoneInfo
as TimeZoneInfo.Utc
while defining your jobs.
RecurringJob.AddOrUpdate(() => Console.Write("Easy!"), Cron.Daily, TimeZoneInfo.Utc);
To execute a job in local time zone you can use TimeZoneInfo.Local
.
To run in a specific timezone instead of UTC, look up the TimeZoneInfo
you want. The task will run at midnight.
var manager = new RecurringJobManager(); manager.AddOrUpdate("some-id", Job.FromExpression(() => Method()), Cron.Daily(), TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With