Base solution for your next web application
Open Closed

Getting Hangfire job context #12236


User avatar
0
Ricavir created

Hi,

Is there a way to get Hangfire job context with ABP 9.3 ? I need to get "retryCount" value on job execution.

This should be possible with following Hangfire command : JobStorage.Current.GetConnection().GetJobParameter(jobId, "RetryCount")

I tried it by adding Hangfire nuget package directly but I'm getting a dependency error :

'MyJob' is waiting for the following dependencies:
- Service 'Hangfire.Server.PerformContext' which was not registered.

6 Answer(s)
  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @Ricavir,

    Could you check this document?

    https://aspnetboilerplate.com/Pages/Documents/Hangfire-Integration

  • User Avatar
    0
    Ricavir created

    Hi,

    Yes for sure ! I know your documentation for a while now ;) Have a look to my question please. Are you able to retreive "retryCount" Hangfire property with ABP ?

    Tks

  • User Avatar
    0
    m.aliozkaya created
    Support Team

    Hi @Ricavir,

    Could you share your MyJob class? It seems there is a dependency issue.

  • User Avatar
    0
    Ricavir created

    It is a dependency issue because I need to include Hangfire core package to get access to JobStorage :

    using System.Linq;
    using Microsoft.EntityFrameworkCore;
    using Abp.Collections.Extensions;
    using Abp.BackgroundJobs;
    using Abp.Dependency;
    using MyApp.FileReferences.Generating;
    using MyApp.Notifications;
    using Abp.Domain.Uow;
    using System;
    using Abp.Extensions;
    using MyApp.Events.Search;
    using System.Linq.Dynamic.Core;
    using System.Threading.Tasks;
    using Hangfire;
    
    namespace MyApp.Events.Exporting
    {
        public class FileDocIoGeneratorJob : AsyncBackgroundJob<FileDocIoGeneratorJobArgs>, ITransientDependency
        {
            private readonly IFileDocIoGenerator _fileDocIoGenerator;
            private readonly IUnitOfWorkManager _unitOfWorkManager;
            private readonly IAppNotifier _appNotifier;
            private readonly IEventQueryManager _eventQueryManager;
    
            public FileDocIoGeneratorJob(IFileDocIoGenerator fileDocIoGenerator,
                                                    IUnitOfWorkManager unitOfWorkManager,
                                                    IAppNotifier appNotifier,
                                                    IEventQueryManager eventQueryManager)
            {
                _fileDocIoGenerator = fileDocIoGenerator;
                _appNotifier = appNotifier;
                _unitOfWorkManager = unitOfWorkManager;
                _eventQueryManager = eventQueryManager;
            }
    
            public override async Task ExecuteAsync(FileDocIoGeneratorEventForecastsJobArgs args)
            {
                await _unitOfWorkManager.WithUnitOfWorkAsync(async () =>
                {
                    using (UnitOfWorkManager.Current.SetTenantId(args.TenantId))
                    {
                        try
                        {
                            var events = await _eventQueryManager.GetEventsQueryable().Take(maxExportCount).ToListAsync();
    
                            if (events.IsNullOrEmpty())
                                return;
    
                            file = await _fileDocIoGenerator.GenerateFileAsync(events);
    
                            await _appNotifier.FileGenerationJobSucceedAsync(file, "FileGenerationJobForSucceed", args.UserIdentifier);                            
                        }
                        catch (Exception ex)
                        {
                            var retry = JobStorage.Current.GetConnection().GetJobParameter("jobId", "RetryCount");                        
                            Logger.Error("File generation job for event forecasts failed", ex);
                            if(retry == "10")
                                await _appNotifier.FileGenerationJobFailedAsync("FileGenerationJobForFailed", args.UserIdentifier);                      
                        }
                        
                    }
                });
            }
        }
    }
    
    

    Since ABP backgroundjob abstracts Hangfire, I don't know how to proceed to get retryCount parameter

  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi

    Have you implemented the issue comment regarding this issue?

  • User Avatar
    0
    Ricavir created

    Hi,

    Thks @oguzhanagir ! Didn't see that one. I've tested and it's working :)