Check if HangFire.JobStorage is created

I have an ASP.NET MVC application that acts like a Hangfire client - it creates various jobs. I am using SqlServerJobStorage

Hangfire to use.

At the moment I am working on a scenario where there is no connection to Hangfire Database and somewhere in the future a connection is created.

The goal is that my ASP.NET MVC application should work up to this point. After the connection is restored, he should start setting tasks.

Thus, the application will throw an exception at Global.asax

:

Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("EshopServiceHangfire");

      

In addition, all job queues also throw exceptions:

 BackgroundJob.Enqueue<ISomeClass>(x => x.DoSomethingGreat(JobCancellationToken.Null));

      

I put the line from Global.asax

in a try / catch block so it won't throw. When someone runs the job, I want to check JobStorage.Current

, and if it is not initialized, I will try to run it again with the same code:

Hangfire.GlobalConfiguration.Configuration.UseSqlServerStorage("EshopServiceHangfire");

      

Does anyone know how to do this? The documentation has no information on this.

Something like Hangfire.GlobalConfiguration.JobStorage.IsInitialized

?

Throwing an exception from a Jobqueque is also the way to go, but I don't like it because it throws non-specific

InvalidOperationException: The value of the JobStorage.Current property has not been initialized. You must install it before using Hangfire Client or Server API.

Many thanks to those who read this place)

+3


source to share


1 answer


You can use a static property Hangfire.JobStorage.Current

to check your Hangfire storage configuration:

//InvalidOperationException " JobStorage.Current property value has not been initialized"
var storage = JobStorage.Current;

GlobalConfiguration.Configuration.UsePostgreSqlStorage(vaildConnString);

//no exception
storage = JobStorage.Current;

      

I suppose the processing InvalidOperationException

instead of something seems DbException

to be okay because the Hangfire core is isolated from the details of a particular database.



Alternatively, you can query the database to test the connection:

JobStorage.Current.GetConnection().GetRecurringJobs();

      

+1


source







All Articles