-2

I encountered a serious problem with Hangfire in production, which is happening occasionally but is quite concerning.

I am using Hangfire with .Net Core 2.1

After deploying my Hangfire job, all of my recurring jobs are started automatically which is not what I would expect. Has anyone else noticed this?

I am not able to resolve this trouble, Please help Thanks in advance.

leon.io
  • 2,779
  • 1
  • 18
  • 26
Billy
  • 1
  • 1
  • 1
    Give more detail about your code and startup configurations. – Hadi Samadzad Apr 30 '20 at 02:44
  • this is my startup configuration, l think it's very common`services.AddHangfire(x => { x.UseSqlServerStorage(Configuration.GetConnectionString("Hangfire"), new Hangfire.SqlServer.SqlServerStorageOptions { QueuePollInterval = TimeSpan.FromMinutes(1) }); x.UseLog4NetLogProvider(); x.UseRecurringJob($"recurringjob.{env}.json"); });` – Billy Apr 30 '20 at 07:00
  • this is my Configure to start dashboard,it's also very common `app.UseHangfireServer(new BackgroundJobServerOptions { Queues = new[] { "DEFAULT", "SECONDARY" } }); app.UseHangfireDashboard("/hangfire", new DashboardOptions { Authorization = new[] { new HangfireAuthorizationFilter() } });` – Billy Apr 30 '20 at 07:01
  • I meant to put them into your question so that avoiding your question to be downvoted. Better quality of questions results in faster answers. – Hadi Samadzad Apr 30 '20 at 07:05

1 Answers1

0

In my experience, this is usually because the jobs previously failed so Hangfire automatically retries them when it gets a chance. (You can check if this is the case by looking in your database for jobs in the Failed state.)

There are ways to modify this behavior if desired:

If you don’t want a job to be retried, place an explicit attribute with 0 maximum retry attempts value:

[AutomaticRetry(Attempts = 0)]
public void BackgroundMethod()
{
}

Use the same way to limit the number of attempts to the different value. If you want to change the default global value, add a new global filter:

GlobalJobFilters.Filters.Add(new AutomaticRetryAttribute { Attempts = 5 });

If you are using ASP.NET Core you can use the IServiceCollection extension method AddHangfire. Note that AddHangfire uses the GlobalJobFilter instance and therefore dependencies should be Transient or Singleton.

services.AddHangfire((provider, configuration) =>
{
   configuration.UseFilter(provider.GetRequiredService<AutomaticRetryAttribute>());
}

Reference: Dealing with exceptions

crgolden
  • 4,332
  • 1
  • 22
  • 40
  • Thank you for your suggestion,but l have checked there was no exception when jobs started automatically. – Billy May 06 '20 at 07:31