Home > Net >  Old Hangfire servers keep coming back and running old code
Old Hangfire servers keep coming back and running old code

Time:11-02

I'm using Hangfire.AspNetCore version 1.7.25 in my .NET Core 2.2 web application to perform a background job.

I use services.AddHanfire and services.AddHangfireServer settings in my startup.cs ConfigureServices method, along with the app.UseHangfireDashboard in my Configure method.

I have written code in one of my Controllers to allow the user to schedule a Hangfire job by clicking on a button. The code looks similar to the following:

var id = client.Schedule(() => Repository.MyJob(), TimeSpan.FromMinutes(1));

Usually, when the user clicks on the button the MyJob() code is first sent to the Hangfire Enqueue and then almost immediately goes to the Processing mode, and the MyJob() code is executed. However, lately, if I make changes to my code and execute the job by clicking on the button Hangfire ignores the new code and runs an old version of my code as if it's been cached somewhere.

I've also noticed while viewing the servers on the Dashboard that Hangfire keeps creating "extra" or "old" servers along with a new server.

I believe, though I'm not 100% positive, that these old servers are the reason old code is executed. I've written the following code in my Startup.cs to get rid of any existing servers as soon as Hanfire is initialized:

    IMonitoringApi monitoringApi = JobStorage.Current.GetMonitoringApi();
    foreach (var s in monitoringApi.Servers())
    {
        JobStorage.Current.GetConnection().RemoveServer(s.Name);
    }

The code above does seem to work, however, after a few seconds the old servers are automatically back in the servers list in Hangfire Dashboard, and old code is again executed.

I use SQL Storage in my Hangfire settings, and I've dropped all the Hangfire tables in my database prior to executing my application, therefore, allowing Hanfire to recreate them when it's re-added. I've also removed any references to Hanfire by deleting the Hanfire package from my application and rebuilding my code. I've also cleared Chrome's cache files. I've basically tried everything I could find in my Google search regarding this issue, but I haven't been able to get rid of the old servers or to stop Hanfire from executing old code.

I know I can't be the only developer to experience this issue. Has anyone found a solution to this problem? Is there a solution? How can I get rid of those old servers and stop Hanfire from executing old code?

I'd really appreciate any help. Thanks!

CodePudding user response:

Okay, so here's what I did wrong that resulted in me getting "ghost" servers in Hangfire. Basically, I had two instances of my application running and I didn't even realize it. I had deployed my application to our TEST server in Azure which was using an Azure Database. This test app was also set to "always run". Now, when I was testing my changes to the application in my development machine I was also connecting to the same Azure database that the test application was connected to. This is why, even when I truncated the Hangfire tables, the "ghost" servers kept coming back. These servers belonged to the application that was running on the TEST server, and since the application was always running, the servers kept getting recreated, and the old code on the TEST server kept being executed since I had never deployed the changes from my DEV server to the TEST server on Azure.

Basically, it was my own stupid fault.

  • Related