Yep, you got is right. It would be something like:
public class DateIntervalAttribute : JobFilterAttribute, IClientFilter, IServerFilter
{
private readonly DateTime? _notBefore, _notAfter;
public DateIntervalAttribute(string notBefore = null, string notAfter = null)
{
_notBefore = string.IsNullOrEmpty(notBefore) ? (DateTime?)null : DateTime.Parse(notBefore, CultureInfo.InvariantCulture);
_notAfter = string.IsNullOrEmpty(notAfter) ? (DateTime?)null : DateTime.Parse(notAfter, CultureInfo.InvariantCulture);
}
public void OnCreating(CreatingContext filterContext)
{
if (_notBefore.HasValue && _notBefore.Value > DateTime.Now)
{
filterContext.Canceled = true;
}
else if (_notAfter.HasValue && _notAfter.Value < DateTime.Now)
{
filterContext.Canceled = true;
// delete recurring job definition after expiration
if (filterContext.Parameters.HasKey("RecurringJobId"))
{
string recurringJobId = (string) filterContext.Parameters["RecurringJobId"];
RecurringJob.RemoveIfExists(recurringJobId);
}
}
}
public void OnCreated(CreatedContext filterContext)
{
}
public void OnPerforming(PerformingContext filterContext)
{
if (_notBefore.HasValue && _notBefore.Value > DateTime.Now)
{
filterContext.Canceled = true;
}
else if (_notAfter.HasValue && _notAfter.Value < DateTime.Now)
{
filterContext.Canceled = true;
}
}
public void OnPerformed(PerformedContext filterContext)
{
}
}
Then just apply this attribute to your job method:
[DateInterval("2016-12-01", "2016-12-25")]
public void JobMethod()
{
// do work
}