10

I am using Hangfire to do jobs, and I'd like to change the behaviour that succeeded jobs are deleted from the database after a day - I'd like them to be stored for a year.

Following the instructions in this thread, which is the same as in this SO question, I have created a class:

public class OneYearExpirationTimeAttribute : JobFilterAttribute, IApplyStateFilter
{
    public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
    {
        context.JobExpirationTimeout = TimeSpan.FromDays(365);
    }

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

and I register it in my Asp.net web api startup class as a global filter:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        // ... other stuff here ...
        GlobalJobFilters.Filters.Add(new OneYearExpirationTimeAttribute());
        GlobalConfiguration.Configuration.UseSqlServerStorage("HangFireDBConnection");
        app.UseHangfireDashboard();
    }
}

The web api is the place where jobs are posted (i.e., the call to BackgroundJob.Enqueue(() => ...) happens). I have not changed the configuration of the clients that do the actual jobs.

If I now post a job and it succeeds, it still has a expiry of one day as you can see in the screenshot, which shows both the dashboard and the entry in the HangfireDb,

enter image description here

What am I doing wrong or what am I missing?

Community
  • 1
  • 1
EluciusFTW
  • 2,565
  • 6
  • 42
  • 59
  • take a look at the last answer https://discuss.hangfire.io/t/how-to-configure-the-retention-time-of-job/34/11 you should configure `SQLServerStorage` class – Alex Feb 27 '17 at 12:39
  • Possible duplicate of [Keep history of jobs executed for more than 1 day in Hangfire](http://stackoverflow.com/questions/34450884/keep-history-of-jobs-executed-for-more-than-1-day-in-hangfire) – Alex Feb 27 '17 at 12:40
  • 1
    @Alex this is not a duplicate. The OP's implementation is similar to the solution from that question but is still having problems. – tom redfern Feb 27 '17 at 12:53
  • 3
    @Alex adding the configuration in the `SQLServerStorage` class as shown in the link, `JobExpirationCheckInterval = TimeSpan.FromMinutes(1)`, only changes the interval for checking the job expiration, not the expiration itself, IMO. – EluciusFTW Feb 27 '17 at 13:21
  • Have you tried adding the attribute directly as an attribute instead of a global filter (just to test)? `[OneYearExpirationTime]public void SomeMethod()` – GôTô Feb 27 '17 at 15:47
  • Do you have a multi server setup? I find it really surprising that it doesn't work, it should be pretty straightforward – GôTô Feb 27 '17 at 15:55
  • 1
    @GôTô Yes, two servers. I just applied the attributes everywhere, job is runnng, will know in 5 mins ... – EluciusFTW Feb 27 '17 at 15:56

2 Answers2

6

My mistake in setup was that the attribute was set on the wrong application. As I stated in the question, I added the filter in the startup.cs file of the asp.net web api where jobs are posted.

Instead I should have added the configuration in the Console application where the jobs are being executed, i.e., my console app starts with

static void Main(string[] args)
{
    GlobalConfiguration.Configuration.UseSqlServerStorage("HangFireDBConnection");
    GlobalJobFilters.Filters.Add(new OneYearExpirationTimeAttribute());
    // ... more stuff ...
}

Then it works. The Hangfire documentation could be a bit clearer on where the filter should be configured.

EluciusFTW
  • 2,565
  • 6
  • 42
  • 59
2

Using version:

// Type: Hangfire.JobStorage
// Assembly: Hangfire.Core, Version=1.7.11.0, Culture=neutral, PublicKeyToken=null

This can be done directly (apparently)

JobStorage.Current.JobExpirationTimeout = TimeSpan.FromDays(6 * 7);
Pang
  • 9,564
  • 146
  • 81
  • 122
yBother
  • 648
  • 6
  • 25
  • 6
    Or equivalently, use `.WithJobExpirationTimeout()` as stated [in the doc here](https://docs.hangfire.io/en/latest/background-methods/index.html#states). – Pang Jun 10 '21 at 07:23