Run a recurring job once with custom arguments

We have a job which runs every 15 minutes, like this:
RecurringJob.AddOrUpdate("MyJob", () => RunJob(), "3/15 * * * *");

In addition to once every 15 minutes, I want to run the job at will, with some custom arguments. Is this possible?

You could simply manually call BackgroundJob.Enqueue(() => RunJob()); in your code when you want it to run.

As for custom arguments, it depends on what kind of argument you want to send. You could pass them when doing the Enqueue.

Yes that would work, but i want to guarantee that this “ad hoc” job run does not run concurrently with the scheduled job. They are not safe to run conchrrently.

For the concurrency you could use the DisableConcurrentExecutionAttribute provided by Hangfire or the SkipConcurrentExecutionAttribute that somebody made.

Make sure to test those yourself to see if they’re what you need depending on your use case.

I think I’m missing something in your answer - I found this, but it won’t let me pass arguments to my job:

var arguments = "Customer123"

Notice the arguments go nowhere!

I’d love this, but it doesn’t seem possible? :

var arguments = "argsargsargs"
RecurringJob.Trigger("MyJob", arguments )

When you added the recurring job, there was no parameter. Where would Hangfire put arguments in your job? Also, how would Hangfire know that the argument of “MyJob” is a string? How would it know how many arguments it should pass to the method?

That’s why, as far as i know, there’s no way to pass arguments to a recurringJob.

From what i understand, you only want arguments when running the job manually. What i suggest, is having BackgroundJob.Enqueue(() => RunJob(arguments)); as an additional endpoint in your api. That way when you need to run the job, you just call your endpoint and it’s going to enqueue the same method and with arguments.

Ah, understood, thanks!