weixin_39979215
weixin_39979215
2021-01-04 04:54

Cross-schedule blocking

I'm trying to handle a case where two schedules are doing database work that is causing deadlocks. What I need is for schedule A and schedule B to never be running at the same time.

Is there a way to have one schedule reference another to see it's running. I'd like to have schedule A only execute if schedule B not currently executing. Or even better, if I could have schedule B stop/start schedule A whenever schedule B executes.

//schedule A - every 1 minute Schedule(() => { //if daily task is NOT running, execute code }).ToRunEvery(1).Minutes();

//schedule B - every 1 day Schedule(() => { //execute code }).ToRunEvery(1).Days();

该提问来源于开源项目:fluentscheduler/FluentScheduler

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • weixin_39979215 weixin_39979215 4月前

    Great! Yes, that will work great. Thanks.

    点赞 评论 复制链接分享
  • weixin_39781143 weixin_39781143 4月前

    Should be able to do something like:

     csharp
    Schedule(() =>
    {
      if (TaskManager.RunningSchedules.Any(x => x.Name == "scheduleB")) {
        // Return because daily task is running
        return;
      }
    
      // TODO: Do work
    }).WithName("scheduleA").ToRunEvery(1).Minutes();
    
    //schedule B - every 1 day
    Schedule(() =>
    {
      while (TaskManager.RunningSchedules.Any(x => x.Name == "scheduleA")) {
        // Wait for scheduleA to finish before running this task
        Thread.Sleep(1000);
      }
    
      // TODO: Do real work here
    }).WithName("scheduleB").ToRunEvery(1).Days();
    
    点赞 评论 复制链接分享
  • weixin_39979215 weixin_39979215 4月前

    Oh, so RunningSchedules is a collection of "Executing" schedules? I thought it was schedules registered (and ticking), but not necessarily executing.

    点赞 评论 复制链接分享
  • weixin_39781143 weixin_39781143 4月前

    Correct, it is a list of schedules that are currently executing, not a list of schedules scheduled to run :)

    Will that work for you?

    点赞 评论 复制链接分享

相关推荐