[Solved] RecurringJob.AddOrUpdate Execute immediately instead on cronjob, after deployment

recurring
Tags: #<Tag:0x00007f78328c4dd8>

#1

Hangfire Version: 1.6.20
Environment: ASP.net WebAPI

Use Case
I’ve got 15 Recurring Jobs but based on deployment not all of them are necessary.
So I’ve wrote something which reads the web.config and iterates trough “isEnabled” and based on it it’s do “AddOrUpdate” or “RemoveIfExists”

The Issue
It seems all recurring jobs which have a schedule within the day, will be immediately executed.
I did publish today (that’s why 2 Hours ago on Screenshot) on a server. Some Task which are scheduled for 22:00 where executed immediately. The Schedules for Monday or Weekly where not executed.

Did someone experiences this kind of behavior or do I have a mistake in my code?
Thanks in advance for any ideas, insights and thoughts about it.

** Screenshot*

The Code

var manager = new RecurringJobManager();
var hangfireSchedules = ConfigurationManager.AppSettings.AllKeys
                             .Where(key => key.StartsWith("HangFireEnable"))
                             .ToList();

Parallel.ForEach(hangfireSchedules, scheduleItem => 
{
	try
	{
		var jobName = scheduleItem.Replace("HangFireEnable", string.Empty);
		var hangFireSchedule = $"{jobName}Schedule";

		if (bool.Parse(ConfigurationManager.AppSettings[scheduleItem]))
		{
			var scheduleInfo = ConfigurationManager.AppSettings[hangFireSchedule];
			if (!string.IsNullOrWhiteSpace(scheduleInfo))
			{
				var method = typeof(HangfireMethods).GetMethod(jobName);
				List<object> parameters = new List<object>();
				foreach (var param in method.GetParameters())
				{
					parameters.Add(param.DefaultValue);
				}

				Job job = null;
				if (parameters.Any())
				{
					job = new Job(typeof(HangfireMethods), typeof(HangfireMethods).GetMethod(jobName), parameters.ToArray());
				}
				else
				{
					job = new Job(typeof(HangfireMethods), typeof(HangfireMethods).GetMethod(jobName));
				}

				manager.AddOrUpdate($"HangfireMethods.{jobName}", job, scheduleInfo);
			}
		}
		else
		{
			manager.RemoveIfExists($"HangfireMethods.{jobName}");
		}
	}
	catch (Exception ex)
	{
		// Logs
	}
});

#2

The Problem was much simpler as expected: Timezone.

The previous Hangfire recurring jobs where within “Western Europe” and the new code was with UTC Timezone. Therefore Hangfire believed all the “daily” jobs where overdue and executed them. So I’ve mixed the timezone which cause the issue.

After modified
manager.AddOrUpdate($"HangfireMethods.{jobName}", job, scheduleInfo);

to

var timeZone = TimeZoneInfo.FindSystemTimeZoneById(&quot;W. Europe Standard Time&quot;);
manager.AddOrUpdate($&quot;HangfireMethods.{jobName}&quot;, job, scheduleInfo, new RecurringJobOptions { TimeZone = timeZone });

the problem was solved