Base solution for your next web application
Open Closed

Hangfire and Separate Tenant database. #7386


User avatar
0
Mazharmig created

Hi, I have tenants with with separate databases, when hangfire job is executed it uses default host database. How can I force repositories to use specific database for db operations? **IMMEDIATE RESPONSE IS REQUIRED. ** Thanks


19 Answer(s)
  • User Avatar
    0
    musa.demir created

    you want Hangfire to work with tenant databases?

  • User Avatar
    0
    Mazharmig created

    yes

  • User Avatar
    0
    Mazharmig created

    For example Tenant A schedules a SMS sending job and this tenant has its own database and this job has nothing to do with Host database and other databases.

  • User Avatar
    0
    musa.demir created

    We don't have such an integrated by default.

  • User Avatar
    0
    Mazharmig created

    Lets keep hangfire a side, how can i force repositories to use a specific DbContext? For example: By providing a specific connection string in EntityframeworkCoreModule public override void PreInitialize() { if (!SkipDbContextRegistration) { Configuration.Modules.AbpEfCore().AddDbContext<MCodeCoreDbContext>(options => { if (options.ExistingConnection != null) { MCodeCoreDbContextConfigurer.Configure(options.DbContextOptions, options.ExistingConnection); } else { MCodeCoreDbContextConfigurer.Configure(options.DbContextOptions, options.ConnectionString); } }); }

        }
    
  • User Avatar
    0
    musa.demir created

    you can create custom repository. see:https://aspnetboilerplate.com/Pages/Documents/EntityFramework-Integration#custom-repositories

  • User Avatar
    0
    Mazharmig created

    How can I initialize custom repository with specific dbContext (connection string)?

  • User Avatar
    0
    musa.demir created

    You can create db context for each custom repository if you have a few connection string.

    Steps: Create dbcontext and then configure them with their connection strings.

    see: https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core?searchKey=DbContext%20#configuration

    Then create custom repositories for them

  • User Avatar
    0
    Mazharmig created

    Can u show me example or some sample code. Thanks

  • User Avatar
    0
    musa.demir created

    I don't have an example project but documents has all of them. see: https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core

    creating db context: https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core#dbcontext configuring db context: https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core#configuration creating custom repositories from db context: https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core#custom-repositories

  • User Avatar
    0
    musa.demir created

    https://aspnetboilerplate.com/Pages/Documents/Entity-Framework-Core#configuration says that:

    "ABP can use IConnectionStringResolver to determine it. This behaviour can be changed and the connection string can be determined dynamically."

    I never try it, but i think you should try it before them https://support.aspnetzero.com/QA/Questions/7386#answer-266967fa-ea7f-b56e-03ee-39ef3b733097

  • User Avatar
    0
    leonkosak created

    Having jobs in tenant's database would also have better performance impact on database. If there are a lot of tenants in system and background jobs in each tenants are frequent, thare could be a bottleneck on host database. Consider also this aspect.

  • User Avatar
    0
    Mazharmig created

    I have tried having jobs in respective tenant database but it was not successful, have you tried this?

  • User Avatar
    0
    leonkosak created

    My response is for Volosoft (in consideration for better background jobs). :)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @Mazharmig,

    Is this issue resolved for you ?

    Thanks,

  • User Avatar
    0
    Mazharmig created

    I just implemented a work around

  • User Avatar
    0
    Mazharmig created

    I want every tenant responsible for its job. For that purpose I implemented a HangfireConfigurer that Initializes tenant's hangfire dashboard at startup. I have done this before in my old application that will be replaced by ANZ but I got issue at Controller level, after setting JobStorage Hangfire didn't worked.

  • User Avatar
    0
    Mazharmig created

    Now I am using Host database for every job (all tenants). I have implemented a ContextJobWorker that picks the job and works in the respective database of tenant. Any suggestion or correction from your side?

  • User Avatar
    0
    ismcagdas created
    Support Team

    I can't think of a better solution at the moment.