How can we modify the batches expiration time?

I’m using Hangfire Pro and Redis as storage backend. I see the new release 1.4.6 blog post and it’s great.
But there is a point that it’s not clear for me: How can I modify the batches expiration time?
Can I use a filter like this How to configure the retention time of job? ?

Thanks & Regards,
Massimo

Currently there are two ways, but both of them are hacky:

  1. Change a value of the internal field Hangfire.Batches.States.ApplyBatchStateContext.DefaultBatchExpirationTime (TimeSpan) using reflection.
  2. Download the source code, change the value and switch the assemblies.

I’m planning to make batch filters public in the next Hangfire.Pro 1.5.0 version. They will behave like job filters, and you’ll be able to change some aspects of batch processing, including their expiration time. I will touch this topic once there is a build with this feature in the Private CI NuGet feed.

Hi @odinserj,

Are you able to show an example with the latest Hangfire Pro to use filters with batching? No matter what I do it does not honor the expiration filter. The same code works fine without batching.

Thanks

Hi @sumeetg, currently you can use the following method to configure expiration time for batch and its jobs:

private static bool SetBatchExpirationTime(TimeSpan expirationTime)
{
    var type = Type.GetType("Hangfire.Batches.States.ApplyBatchStateContext, Hangfire.Pro", throwOnError: false);
    if (type == null) return false;

    var field = type.GetField("DefaultBatchExpirationTime", BindingFlags.Static | BindingFlags.NonPublic);
    if (field == null) return false;

    field.SetValue(null, expirationTime);
    return true;
}

Thx, is there any plan to change this to use the filtering? I love the filtering since I can control it on every job with just a simple attribute. We are using the latest of Core and Pro.

Even if I make filters for batches available for everyone, it will be hard to differentiate one batch from another. The only option available is to use their descriptions, but it will be very strange. Currently I consider to have a global setting for batches, like IGlobalConfiguration.UseBatches(TimeSpan expirationTime).

Ok got it. Just tested with the code you provided and it does indeed work. Also wanted to say great work on this product, really loving the batching.

How and where do i do this? Can you document this?