HangfireIO / HangfireIO/Hangfire

Hangfire in a one-db-per-tenant scenario

Open
#793 3 comments 0 reactions 0 assignees View on GitHub
t: question
Dominant language
C#
Stars
10.1k
Forks
1.8k
Avg merge
1h 19m
Merged PRs (30d)
1

Description

I use hangfire in a multi tenant web application where each tenant has it's own database. Every time a http request comes in, the database that will be used is determined by a tenant id (retrieved by subdomain or http header). Based on the tenant id the correct Enterprise Framework context will be injected through an IoC container using per-request-lifetime.

Now i want to be able to enqueue hangfire jobs for a specific tenant and inject the corresponding db context for the job execution. In a hangfire job the tenant id can't be retrieved from the `HttpContext`. Maybe it would be possible to pass the `HttpContext` around somehow but at least for recurring jobs this would not be an option because they are not triggered by an http request.

I need a possibility to attach a tenant id to a job when enqueuing it which will later be available in an implementation of `IServerFilter`. Then i could create a child DI context using a `JobActivator`implementation in the `OnPerforming`-method and dispose it in the `OnPerformed`-method and pass the tenant id to the context creation.

For recurring jobs this is possible by adding the tenant id as part of the job id. For normal background jobs i see no clean way to attach tenant id metadata and later retrieve it from `PerformingContext`.

Contributor guide

Open the contributing guide

Research direction

Start with the enqueueing path and the IServerFilter/PerformingContext lifecycle described in the issue. Trace how JobActivator handles OnPerforming and OnPerformed, and compare recurring-job IDs with normal background jobs. Done means a tenant ID can be attached when a job is enqueued and retrieved by the filter for per-tenant context creation, including recurring jobs.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.