-1

So I'm using Hangfire schedule jobs that send out reminders to users. Whenever I set the Job to run every monday for example at 14h00. If I do this in the morning say at 09h00. The first execution, should go off at 14h00. Problem is what seems to be happening is when I look in the dashboard it says Next execution is in 7 days.

Heres the code :

public class ReminderTimeService : IReminderTimeService

{
            private readonly IRecurringJobManager _recurringJobClient;

            public ReminderTimeService(
                 IRecurringJobManager recurringJobClient)
            {
                _recurringJobClient = recurringJobClient;
            }                    


            public void ScheduleJobs(List<ReminderTime> reminderTimeList)
            {
                  foreach (var reminder in reminderTimeList)
                  {
                        _recurringJobClient.AddOrUpdate<IProactiveMessageService>(
                                // Job Id
                                $"send-status-set-reminder-{reminder.Day.Substring(0, 3)}- 
                                {reminder.StartTime.Split(":")[0]}-{reminder.StartTime.Split(":")[1]}",
                                // Service to run work
                                c => c.SendStatusSetReminder(),
                                // Repeat weekly
                                GetWeekCronExpression(reminder.Day, reminder.StartTime),
                                // Use local time
                                TimeZoneInfo.Local);
                  }
           private DayOfWeek GetDayOfTheWeek(string day)
           {
            switch (day)
            {
                case "Monday":
                    return DayOfWeek.Monday;

                case "Tuesday":
                    return DayOfWeek.Tuesday;

                case "Wednesday":
                    return DayOfWeek.Wednesday;

                case "Thursday":
                    return DayOfWeek.Thursday;

                case "Friday":
                    return DayOfWeek.Friday;

                default:
                    return DayOfWeek.Monday;
            }
        }

        private string GetWeekCronExpression(string day, string time)
        {
            var hour = Convert.ToInt16(time.Split(":")[0]);
            var minute = Convert.ToInt16(time.Split(":")[1]);
            var weekStartExp = Cron.Weekly(GetDayOfTheWeek(day), hour, minute);

            return weekStartExp;
        }
}



    }

My guess is theres something I'm fundamentally not understanding or theres a bug with Hangfire. In any case I'm happy to just get a work around.

Thanks

Mbuso Mkhize
  • 89
  • 1
  • 4

1 Answers1

1

Maybe there is a timezone problem and your hour:minute pair is slightly in the past. I just created a recurring job with manual parameters of Cron.Weekly() and in the dashboard it says, it will be perform in two hours (as expected).

Maybe you should hover over the string In 7 days on the dashboard, then you'll see the exact time. Check if it is your expected time and check with the debugger what comes within the StartTime property in your reminder object.

If all of these don't help, would be my last rescue to also add an ordinary job by calling jobClient.Enqueue() with the desired time.

Oliver
  • 43,366
  • 8
  • 94
  • 151