Skip to content

1.5.2.1 released

Compare
Choose a tag to compare
@vipwan vipwan released this 15 May 17:57
· 326 commits to master since this release
  1. 优化调度服务和元数据存储实现 定义了MetadataStore
  2. IScheduler调度器抽象化
  3. 添加本地锁功能LocalLock 用以保证特定情况下的任务调度单次执行
public class DemoTask(ILogger<DemoTask> logger, ILocalLock @lock) : IScheduleTask
{
    public async Task ExecuteAsync()
    {
        var timeout = TimeSpan.FromMilliseconds(30_000);
        //使用ILocalLock进行锁定,防止重复执行
        var (locker, _) = await @lock.TryAcquireLockAsync("ONLY_ONE_ScheduleTask_OF_DemoTask", timeout, timeout);
        if (locker is null)
        {
            return;//当前锁定,则不执行!
        }
        using (locker)
        {
            await Task.Delay(31_000);
            logger.LogInformation("Demo memory store Schedule Done!"); //todo something:
        }
    }
}

提供OnlyOneRunningScheduleTask

[ScheduleTask(Constants.CronEveryMinute, IsStartOnInit = true)]
public class OnlyOneTask(ILogger<OnlyOneTask> logger) : OnlyOneRunningScheduleTask
{
    public override Task OnAbort()
    {
        logger.LogWarning($"[{DateTime.Now}]任务被打断.因为有一个相同的任务正在执行!");
        return Task.CompletedTask;
    }

    public override async Task ExecuteAsync()
    {
        var now = DateTime.Now;
        //模拟一个耗时2分钟的任务
        await Task.Delay(TimeSpan.FromMinutes(2));
        logger.LogInformation($"[{now}] ~ {DateTime.Now} 执行一个耗时两分钟的任务!");
    }
}