Open Closed

Hangfire background worker #9686


User avatar
0
LiSong created

I have a hangfilre job: public class HourlyBackgroundWorker : HangfireBackgroundWorkerBase { private readonly ILogger<HourlyBackgroundWorker> _logger; private readonly IUserFileFeedbackService _userFileFeedbackService; private readonly IDataConnectionUserAnswerService _dataConnectionUserAnswerService; private readonly IDataConnectionUserAnswerRepository _dataConnectionUserAnswerRepository; private readonly IFeedbackFilesUpdaterService _feedbackFilesUpdaterService;

public HourlyBackgroundWorker(ILogger&lt;HourlyBackgroundWorker&gt; logger, 
    IUserFileFeedbackService userFileFeedbackService,
    IDataConnectionUserAnswerService dataConnectionUserAnswerService,
    IDataConnectionUserAnswerRepository dataConnectionUserAnswerRepository,
    IFeedbackFilesUpdaterService feedbackFilesUpdaterService)
{
    RecurringJobId = nameof(HourlyBackgroundWorker);
    CronExpression = Cron.Hourly();
    _logger = logger;
    _userFileFeedbackService = userFileFeedbackService;
    _dataConnectionUserAnswerService = dataConnectionUserAnswerService;
    _dataConnectionUserAnswerRepository = dataConnectionUserAnswerRepository;
    _feedbackFilesUpdaterService = feedbackFilesUpdaterService;
}

public override async Task DoWorkAsync(CancellationToken cancellationToken = default)
{
    using var uow = LazyServiceProvider.LazyGetRequiredService&lt;IUnitOfWorkManager&gt;().Begin();
    _logger.LogInformation("Hangfire:HourlyBackgroundWorker::UpdateFeedbacks");
    await _feedbackFilesUpdaterService.UpdateAllFeedbacksAsync();
}

Is Hangfire multi-tenant aware by default? does this hourly job runs for each tenant? i.e. runs once per tenant?


2 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent

    No, Hangfire is not multi-tenant aware by default in ABP Framework. When you define a recurring job like your HourlyBackgroundWorker, it will run once per schedule for the entire application, not once per tenant. This means the job executes globally, not per tenant, unless you explicitly implement logic to iterate over tenants and perform actions within each tenant's context.

    If you want the job to run separately for each tenant, you need to manually enumerate all tenants and use ICurrentTenant.Change(tenantId) to execute tenant-specific logic inside your DoWorkAsync method. Here is a simplified example:

    public override async Task DoWorkAsync(CancellationToken cancellationToken = default)
    {
        var tenantRepository = LazyServiceProvider.LazyGetRequiredService<ITenantRepository>();
        var currentTenant = LazyServiceProvider.LazyGetRequiredService<ICurrentTenant>();
    
        var tenants = await tenantRepository.GetListAsync();
    
        foreach (var tenant in tenants)
        {
            using (currentTenant.Change(tenant.Id))
            {
                // Your tenant-specific logic here
                await _feedbackFilesUpdaterService.UpdateAllFeedbacksAsync();
            }
        }
    }
    

    This approach ensures the job runs for each tenant context.

    Sources:

    This approach aligns with common ABP multi-tenancy conventions, but specific official documentation for this exact scenario is not available.


    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    hi

    It runs once every hour and doesn't have a multi-tenant concept, it simply executes on a schedule. You can switch tenants within the worker.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on September 01, 2025, 08:37