1

Hi i have a background job that i initialized in my HangfireController and i want this job to only be triggered once a button is clicked,the job does not fire at and hit my breakpoint inside the method i am callingall and no errors arise and my other recurring jobs work as they should,could i be missing something?Please see code below

Hangfire Controller

public class HangfireController : Controller
{
public HangFireController(my parameters...) // my constructor
{
Int SID = 0;
 RecurringJob.AddOrUpdate("Import Users", () => RunLoadUsers(), "0 0,3,6,9,12,15,18,21 * * *", TimeZoneInfo.Local);

            RecurringJob.AddOrUpdate("Test Example", () => TestData(), "*/15 * * * * *", TimeZoneInfo.Local); //run every 15 sec
            BackgroundJob.Enqueue(()=> RunSaveStatus(SID)); //new job
}  

public void RunSaveStatus(int SID)
{
  //where i placed breakpoint,this point is not reached
  //my logic here
}

}

Home Controller

{
     //my button calls this method when it is clicked
     [HttpPut("UpdateStatus")]
        public IActionResult UpdateStatus(int key, string values)
        {
            var SID = 10;
             BackgroundJob.Enqueue<HangfireController>(x=>x.RunSaveStatus(SID));
        }
}

StartUp.CS where i configure hangfire to initialize

   string hangfireConnectionString = Configuration.GetConnectionString("myConnDB");
            services.AddHangfire(configuration =>
            {
                configuration.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                .UseSimpleAssemblyNameTypeSerializer()
                .UseRecommendedSerializerSettings()
                
                //.UseBatches()
                //.UsePerformanceCounters()
                .UseSqlServerStorage(hangfireConnectionString, new SqlServerStorageOptions
                {
                    CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
                    SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
                    QueuePollInterval = TimeSpan.Zero,
                    UseRecommendedIsolationLevel = true,
                    UsePageLocksOnDequeue = true,
                    DisableGlobalLocks = true
                });
            });

            services.AddHangfireServer();

N.B the two recurring jobs are working perfectly fine,only the Backround.enqueue job does not trigger.

Tumo Maraisane
  • 105
  • 3
  • 11
  • Have you tried [adding the Hangfire dashboard](https://docs.hangfire.io/en/latest/getting-started/aspnet-core-applications.html#adding-dashboard-ui) and checking on the status of it there? – mason Feb 09 '22 at 13:57
  • Try to identify the issue by calling your RunSaveStatus as a static method, or put this methode in a more simple class than a controller with complex constructor logic. Enqueuing and scheduling jobs in the HangfireController constructor which will itself enqueue the job is not a good idea. – jbl Feb 10 '22 at 09:12

0 Answers0