惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
V
V2EX
S
Security Affairs
T
Threatpost
C
CXSECURITY Database RSS Feed - CXSecurity.com
IT之家
IT之家
J
Java Code Geeks
The Register - Security
The Register - Security
U
Unit 42
C
CERT Recently Published Vulnerability Notes
月光博客
月光博客
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Cisco Talos Blog
Cisco Talos Blog
Project Zero
Project Zero
S
Schneier on Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
博客园 - 司徒正美
V
Vulnerabilities – Threatpost
T
Tor Project blog
Security Latest
Security Latest
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
小众软件
小众软件
L
LangChain Blog
Attack and Defense Labs
Attack and Defense Labs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
P
Palo Alto Networks Blog
A
Arctic Wolf
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 叶小钗
D
Darknet – Hacking Tools, Hacker News & Cyber Security
L
LINUX DO - 最新话题
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
H
Hacker News: Front Page
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta

博客园 - aquilahkj

在Flutter中使用极光推送集成华为通道踩坑 Flutter中生成Android的jks签名文件并使用 ABP Vnext使用mysql数据库 实现ElementUI Dialog宽度响应式变化 使用Vue Baidu Map对百度地图实现输入框搜索定位 使用Docker搭建HttpRunnerManager环境 docker搭建nginx反代ngrok服务 - aquilahkj 开源一个基于dotnet standard的轻量级的ORM框架-Light.Data VMware Fusion下安装Win8启用Hyper-V centos下安装rinetd(转载) 在Mac OS X上安装JavaHL SVN数据仓库配置 在MONO下实现WCF所遇到的问题 mysql 创建 主键索引 唯一索引 全文索引 多列索引 添加索引 CentOS 6.0 安装 MONO 2.10.8 关于MYSQL 远程登录的授权方法 命令 CentOS 6.0 编译安装 MySQL 5.5.17 linux下允许root用户远程登录 Linux Vi命令
在dotnet core实现类似crontab的定时任务
aquilahkj · 2019-07-22 · via 博客园 - aquilahkj

  前段需要在业务中实现某些时间段的简单定时任务,类似crontab的调度,因为业务会放在docker中,所以不想用直接用crontab,在网上搜了一下,发现一个开源的实现 Pomelo.AspNetCore.TimedJob,使用简单,但是因为是时间间隔执行,不太符合指定时间段要求,不过感谢此开源代码和作者,我在其基础上重新撸了个crontab的实现,并增加了一些功能,代码已放在Github上。

    源码地址:https://github.com/aquilahkj/Light.Cron

    使用Demo:https://github.com/aquilahkj/Light.Cron/tree/master/sample/Light.Cron.Sample

    首先通过Nuget安装

PM> Install-Package Light.Cron

  配置StartUp

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
    public IConfiguration Configuration { get; }
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        services.AddCrontabJob();
    }
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseCrontabJob();
        app.UseMvc();
    }
}

  编写crontab方法

[CrontabJob]
public class CrontabObject
{
    [CrontabSchedule("crontab1", "* * * * *")]
    public void DoSomeThing()
    {
        // Todo
    }
}

  即可完成每分钟调用一次的定时任务

Light.Cron完全兼容Linux Crontab时间格式

*  *  *  *  *
分 时 日 月 周

第1列表示分钟0~59

第2列表示小时0~23

第3列表示日期1~31

第4列表示月份1~12

第5列标识号星期0~7(0和7表示星期天)

每天10点至16点 
0 10-16 * * *

每隔两个小时 
0 */2 * * *

并在此基础上对一直以来在crontab使用上的一些痛点进行了功能增强

跨时间段

当时间段格式出现结束时间小于开始时间, 则代表由开始时间到下一级时间的开始时间, 如

每月1号22点到2号4点之间的时间段
* 22-4 1 * * 
每周一至周五晚上22点到次日4点之间的时间段, 里面包含周六的0点-4点, 不包含周一的0点-4点
* 22-4 * * 1-5

月末日期

日期格式中增加新增e标记作为月末最后一天, 并且可以通过e+数字代表最后一天再往前倒数天数, 如当月共31天, 则e=31, e1=30, e2=29, e3=28. 当月共30天, 则e=30, e1=29, e2=28, e3=27. 该语法同样支持范围

每月1日、15日和最后一日
0 0 1,15,e * *
倒数第4天至倒数第2天
0 0 e3-e1 * *

时间范围

Light.Cron可以通过新语法HH:mm-HH:mm将时分结合, 替换原有的时分设置, 如

每日9点30分至15点0分, 每隔一分钟执行一次
09:30-15:00 * * *
每日21点30分至次日的5点30分, 每隔5分钟执行一次
21:30-05:30/5 * * *

多组调度计划

Light.Cron可以通过|符号支持多组调度计划, 如

0点至11点, 每分钟执行一次, 12点至23点, 每5分钟执行一次
* 0-11 * * *|*/5 12-23 * * *

简略写法

如下一级及其以之后的时间范围都是*, 可以简略不写, 如

* * * * * = *
30 9 * * * = 30 9
09:30-15:00 * * * = 09:30-15:00
0 0 1 * * = 0 0 1
0 0 1 2-5 * = 0 0 1 2-5