Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can not use ISettingManager in QuartzBackgroundWorkerBase,What is the correct way to init Trigger with settingManager info? #19799

Open
1 task done
CAH-FlyChen opened this issue May 13, 2024 · 3 comments

Comments

@CAH-FlyChen
Copy link

CAH-FlyChen commented May 13, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Description

 public class OrderExpCheckerWorker : QuartzBackgroundWorkerBase
 {
     IOrderRepository _orderRepository;
     ISettingManager _settingManager;
     IServiceProvider _serviceProvider;

     public OrderExpCheckerWorker(ISettingManager settingManager, IOrderRepository orderRepository, IServiceProvider serviceProvider) : base()
     {
         _settingManager = settingManager;
         _orderRepository = orderRepository;
         _serviceProvider = serviceProvider;

         AutoRegister = true;

         /// got deadlock
         var p = _settingManager.GetOrNullGlobalAsync("ccccc").GetAwaiter().GetResult();

         JobDetail = JobBuilder.Create<OrderExpCheckerWorker>().WithIdentity(nameof(OrderExpCheckerWorker)).Build();
         Trigger = TriggerBuilder.Create()
                 .WithIdentity(nameof(OrderExpCheckerWorker))
                 .WithSimpleSchedule(s => s.WithIntervalInMinutes(Convert.ToInt32(p))
                                         .RepeatForever()
                                         .WithMisfireHandlingInstructionIgnoreMisfires()
                                     )
                 .Build();
     }


     public override Task Execute(IJobExecutionContext context)
     {
         Logger.LogInformation("test");
         return Task.CompletedTask;
     }
 }

when run var p = _settingManager.GetOrNullGlobalAsync("ccccc").GetAwaiter().GetResult(); got deadlock

What is the correct way to init Trigger with settingManager info?

I have been tried several way to run _settingManager.GetOrNullGlobalAsync("ccccc").GetAwaiter().GetResult() but got all fail。

It seems the definition is later construct than the backgroundwork construct

@maliming maliming removed the bug label May 13, 2024
@nebula2
Copy link
Contributor

nebula2 commented May 13, 2024

try getting settings in the task and not in the ctor

@CAH-FlyChen
Copy link
Author

try getting settings in the task and not in the ctor

tried but all fail. for example AsyncHelper , Task ,
I think it's better to have a async methord to init the backgroundworker,not in the constructor.

@CAH-FlyChen
Copy link
Author

CAH-FlyChen commented May 14, 2024

solve by strange way
in order to start define settings before worker start, change AutoRegister to false. and start in postapplication event.
in order to execute async methord , config schedule job to flowing

`
public OrderExpCheckerWorker(ISettingManager settingManager, IOrderRepository orderRepository, IServiceProvider serviceProvider) : base()
{
_settingManager = settingManager;
_orderRepository = orderRepository;
_serviceProvider = serviceProvider;

        AutoRegister = false;

        JobDetail = JobBuilder.Create<OrderExpCheckerWorker>().WithIdentity(nameof(OrderExpCheckerWorker)).Build();
        Trigger = TriggerBuilder.Create()
                .WithIdentity(nameof(OrderExpCheckerWorker))
                .Build();
        ScheduleJob = async scheduler =>
        {
            if (!await scheduler.CheckExists(JobDetail.Key))
            {
                var p = await _settingManager.GetOrNullGlobalAsync(EShopConsts.OrderExpireCheckPeriodSetting);
                Trigger = TriggerBuilder.Create()
                .WithIdentity(nameof(OrderExpCheckerWorker))
                .WithSimpleSchedule(s => s.WithIntervalInMinutes(Convert.ToInt32(p))
                                        .RepeatForever()
                                        .WithMisfireHandlingInstructionIgnoreMisfires()
                                    )
                .Build();
                await scheduler.ScheduleJob(JobDetail, Trigger);
            }
        };
    }

`

another way is change Trigger before manual start

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants