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

提供作业调度功能 #18

Open
vipwan opened this issue May 13, 2024 · 1 comment
Open

提供作业调度功能 #18

vipwan opened this issue May 13, 2024 · 1 comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@vipwan
Copy link
Owner

vipwan commented May 13, 2024

任务调度ScheduleTask:
实现IScheduleTask接口,添加 ScheduleTask 特性,指定任务调度时间表达式,即可实现任务调度。

/// <summary>
/// KeepAlive ScheduleTask
/// </summary>
/// <param name="logger"></param>
[ScheduleTask("* * * * *")] //每分钟一次
[ScheduleTask("0/3 * * * *")]//每3分钟执行一次
public class KeepAlive(ILogger<KeepAlive> logger) : IScheduleTask
{
    public async Task ExecuteAsync()
    {
        logger.LogInformation("keep alive!");
        await Task.CompletedTask;
    }
}

支持从Store中初始化任务调度列表,支持动态添加任务调度,支持动态删除任务调度。

/// <summary>
/// Demo ScheduleTask,用于Store演示
/// </summary>
/// <param name="logger"></param>
public class DemoTask(ILogger<DemoTask> logger) : IScheduleTask
{
    public Task ExecuteAsync()
    {
        logger.LogInformation("Demo Schedule Done!");
        return Task.CompletedTask;
    }
}

/// <summary>
/// DemoStore演示
/// </summary>
public class DemoStore : IScheduleMetadaStore
{
    public Task<IEnumerable<ScheduleTaskMetadata>> GetAllAsync()
    {
        //模拟从数据库或配置文件中获取ScheduleTaskMetadata
        IEnumerable<ScheduleTaskMetadata> metadatas =
            [
                new ScheduleTaskMetadata(typeof(DemoTask), "* * * * *")
                {
                    Description="测试的Schedule"
                }
            ];

        return Task.FromResult(metadatas);
    }
}

最后注册DemoStore到DI容器中即可。

builder.Services.AddScheduleMetadaStore<DemoStore>();
vipwan added a commit that referenced this issue May 13, 2024
@vipwan vipwan added documentation Improvements or additions to documentation enhancement New feature or request labels May 13, 2024
@vipwan vipwan pinned this issue May 14, 2024
@vipwan
Copy link
Owner Author

vipwan commented May 14, 2024

vipwan added a commit that referenced this issue May 15, 2024
内部实现一个基于NCrontab的简易调度器(粒度为分)SampleNCrontabScheduler
#18
vipwan added a commit that referenced this issue May 16, 2024
在本次提交中,我们主要添加了处理同一时间只能存在一个的任务的功能。如果有相同的任务正在运行,那么当前的任务会被中断。#18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant