Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use multiple instance of hangfire with single database

Tags:

hangfire

Has anyone used multiple instances of Hangfire (in different applications) with same SQL DB for configuration. So instead of creating new SQL DB for each hangfire instance i would like to share same DB with multiple instances.

As per the hangfire documentation here it is supported since v1.5 However forum discussion here and here shows we still have issues running multiple instances with same db

Update 1
So based on suggestions and documentation i configired hangfire to use queue

   public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
   ILoggerFactory loggerFactory)
   {
        app.UseHangfireServer(new BackgroundJobServerOptions()
        {
            Queues = new string[] { "instance1" }
        });
   }

Method to invoke

[Queue("instance1")]
public async Task Start(int requestID)
{

}

This is how i Enqueue job

 _backGroundJobClient.Enqueue<IPrepareService>(x => x.Start(request.ID));

however when i check [JobQueue] table the new job has queue name default and because of that hangfire will never pickup that job because it picks up jobs for queues.

I think is a bug

Update 2

Found one more thing. I am using instance of IBackgroundJobClient. The instance is automatically get injected by .Net Core's inbuilt container.

So if i use instance to enqueue the job then hangfire creates new job with default queue name

 _backGroundJobClient.Enqueue<IPrepareService>(x => x.Start(request.ID));

However if i use static method, then hangfire creates new job with configured queue name instance1

  BackgroundJob.Enqueue<IPrepareService>(x => x.Start(prepareRequest.ID));

How do i configure hangfire in .Net Core so the instance of IBackgroundJobClient will use configure queue name ?

like image 838
LP13 Avatar asked Jun 29 '17 17:06

LP13


People also ask

Is Hangfire multithreaded?

The Hangfire Server uses multiple threads to perform background jobs. Meaning it can process a background job per thread within the Hangfire server. This allows you to execute background jobs concurrently. By default, the number of threads it uses is 5 per Processor Count.

What can be used to create complex Hangfire workflows?

Batch jobs will help to build more complex workflows with Hangfire. They will give you the power of parallel processing and continuations, you can look at this code snippet: BatchJob . Create(x => { x.

What do you use Hangfire for?

Hangfire is an open-source framework that can be used to perform background processing in . Net and . Net Core applications. It is mainly used to perform background tasks such as batch/email notification, batch import of files, video/image processing, database maintaining, file purging, etc.

How does Hangfire Server work?

Hangfire Server consists of different components that are doing different work: workers listen to queue and process jobs, recurring scheduler enqueues recurring jobs, schedule poller enqueues delayed jobs, expire manager removes obsolete jobs and keeps the storage as clean as possible, etc.


1 Answers

This is possible by simply setting the SQL server options with a different schema name for each instance.

Instance 1:

configuration.UseSqlServerStorage(
    configuration.GetConnectionString("Hangfire"),
    new SqlServerStorageOptions { SchemaName = "PrefixOne" }
);

Instance 2:

configuration.UseSqlServerStorage(
    configuration.GetConnectionString("Hangfire"),
    new SqlServerStorageOptions { SchemaName = "PrefixTwo" }
);

Both instances use same connection string and will create two instances of all the required tables with the prefix specified in the settings.

Queues are used for having separate queues in the same Hangfire instance. If you want to use different queues you'll need to specify the queues you want the IBackgroundJobClient to listen to and then specify that queue when creating jobs. This doesn't sound like what you're trying to accomplish.

like image 92
playsted Avatar answered Sep 29 '22 08:09

playsted