Thanks @pieceofsummer for the very useful example here.
I am trying to do something similar, but I would like to be able to specify a different notBefore and notAfter for each recurring job I set up. My initial idea was to handle this within the job method itself, passing the dates in as arguments to this method.
This would look something like this:
public void JobMethod(DateTime? notBefore, DateTime? notAfter, PerformContext context)
if (notBefore.HasValue && notBefore.Value > DateTime.Now)
if (notAfter.HasValue && notAfter.Value < DateTime.Now)
// Can we get the recurringJobId at this point so we can remove the job?
// do work
The code that creates the recurring job would then look something like this:
string recurringJobId = Guid.NewGuid().ToString();
DateTime? notBefore = new DateTime(2021, 1, 1);
DateTime? notAfter = new DateTime(2022, 1, 1);
string cronExpression = "0 9 1 * *";
j => j.JobMethod(notBefore, notAfter, null),
However, I can’t see how to obtain the RecurringJobId from within the job method. Is it possible to get this from PerformContext?
I could just pass this in to JobMethod as another argument, but it would be cleaner to obtain this from the context.
More fundamentally, do you see any other issues with removing the recurring job from within the job method itself, rather than one of the interception methods such as OnCreating?
Alternatively, when using a job filter like your example, is there a way of specifying notBefore and notAfter for each recurring job, as this seems a much cleaner solution.
Many thanks in advance.