Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep history of jobs executed for more than 1 day in Hangfire

Tags:

hangfire

I've just started using Hangfire, and I am loving it.

I understand that Hangfire maintains the history for succeeded jobs for 1 day, and clear it thereafter.

Is there is a way where I can customize this default behavior and persist the history for any duration say 7 days?

like image 495
Yogi Avatar asked Dec 24 '15 10:12

Yogi


People also ask

Where does hangfire store recurring jobs?

Where does HangFire store recurring jobs? Persistent. Background jobs are created in a persistent storage – SQL Server and Redis supported officially, and a lot of other community-driven storages. You can safely restart your application and use Hangfire with ASP.NET without worrying about application pool recycles.

What hangfire object and method is used to create a recurring job?

The call to AddOrUpdate method will create a new recurring job or update existing job with the same identifier.

What is Cron expression in hangfire?

cron | CRON expression A special component in Hangfire Server (see Processing background jobs) checks the recurring jobs on a minute-based interval and then enqueues them as fire-and-forget jobs.

What to use Hangfire for?

Hangfire is an open-source framework that can be used to perform background processing in . Net and . Net Core applications. It is mainly used to perform background tasks such as batch/email notification, batch import of files, video/image processing, database maintaining, file purging, etc.


1 Answers

To do this, you need to create a job filter and register it through hangfire global configurations, as discussed here - https://discuss.hangfire.io/t/how-to-configure-the-retention-time-of-job/34

Create job filter -

using Hangfire.Common;
using Hangfire.States;
using Hangfire.Storage;
using System;

namespace HangfireDemo
{
    public class ProlongExpirationTimeAttribute : JobFilterAttribute, IApplyStateFilter
    {
        public void OnStateApplied(ApplyStateContext filterContext, IWriteOnlyTransaction transaction)
        {
            filterContext.JobExpirationTimeout = TimeSpan.FromDays(7);
        }

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

...and register in global job filters -

GlobalJobFilters.Filters.Add(new ProlongExpirationTimeAttribute());
like image 82
Yogi Avatar answered Oct 13 '22 11:10

Yogi