'How to debug hangfire jobs locally while not triggering scheduled jobs
Is there a way to run Hangfire Jobs locally while debugging in Visual Studio while not triggereing any scheduled jobs defined in the database.
For example, I have HangfireJobA and I want to run it one time while in Visual Studio and step through it, however, when debugging, if HangfireJobA is configured in the DB to run every 3 minutes, I don't necessary want that schedule to be triggered.
How can I logically separate the two requirements. I don't want the scheduled jobs to run while I am debugging.
Currently, I have created a #IF(DEBUG) ...#endif and dynamically load all classes that implement an IServiceWorker interface and then select one to instantiate and run it's worker function. This seems hacky and there may be a better way. Any suggestions?
Solution 1:[1]
It's a pretty simplified example, but you'll definitely get the idea. Assuming the following code:
public void AddRecurringJob() => RecurringJob.AddOrUpdate("helloworldjob", HelloWorld, Cron.Daily);
public void HelloWorld() => Console.Write("Hello World!");
What about changing HelloWorld() so something like this:
public void HelloWorld()
{
if (System.Diagnostics.Debugger.IsAttached) return;
Console.Write("Hello World!");
}
Since you're in the debugger, you can step over the if statement manually. Hangfire will pick up your job anyways, but since the debugger is attached, it will do nothing and return immediately.
Solution 2:[2]
The solution being test driven at the moment is:
Require all Developers install SQL Server LocalDB
In appsettings.local.json add the local version of the hangfire db.
"HangfireDB": "Data Source = localhost; Initial Catalog = Hangfire_XXXX; Integrated Security = SSPI;"
In registration, there is a differentiation between debugging locally or running in a target environment.
NOTE : Adding Cron.Never() as the Schedule will place the job in a visible but disabled state. This means the only way to trigger a job is to implicitly click the "Trigger Now", thus invoking the debugger on it.
public static class RegisterServiceWorkers
{
public static void Register(string environment)
{
if (environment.Equals("LOCAL"))
{
RecurringJob.AddOrUpdate<LeaseReportDataBuildupWorker>("LeaseReportDataBuildupWorker_LIFO", x => x.PerformServiceWork("LIFO"), () => Cron.Never());
RecurringJob.AddOrUpdate<LeaseReportDataBuildupWorker>("LeaseReportDataBuildupWorker_FIFO", x => x.PerformServiceWork("FIFO"), () => Cron.Never());
RecurringJob.AddOrUpdate<LeaseReportDataBuildupWorker>("LeaseReportDataBuildupWorker_RANDOM", x => x.PerformServiceWork("RANDOM"), () => Cron.Never());
}
else
{
RecurringJob.AddOrUpdate<LeaseReportDataBuildupWorker>("LeaseReportDataBuildupWorker_FIFO", x => x.PerformServiceWork("LIFO"), () => "*/3 * * * *");
RecurringJob.AddOrUpdate<LeaseReportDataBuildupWorker>("LeaseReportDataBuildupWorker_LIFO", x => x.PerformServiceWork("FIFO"), () => "*/3 * * * *");
RecurringJob.AddOrUpdate<LeaseReportDataBuildupWorker>("LeaseReportDataBuildupWorker_RANDOM", x => x.PerformServiceWork("RANDOM"), () => "*/3 * * * *");
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mu88 |
| Solution 2 | Ross Bush |
