设置Hangfire成功作业到期属性不起作用 [英] Set Hangfire succeeded job expiry attribute not working
问题描述
我正在使用Hangfire来执行作业,并且我想更改一天后从数据库中删除成功的作业的行为-我希望将它们存储一年.
I am using Hangfire to do jobs, and I'd like to change the behaviour that succeeded jobs are deleted from the database after a day - I'd like them to be stored for a year.
按照此中的说明进行操作线程,与
Following the instructions in this thread, which is the same as in this SO question, I have created a class:
public class OneYearExpirationTimeAttribute : JobFilterAttribute, IApplyStateFilter
{
public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
context.JobExpirationTimeout = TimeSpan.FromDays(365);
}
public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
context.JobExpirationTimeout = TimeSpan.FromDays(365);
}
}
并将其注册到我的Asp.net Web API启动类中作为全局过滤器:
and I register it in my Asp.net web api startup class as a global filter:
public class Startup
{
public void Configuration(IAppBuilder app)
{
// ... other stuff here ...
GlobalJobFilters.Filters.Add(new OneYearExpirationTimeAttribute());
GlobalConfiguration.Configuration.UseSqlServerStorage("HangFireDBConnection");
app.UseHangfireDashboard();
}
}
网络api是发布作业的地方(即,调用BackgroundJob.Enqueue(() => ...)
的地方).我尚未更改执行实际工作的客户端的配置.
The web api is the place where jobs are posted (i.e., the call to BackgroundJob.Enqueue(() => ...)
happens). I have not changed the configuration of the clients that do the actual jobs.
如果我现在发布一项工作并且成功完成,那么它仍然有一天有效期,如您在屏幕快照中所见,该屏幕快照同时显示了仪表板和HangfireDb中的条目,
If I now post a job and it succeeds, it still has a expiry of one day as you can see in the screenshot, which shows both the dashboard and the entry in the HangfireDb,
我做错了什么或我想念什么?
What am I doing wrong or what am I missing?
推荐答案
我的设置错误是该属性是在错误的应用程序上设置的.正如我在问题中所述,我将过滤器添加到了发布工作的asp.net网络api的startup.cs文件中.
My mistake in setup was that the attribute was set on the wrong application. As I stated in the question, I added the filter in the startup.cs file of the asp.net web api where jobs are posted.
相反,我应该在正在执行作业的控制台应用程序中添加配置,即我的控制台应用程序以
Instead I should have added the configuration in the Console application where the jobs are being executed, i.e., my console app starts with
static void Main(string[] args)
{
GlobalConfiguration.Configuration.UseSqlServerStorage("HangFireDBConnection");
GlobalJobFilters.Filters.Add(new OneYearExpirationTimeAttribute());
// ... more stuff ...
}
然后它起作用. Hangfire文档可能会更清楚地说明应在何处配置过滤器.
Then it works. The Hangfire documentation could be a bit clearer on where the filter should be configured.
这篇关于设置Hangfire成功作业到期属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!