27

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.

KaraKaplanKhan
  • 734
  • 1
  • 14
  • 31
Anas Shahid
  • 353
  • 2
  • 5
  • 10

4 Answers4

46

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.

Yogi
  • 9,174
  • 2
  • 46
  • 61
15

To run in a specific timezone instead of UTC, look up the TimeZoneInfo you want.

This task will run at midnight IST.

var manager = new RecurringJobManager();
manager.AddOrUpdate("some-id", Job.FromExpression(() => Method()),
    Cron.Daily(),
    TimeZoneInfo.FindSystemTimeZoneById("India Standard Time"));
Arun Prasad E S
  • 9,489
  • 8
  • 74
  • 87
2

As @Arun Prasad E S answer is correct, but it gives obsolete warning, so need to change as given below,

var timeZoneOptions = new RecurringJobOptions();
                timeZoneOptions.TimeZone = TimeZoneInfo.FindSystemTimeZoneById("India Standard Time");

var manager = new RecurringJobManager();
manager.AddOrUpdate("some-id", Job.FromExpression(() => Method()),
    Cron.Daily(),
    timeZoneOptions );

Warning Message : warning CS0618: 'RecurringJob.AddOrUpdate(Expression, string, TimeZoneInfo, string)' is obsolete: 'Please use an overload with the explicit recurringJobId parameter and RecurringJobOptions instead. Will be removed in 2.0.0.'

Somnath Kadam
  • 6,051
  • 6
  • 21
  • 37
0
RecurringJob.AddOrUpdate(() => 
   homeCtrl.SendEmail(), Cron.Daily(), 
   TimeZoneInfo.Local);

// 12:00:00 AM
Syscall
  • 19,327
  • 10
  • 37
  • 52
Md Shahriar
  • 2,072
  • 22
  • 11