I have couple of jobs scheduled to run every 5 mins in Hangfire backed by PostgreSql. I see that when SQL server is used the successful jobs are automatically deleted from DB. I don't find anything similar in PostGreStorageOptions. Any idea how to setup automatic delete of successful jobs in postgresql ?
Asked
Active
Viewed 2,565 times
1 Answers
0
See link: https://discuss.hangfire.io/t/how-to-configure-the-retention-time-of-job/34 I wrote a custom class such as:
public class AutoDeleteAfterSuccessAttribute : JobFilterAttribute, IApplyStateFilter
{
private readonly TimeSpan _deleteAfter;
public AutoDeleteAfterSuccessAttribute(int hours, int minutes, int seconds)
{
_deleteAfter = new TimeSpan(hours, minutes, seconds);
}
public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
context.JobExpirationTimeout = _deleteAfter;
}
public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
}
}

Oğuzhan Kahyaoğlu
- 1,727
- 2
- 15
- 20
-
While this works, you will have to add the attribute to every single job. It does make sense to configure retention time differently for different jobs. but if you want to configure a global retention policy, you may want to do this via storage parameters. – Florian Winter Feb 17 '23 at 16:27
-
Correction to what I said above: You cannot set expiration time via storage parameters (but you can reduce the interval at which the SQL server storage provider checks for expired jobs). – Florian Winter Feb 17 '23 at 16:31
-
Also, you can use `GlobalJobFilters.Filters` to apply the filter to all jobs. – Florian Winter Feb 17 '23 at 16:31