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
}
});