使用 chroniton.NETCore 在 asp.NET 中安排具有固定终止日期的作业

Scheduling a Job with a fixed termination date in asp.NET using chroniton.NETCore

我正在开发一个 WebAPI 来安排事件和 appointments.In 为了做到这一点,我正在使用 chroniton.NetCore 包。我可以使用 cron 表达式或其他函数设置时间表,但是有什么方法可以设置时间表的停止日期或终止日期吗?

      var schedule = new EveryXTimeSchedule(TimeSpan.FromSeconds(5));

        var scheduledJob = singularity.ScheduleParameterizedJob(
            schedule, job, "Hello World", true); //starts immediately

我希望在第二天午夜停止这项工作而不使用 Task.Delay(因为 api 是无状态的) 谢谢。

chroniton.NETCore 的当前版本不支持开箱即用的这种行为。您可以根据事实实现自己的逻辑,即您拥有有关时间范围的信息。

  • ScheduleParameterizedJob() returns 一个 IScheduledJob 可用于在需要时取消作业。您可以创建额外的 RunOnceSchedule 以在第二天午夜使用

    关闭您的第一份工作

    Singularity.Instance.StopScheduledJob(IScheduledJob job);

  • 如果 'next scheduled time' >= 第二天,您可以继承 EveryXTimeSchedule 并将 NextScheduledTime 方法重写为 return Chroniton.Constants.Never午夜;

Best way to create a Midnight DateTime in C# :

DateTime nextDayAtMidnight = DateTime.Today.AddDays(1)