Giter Club home page Giter Club logo

hangfire.mamqsqlextension's Introduction

Hangfire.MAMQSqlExtension

A Multi-App, Multi-Queue SqlExtension for running multiple servers for multiple applications against a single SQL Server database.

The problem

An intuitive idea of running multiple Hangfire servers for multiple applications is to run different applications in different queues, a dedicated server for each queue.

For example:

AppServer1 could run jobs defined in library AppLib1 that are registered as recurring jobs in queue AppQueue1. And AppServer2 would run jobs defined in library AppLib2 registered as recurring jobs in queue AppQueue2. And the two server executables would only load assemblies belonging to their applications and can be independent of each other.

Unfortunately, this does not work. Hangfire job queue was initially designed only for job execution, not job scheduling. In the setup above, schedulers in AppServer1 sees jobs in AppQueue2 and will not respect the queue setting. It tries to deserialize the jobs for scheduling. The deserialization will cause a dll not found exception. What's more, when the job fails and is being retried, retries will also not respect the queue setting. Jobs set for AppQueue2 will be retired by AppServer1 and will cause retry failure again.

Solution

This extension is a workaround before Hangfire supports the multiple application, multiple servers, single database setup officially.

Usage

Usage examples are provided in the examples. The necessary steps are listed below.

  1. Install NuGet package Hangfire.MAMQSqlExtension: i.e. Install-Package Hangfire.MAMQSqlExtension -Version 1.0.5

  2. Filter the jobs according to their queue settings before giving them to RecurringJobScheduler and DelayedJobScheduler for scheduling

GlobalConfiguration.Configuration
   .UseMAMQSqlServerStorage(connectionString, new SqlServerStorageOptions{...}, new[]{ "MyQueue" })
  1. Force the job retries to respect the queue setting
[RetryInQueue("MyQueue")]
public void MethodForJob()
{
    ...
}
  1. Specify queues for the BackgroundJobServer.
    var serverOptions = new BackgroundJobServerOptions
    {
        Queues = new [] {"MyQueue"},
    }
    new BackgroundJobServer(serverOptions)
  1. Specify a queue when registering the job
RecurringJob.AddOrUpdate(jobID, jobAction, cron, TimeZoneInfo.Local, "MyQueue");
  1. Since the dashboard is database specific, it has to be shared between the two applications. The dashboard will need to be able to deserialize jobs for both applications. This means that the Dashboard executable needs to reference all dlls that the jobs are defined in (e.g. AppLib1 and AppLib2).

hangfire.mamqsqlextension's People

Contributors

dependabot[bot] avatar eg-dev avatar emisand avatar gexiaoguo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.