abp icon indicating copy to clipboard operation
abp copied to clipboard

Obtain job information while running background jobs

Open git102347501 opened this issue 1 year ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

I have a backend job requesting an API, and I will dynamically add backend authors through the interface in the frontend, as follows: When adding PostApiWorker, I will pass in the specific parameters of the request API and the request address for use during work.

var prop1 = new { url = "https://xxx.xxx.com/getinfo", method = "get" }; 
await _backgroundWorkerManager.AddAsync(new PostApiWorker(prop1));

This is the implementation of PostApiWorker, using Hangfire to work:

public class PostApiWorker: HangfireBackgroundWorkerBase, IPostApiWorker
{
    private PostWorkerModel Data { set; get; }
    
    public PostApiWorker(PostWorkerModel input)
    {
        RecurringJobId = "post-api-" + DateTime.Now.ToString("yyyyMMddHHmmss");
        CronExpression = input.Cron;
        Data = input;
    }

    public override Task DoWorkAsync(CancellationToken cancellationToken = default)
    {
        // Obtain the Data information of PostWorkerModel or the RecurringJobId information when creating the job
        // This type of assignment cannot obtain job information from the database in a fixed manner because it is 
        // dynamically added
        // I can associate jobid with parameter information, but I cannot obtain the jobid during construction here either
    }
}

Describe the solution you'd like

I want to retrieve information from await_backgroundWorkerManager during DoWorkAsync AddAsync (new PostApiWorker (prop1));

When adding homework, the prop1 parameter information was used to obtain the required interface address and parameters, but it was found that it could not be achieved

The reasons are as follows:

  • The construction of PostApiWorker can only accept parameters from DI dependency injection
  • The DoWorkAsync method cannot retrieve any information related to the current job, such as RecurringJobId reading spatiotemporal information in DoWorkAsync

Therefore, the dynamic job that requests the API cannot obtain the parameter information of the dynamically added job at runtime, and therefore cannot work

I need to obtain the valid parameter information prop1 or PostWorkerModel when inserting a job in DoWorkAsync. How will this be implemented?

Additional context

No response

git102347501 avatar Mar 30 '24 04:03 git102347501