1 个不稳定版本
0.1.0 | 2024年6月17日 |
---|
#372 在 过程宏
11KB
83 行
cronitor
简单的 Rust Cron 框架
lib.rs
:
Cronitor
Cronitor 是一个简单高效的 Rust Cron 框架。它允许你轻松地安排和运行 cron 作业。
!! 注意 !! 你将需要依赖项,因为过程宏有时可能会有些古怪。
cronitor = { git = "https://github.com/floris-xlx/cronitor.git", branch = "main" }
cronitor_runtime = { git = "https://github.com/floris-xlx/cronitor.git", branch = "main" }
ctor = "0.2.8"
示例
以下是使用 Cronitor 的示例
示例 1:每分钟运行一个作业
use cronitor_macro::cronitor;
use cronitor_runtime::cron_runtime;
#[cronitor("*/1 * * * *")]
fn ping_every_minute() {
println!("Ping! Pong!");
}
fn main() {
cron_runtime();
loop {
std::thread::park();
}
}
示例 2:运行多个作业
use cronitor_macro::cronitor;
use cronitor_runtime::cron_runtime;
#[cronitor("*/5 * * * *")]
fn ping_every_5_minutes() {
println!("Ping! Pong!");
}
#[cronitor("*/15 * * * *")]
fn ping_every_15_minutes() {
println!("Ping! Pong! part 2");
}
fn main() {
cron_runtime();
loop {
std::thread::park();
}
}
示例 3:每天早上6点运行一个作业
use cronitor_macro::cronitor;
use cronitor_runtime::cron_runtime;
#[cronitor("0 6 * * *")]
fn ping_every_day_at_6() {
println!("Ping! Pong!");
}
fn main() {
cron_runtime();
loop {
std::thread::park();
}
}
示例文件
示例文件位于 /examples 文件夹中。您可以使用以下命令运行这些示例
cargo run --example EXAMPLE_FILE.rs
时区
Cronitor 会选择计算机所在的时区,确保您的 cron 作业根据本地时间设置运行。
同步上下文
Cronitor 目前是为同步上下文设计的。这意味着它最适合需要按顺序执行任务的那些应用程序。
验证 Cron 表达式
如果您想验证一个 cron 表达式,可以使用此网站:https://crontab.cronhub.io/
依赖项
~2–13MB
~103K SLoC