rust-protobuf 是作者 7 年前开始的 Rust protobuf 的实现. 在 Rust 中使用过 protobuf的人都知道, 存在着两个实现,一个是 rust-protobuf, 另外是一个 Prost 为基础的其他实现. 两个实现都有各自的优缺点. 作者同时也表达了自己未来对该库的迷茫.也许未来,多个 protobuf 的实现会合并也不一定.
use tokio_cron_scheduler::{JobScheduler, JobToRun, Job};
#[tokio::main]
async fn main() {
let mut sched = JobScheduler::new();
sched.add(Job::new("1/10 * * * * *", |uuid, l| {
println!("I run every 10 seconds");
}).unwrap());
sched.add(Job::new_async("1/7 * * * * *", |uuid, l| Box::pin( async {
println!("I run async every 7 seconds");
})).unwrap());
sched.add(Job::new("1/30 * * * * *", |uuid, l| {
println!("I run every 30 seconds");
}).unwrap());
sched.add(
Job::new_one_shot(Duration::from_secs(18), |_uuid, _l| {
println!("{:?} I'm only run once", chrono::Utc::now());
}).unwrap()
);
let mut jj = Job::new_repeated(Duration::from_secs(8), |_uuid, _l| {
println!("{:?} I'm repeated every 8 seconds", chrono::Utc::now());
}).unwrap();
sched.add(jj);
jj.on_start_notification_add(Box::new(|job_id, notification_id, type_of_notification| {
Box::pin(async move {
println!("Job {:?} was started, notification {:?} ran ({:?})", job_id, notification_id, type_of_notification);
})
}));
jj.on_stop_notification_add(Box::new(|job_id, notification_id, type_of_notification| {
Box::pin(async move {
println!("Job {:?} was completed, notification {:?} ran ({:?})", job_id, notification_id, type_of_notification);
})
}));
jj.on_removed_notification_add(Box::new(|job_id, notification_id, type_of_notification| {
Box::pin(async move {
println!("Job {:?} was removed, notification {:?} ran ({:?})", job_id, notification_id, type_of_notification);
})
}));
let five_s_job = Job::new("1/5 * * * * *", |_uuid, _l| {
println!("{:?} I run every 5 seconds", chrono::Utc::now());
})
.unwrap();
sched.add(five_s_job);
let four_s_job_async = Job::new_async("1/4 * * * * *", |_uuid, _l| Box::pin(async move {
println!("{:?} I run async every 4 seconds", chrono::Utc::now());
})).unwrap();
sched.add(four_s_job_async);
sched.add(
Job::new("1/30 * * * * *", |_uuid, _l| {
println!("{:?} I run every 30 seconds", chrono::Utc::now());
})
.unwrap(),
);
sched.add(
Job::new_one_shot(Duration::from_secs(18), |_uuid, _l| {
println!("{:?} I'm only run once", chrono::Utc::now());
}).unwrap()
);
sched.add(
Job::new_one_shot_async(Duration::from_secs(16), |_uuid, _l| Box::pin( async move {
println!("{:?} I'm only run once async", chrono::Utc::now());
})).unwrap()
);
let jj = Job::new_repeated(Duration::from_secs(8), |_uuid, _l| {
println!("{:?} I'm repeated every 8 seconds", chrono::Utc::now());
}).unwrap();
sched.add(jj);
let jja = Job::new_repeated_async(Duration::from_secs(7), |_uuid, _l| Box::pin(async move {
println!("{:?} I'm repeated async every 7 seconds", chrono::Utc::now());
})).unwrap();
sched.add(jja);
sched.start().await;
}
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。