I had a ProlongExpirationTimeAttribute set and working in my ASP.NET Web API 2 application, however ever since I followed the steps outlined for how to make sure the ASP.NET site runs 24/7 this has stopped working. Does anybody know how to set this option when you are starting hangfire via HangfireBootstrapper?
http://docs.hangfire.io/en/latest/deployment-to-production/making-aspnet-app-always-running.html
I have tried:
Hangfire.GlobalConfiguration.Configuration
.UseSqlServerStorage("Hangfire");
Hangfire.GlobalConfiguration.Configuration.UseFilter(new ProlongExpirationTimeAttribute());
AND
Hangfire.GlobalJobFilters.Filters.Add(new ProlongExpirationTimeAttribute());
WITH
public class ProlongExpirationTimeAttribute : JobFilterAttribute, IApplyStateFilter
{
public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
context.JobExpirationTimeout = TimeSpan.FromDays(7);
}
public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
context.JobExpirationTimeout = TimeSpan.FromDays(14);
}
}
I also tried appending it to the “UseSqlServerStorage” configuration in the Start() method.
public void Start()
{
lock (_lockObject)
{
if (_started) return;
_started = true;
HostingEnvironment.RegisterObject(this);
Hangfire.GlobalConfiguration.Configuration
.UseSqlServerStorage("Hangfire").UseFilter(new ProlongExpirationTimeAttribute());
_backgroundJobServer = new BackgroundJobServer();
}
}
It is still set to 1 day. The only way I have been able to achieve this with the always running method is by building my own version of hangfire.core and changing this section of ApplyStateContext.cs
public ApplyStateContext(
[NotNull] JobStorage storage,
[NotNull] IStorageConnection connection,
[NotNull] IWriteOnlyTransaction transaction,
[NotNull] BackgroundJob backgroundJob,
[NotNull] IState newState,
[CanBeNull] string oldStateName)
{
if (storage == null) throw new ArgumentNullException(nameof(storage));
if (connection == null) throw new ArgumentNullException(nameof(connection));
if (transaction == null) throw new ArgumentNullException(nameof(transaction));
if (backgroundJob == null) throw new ArgumentNullException(nameof(backgroundJob));
if (newState == null) throw new ArgumentNullException(nameof(newState));
BackgroundJob = backgroundJob;
Storage = storage;
Connection = connection;
Transaction = transaction;
OldStateName = oldStateName;
NewState = newState;
JobExpirationTimeout = TimeSpan.FromDays(7);
}