2 years ago

#157449

test-img

Tumo Maraisane

Hangfire Background Job not triggering

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.

asp.net-core

hangfire

ef-core-5.0

hangfire-sql

0 Answers

Your Answer

Accepted video resources