weixin_39769984
weixin_39769984
2021-01-01 13:07

Schedule (The ability to schedule at a specific time)

We currently schedule jobs in our NSB endpoint as follows.

Schedule.Every(24.Hours()).Action(JobToExecute);

The problem with this that the time the job executes is dependent on when the endpoint host starts.

It would be great if you could schedule the job for a specific date time/ time.

ie Schedule.At(EveryDay.At(2).PM()).Action(JobToExecute);

该提问来源于开源项目:Particular/NServiceBus

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

4条回答

  • weixin_39631667 weixin_39631667 4月前

    would the solution in here work for you?

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

    Task.Delay isn't available in .NET4

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

    I use NCronTab for such needs. A cron schedule exists independent of the process start time.

     C#
    var nextRun = CrontabSchedule.Parse(Cron).GetNextOccurrence(DateTime.UtcNow);
    

    The thing about recurring scheduling is that there is no guarantee that the last recurrence is complete before the next one begins. Setting too high a frequency can result in two occurrences running at the same time which might be problematic. For this reason I usually employ a timer where the autoreset = false

     C#
    _timer = new Timer() { AutoReset = false };
    _timer.Elapsed += {
       ...
       var nextRun = CrontabSchedule.Parse("1 23 * * *").GetNextOccurrence(DateTime.UtcNow);
       var milliseconds = nextRun.Subtract(DateTime.UtcNow).TotalMilliseconds;
       _timer.Interval = milliseconds;
      _timer.Start();
    }
    

    The above code will set a timer to run at 2300 hours on the first minute daily and ensure no two timer jobs overlap.

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

    duplicate of #373

    点赞 评论 复制链接分享

相关推荐