netaholic
netaholic

Reputation: 1385

Remove a hangfire job without instantiating a hangfire server

I have 2 separate apps. One uses HangFire and schedules a recurring job. Another one contains tests. Tests are integrational, so they reuse actual pathways without mocking anything. Both apps have an access to the same db

As a result of running a test a recurring job appears, created by the first app, and it is desirable. However since the test is integrational - it is run on the production DB, so after the test is done I would like to remove created recurring job. And I would like to handle removing in a test.

I'd like to know if I can configure hangfire classes in the second app in a way, that would allow me to remove a recurring job, created by the first app.

If I simply configure a hangfire to use the same SQL server in the second app then a new HangFireServer will be instantiated that will start "grabbing" jobs for execution.

I can run an SQL query to remove the job, but I'd like to avoid that.

I hope I explained myself clear enough.

Upvotes: 1

Views: 3728

Answers (2)

jtabuloc
jtabuloc

Reputation: 2535

In Hangfire you can Remove/Add Job whether it is recurringjob or background job as long as you populate JobStorage first.

There probably two way to set this up.

JobStorage.Current = new SqlServerStorage("Your ConnectionString");; 

or

GlobalConfiguration.Configuration.UseSqlServerStorage("Your ConnectionString");

Then you can now use your RecurringJob.RemoveIfExists("id") and no need to instantiate Hangfire server. Assuming that you already have created the Hangfire Scheme before hand in other application.

Upvotes: 2

derkesthai
derkesthai

Reputation: 21

Assuming that you created your recurring job with an id you can just do RecurringJob.RemoveIfExists("id"), as stated on the documentation page: http://docs.hangfire.io/en/latest/background-methods/performing-recurrent-tasks.html

Upvotes: 1

Related Questions