Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when creating a new job from existing job 1.7.28 #2003

Closed
half-naan opened this issue Feb 24, 2022 · 3 comments
Closed

Error when creating a new job from existing job 1.7.28 #2003

half-naan opened this issue Feb 24, 2022 · 3 comments

Comments

@half-naan
Copy link

half-naan commented Feb 24, 2022

I am trying to resolve an issue where all the jobs are executed after server reboot. It is not desired behavior for us given that the timing of jobs is very important. I stumbled across this post and tried to re-create the jobs at startup. Unfortunately, I am getting an error message with 1.7.28 that the method params and the argument params do not match. Even though they match, please see the attached screenshot.

Thanks!

using (var connection = JobStorage.Current.GetConnection())
{
	foreach (var recurringJob in connection.GetRecurringJobs())
	{
		var jobTimeZone = TimeZoneInfo.FindSystemTimeZoneById(recurringJob.TimeZoneId) ?? TimeZoneInfo.Utc;
		var name = recurringJob.Id;

		RecurringJob.RemoveIfExists(recurringJob.Id);

		var manager = new RecurringJobManager();
		var methodParameters = recurringJob.Job.Method.GetParameters().Length;
		var jobArgs = recurringJob.Job.Args.Count;
		var job = new Hangfire.Common.Job(recurringJob.Job.Type, recurringJob.Job.Method, recurringJob.Job.Args);

		manager.AddOrUpdate(name, job, recurringJob.Cron, jobTimeZone, recurringJob.Queue);
	}
}

image

@alcalin
Copy link

alcalin commented Dec 5, 2024

Hi, this worked for me:

public class SkipMissedRecurringJobsAttribute : JobFilterAttribute, IServerFilter
{
    public void OnPerforming(PerformingContext filterContext)
    {
        var jobData = filterContext.Connection.GetJobData(filterContext.BackgroundJob.Id);
        if (jobData.CreatedAt < DateTime.UtcNow.AddMinutes(-5))
        {
            filterContext.Canceled = true; 
        }
    }

    public void OnPerformed(PerformedContext filterContext) { }
}

@odinserj
Copy link
Member

odinserj commented Dec 6, 2024

Omg, thanks for pinging, this should be fixed. The problem is that the Job's constructor overload takes params object[] parameter for args, so when array is passed, it's considered as the first argument, resulting in the exception. Another constructor overload should be added that uses the object[] parameter instead for this use case.

@odinserj
Copy link
Member

odinserj commented Dec 9, 2024

The following line in the original question:

var job = new Hangfire.Common.Job(recurringJob.Job.Type, recurringJob.Job.Method, recurringJob.Job.Args);

Should be changed wit the ToArray call:

var job = new Hangfire.Common.Job(recurringJob.Job.Type, recurringJob.Job.Method, recurringJob.Job.Args.ToArray());

That Job constructor overload takes params object[] as the last argument, while the Args property returns IReadOnlyList<object>. So the whole list is passed as a single argument, due to the type mismatch.

I think the best way to handle this inconsistency would be to change the parameter type of that Job constructor to params IReadOnlyList<object>, since the newest C# compilers allow doing this. However, this is a breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants