Hangfire.MemoryStorage icon indicating copy to clipboard operation
Hangfire.MemoryStorage copied to clipboard

How to clean up the completed job regularly

Open davidchencloudexceed opened this issue 3 years ago • 4 comments

Hello, I have a problem regarding cleanning up the completed job. I tried to use the attribute to define the expiration/life time.

public class HangfireJobRentionAttribute : JobFilterAttribute, IApplyStateFilter
 {
     public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
     {
         context.JobExpirationTimeout = TimeSpan.FromMinutes(1);
     }

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

After apply the attribute, I do see the hangfire pick it up in my dashboard. It said that it should be auto delted 28 min ago. Capture

However, I don't see the reduction of the memory footprint of my hangfire application. It keeps going up.

Does MemoryStorage has some build in clean up mechanism like SQL?

https://discuss.hangfire.io/t/does-hangfire-clear-down-old-job-data/2294/2

public SqlServerStorageOptions()
{
    TransactionIsolationLevel = null;
    QueuePollInterval = TimeSpan.FromSeconds(15);
    SlidingInvisibilityTimeout = null;
#pragma warning disable 618
    InvisibilityTimeout = TimeSpan.FromMinutes(30);
#pragma warning restore 618
    JobExpirationCheckInterval = TimeSpan.FromMinutes(30);
    CountersAggregateInterval = TimeSpan.FromMinutes(5);
    PrepareSchemaIfNecessary = true;
    DashboardJobListLimit = 10000;
    _schemaName = Constants.DefaultSchema;
    TransactionTimeout = TimeSpan.FromMinutes(1);
}

davidchencloudexceed avatar May 24 '21 00:05 davidchencloudexceed

Hi,

The HangfireJobRentionAttribute is not used. The ExpirationManager uses the ExpireAt (IExpirable interface) property of the job.

perrich avatar May 24 '21 07:05 perrich

thank you @perrich. How to set the job's ExpireAt property? I try to set the jobexpireationtimeout at the globalconfiguraiton. (I have to download the src code of hangfire and change the source to remove the 1 hour limit) However, I don't see InMemory storage will honor the change as my memory consumption same as before.

Hangfire.GlobalConfiguration.Configuration
                .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                .UseMemoryStorage(new MemoryStorageOptions
                {
                     JobExpirationCheckInterval = TimeSpan.FromMinutes(1)
                })
                .**WithJobExpirationTimeout(TimeSpan.FromMinutes(1))**
                .UseAutofacActivator(container);

davidchencloudexceed avatar May 24 '21 13:05 davidchencloudexceed

find out why. InMemory did honor the setting. It is the one by one for loop detele slow the clean up.

davidchencloudexceed avatar May 24 '21 13:05 davidchencloudexceed

The loop is not done one by one but by 1000 (of one of the managed type).

perrich avatar May 24 '21 15:05 perrich