Tumo Maraisane
Tumo Maraisane

Reputation: 105

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.

Upvotes: 1

Views: 1936

Answers (1)

Ruslan
Ruslan

Reputation: 11

Possibly you have more than one Hangfire server and background job can be processed on another server. Open Hangfire dashboard and check in succeeded jobs where your enqueued job processing.Succeeded job

Upvotes: 1

Related Questions