Open Closed

Need working hangfire sample #6046


User avatar
0

Hi,

We're in desperate need of a working abp sample using hangfire with SQL, where jobs can be enqueued from Web and Application (from AppService). Would this be possible?

We've having a heck of a time trying to get this working, because the documentation isn't straightforward enough and doesn't really include this scenario (enqueue job from app service) but it's vital for a job that generates reports which can be done from multiple clients (web, maui, etc).

  • ABP Framework version: v7.0.3 (trying to update)
  • UI Type: MVC
  • Database System: EF Core
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes

Issues we've run into:

  • Unidentified background job for the job args type (type here)
  • The job type is not registered to DI Volo.Abp.AbpException: The job type is not registered to DI: CabMD.GeneratedReports.GenerateReportJob at Volo.Abp.BackgroundJobs.BackgroundJobExecuter.ExecuteAsync(JobExecutionContext context) at Volo.Abp.BackgroundJobs.Hangfire.HangfireJobExecutionAdapter`1.ExecuteAsync(String queue, TArgs args) at InvokeStub_TaskAwaiter.GetResult(Object, Object, IntPtr*) at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

What would be nice to see exactly:

  • What modules should be included in what. For example, when should AbpBackgroundJobsHangfireModule be used vs AbpBackgroundJobsAbstractionsModule, when does it matter?
  • What projects need to reference what?
  • Where should the code for background jobs be in the default tiered example?
  • Where does IsJobExecutionEnabled need to set? In what module(s)?
  • Where should JobStorage.Current be set?
  • The documentation suggests using UseAbpHangfireDashboard but AFAIK, that's not available - though maybe that's because of where I'm using it? Seeing this in a working example would be nice.

In my example, the job is simply defined:

public class GenerateReportJob: AsyncBackgroundJob<GenerateReportJobArgs>, ITransientDependency
{}

[Serializable]
public class GenerateReportJobArgs
{
    public GeneratedReportType_Id GeneratedReportType_Id { get; set; }
    public GeneratedReport_Id Id { get; internal set; }
    public string StringParam { get; set; }
    public ReportTaskType TaskTypeId { get; set; } // will be enum, likely report and score_card
    public User_Id UserId { get; set; }
    public int TenantId { get; set; }
}

3 Answer(s)
  • User Avatar
    0

    I do see the SeperateBackgroundJob sample, but it's not quite the same use case. In most cases, I would think people would be running hangfire with the Web project and be able to queue jobs from any other code.

  • User Avatar
    0

    I have made a sample, though this isn't using hangfire: https://github.com/kfrancis/abp-hangfire-sample and that seems to work. If you seed and login under default/admin then go to the books listing, you can see one book (one is tenanted, one is not). Click on the book row to fill in the id, then click "run". You can see in the HttpApi.Host logs that the job is running and it's pulling the name of the book to use in the log entry.

    • The report job is enqueued from the BooksAppService
    • DI seems to be working normally
    • Data filtering seems to be working normally (using CurrentTenant.Change)
    • I've added SignalR to facilitate abp.notify on the clients when the background job completes (to the specific user)

    Here's what that looks like:

    If you can, would you be able to take a look and see what changes need to be made to make this use Hangfire?

  • User Avatar
    0
    gterdem created
    Senior .NET Developer

    Well, the problems here is; you are using web-socket for enqueing the background jobs. That means your application service endpoint is never being called. So, the hangfire integration needs to be done in the Web layer instead of the HttpApi.Host.

    You can examine the PR I've sent https://github.com/kfrancis/abp-hangfire-sample/pull/1

    Since the the hangfire is being hosted by the Web application, navigate to https://localhost:44391/hangfire to see the hangfire dashboard.

    PS: Thank you very much for creating a repository for your problem.

Made with ❤️ on ABP v9.1.0-preview. Updated on November 11, 2024, 11:11