Search code examples
c#pleskhangfire

Hangfire recurring Job in Plesk website not working


I have the following setup in my C# MVC project, in the Global.asax.cs file:

 ////hangfire config
 GlobalConfiguration.Configuration
     .UseSqlServerStorage("ContextName", new SqlServerStorageOptions
     {
         CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
         SlidingInvisibilityTimeout = TimeSpan.FromMinutes(15),
         QueuePollInterval = TimeSpan.Zero,
         UseRecommendedIsolationLevel = true,
         UsePageLocksOnDequeue = true,
         DisableGlobalLocks = true
     })
     .UseRecommendedSerializerSettings();
 //.UseLog4NetLogProvider(); // this creates a bunch of logs that inflates the DB, only for testing purposes!

and this code which I tried tweaking around but I can't get it to work in production, locally it works fine.

BackgroundJobServer backgroundJobServer = new BackgroundJobServer();
RecurringJob.AddOrUpdate(() => MethodCalling(DateTime parameter), "0 8 * * *");

This does work Local but not when I publish this to my Plesk environment. I tried as well with this:

RecurringJob.Trigger("Identifier here");

and of course having set the Identifier in the AddOrUpdate method but it triggers each time the site is browsed I think. I want it to run every day of the year at 08:00 AM.

Tried chaning the time:

"00 * * * *" set to run every hour, every day every month or even with the obsolete Cron.HourInterval(int) but it seems to be triggered whenever I browse the site or +-3 times a day if I don't browse it, seems like maybe the IIS recycling cycle maybe?

I also tried changing it to this:

BackgroundJob.Enqueue(()=> MethodCalling(DateTime parameter));

When I check my database (using EF) I can clearly see that it runs but something is not configured properly but I don't know what.

ANY suggestions are welcome! Thank you! (NOTE: not using .NET Core)


Solution

  • First, let's rephrase the issue based on the discussion above. You want a daily trigger to initialize some tasks, and would like to know what can be the options when your solution is deployed to a shared hosting platform.

    IIS/ASP.NET/ASP.NET Core is not designed for such tasks, and on a shared hosting platform you won't be able to trigger daily jobs without hurdles.

    Therefore, the best option is to find an external resource (like Zapier) as the daily trigger, and let it call your web app to initialize the jobs.