Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Best practice dealing with multi-tenancy from background jobs #7312


User avatar
0
rmorris created

Let's say I want to write some service that can be called during a web request or possibly from a background worker outside of a web request. In the current case code for accessing blob storage. I'm just going to prefix the containers with tenant ID.

So far I've just been iterating over the tenants and fanning out recurrent jobs by enqueuing a job per tenant. Then just always having the tenant ID as a job arg as further jobs may be enqueued, passing the tenant IDs down into their dependencies.

What's the best way to handle this? Can I set an IAbpSession from a background job? Or do I always have to pass tenant IDs down from the job? I'd imagine just using another app service over the base service that gets the tenant from the session if I didn't want to do that explicitly from the entry point app service of a web request.


2 Answer(s)
  • User Avatar
    0
    ryancyq created
    Support Team

    Hi,

    In general for non mvc controller, it would be better to leverage on UnitOfWork instead of AbpSession.

    Reason being, when a unit of work is being started, it will take TenantId from AbpSession by default. In addtion, when you are within a unit of work scope, you can change the TenantId value at any point in time.

    For example,

    • service class gets tenant id from current unit of work
    • mvc controller begins unit of work with tenant id from abp session (this is done automatically by the framework)
    • background job begins unit of work scope and call service class within the scope
  • User Avatar
    0
    kansoftware created

    @ryancyq

    I need to schedule jobs tenantwise. For example i have 100 tenants and for all of them i need to clear some data on daily basis. How i can schedule a job using hangfire for all of them so that can run simultaneously for all the tenants at specific time.

    Regards, Harshit