Job retention configuration does not have any effect


#1

Hello,

We use hangfire 1.6.20 with hangfire pro 2.1.1: both uses net.core runtime

By default jobs has expiration of 7 days and succeeded jobs has “The job is finished. It will be removed automatically in 7 days.” note.

As discussed here:

I can configure the retention in this way:

public class JobExpirationTimeAttribute : JobFilterAttribute, IApplyStateFilter
{
    private readonly int days;
    private readonly int hours;
    private readonly int minutes;
    private readonly int seconds;

    public JobExpirationTimeAttribute(int seconds = 0, int minutes = 0, int hours = 0, int days = 0)
    {
        if (seconds < 0) throw new ArgumentOutOfRangeException(nameof(seconds));
        if (minutes < 0) throw new ArgumentOutOfRangeException(nameof(minutes));
        if (hours < 0) throw new ArgumentOutOfRangeException(nameof(hours));
        if (days < 0) throw new ArgumentOutOfRangeException(nameof(days));
        this.hours = hours;
        this.days = days;
        this.seconds = seconds;
        this.minutes = minutes;
    }

    public TimeSpan TTL
        =>
            TimeSpan.FromSeconds(seconds) + TimeSpan.FromHours(hours) + TimeSpan.FromMinutes(minutes) +
            TimeSpan.FromDays(days);

    public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
    {
        context.JobExpirationTimeout = TTL;
    }

    public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
    {
        context.JobExpirationTimeout = TTL;
    }
}

and apply JobExpirationTimeAttribute(hours: 12)] to job method like this

    [Queue("items")]
    [DisplayName("Refresh item {0} {1}")]
    [AutomaticRetry(Attempts = 2, LogEvents = true, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
    [JobExpirationTimeAttribute(hours: 12)]
    public void ExecuteItem(string[] products)
    {
        foreach (var product in products)
        {
             ....
        }
    }

This configuration does not have any effect

“The job is finished. It will be removed automatically in 7 days.” swill present and jobs are not evicted.

Please let me know how to process. Can it be the issue of net.core version?

Thanks


#2

Looks like that job is created inside a batch. They have different expiration policy to conform to the expiration time of a batch itself. You can configure batch expiration time when calling the UseBatches method:

GlobalConfiguration.Configuration
    .UseBatches(TimeSpan.FromHours(12))

#3

It works as expected now. It can be also useful to improve the related documentation.

Thanks again for help!