sched_task_rs/src/main.rs

115 lines
5.2 KiB
Rust

use std::{env, sync::Arc, thread, time::Duration};
use clokwerk::{Scheduler, TimeUnits, Job};
use log::{info, error};
mod tasks;
use tasks::*;
mod config;
fn main() {
env_logger::init();
let mut scheduler = Scheduler::new();
let fmt = "%Y年%m月%d日 %H:%M:%S";
// let notify_url = "https://ding.airpig.cn/QfagvFa7u323xUS5yRbFR3";
let file_path = if env::var("RUST_ENV").unwrap_or_else(|_|"dev".to_string()) == "dev" {
"./config.toml"
} else {
"/etc/sched_rs/config.toml"
};
let config = config::read_config(file_path).unwrap();
let config = Arc::new(config);
let clone_config1 = Arc::clone(&config);
scheduler.every(1.day()).at("00:01:00").run(move|| {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let notify_urls = clone_config1.notify_urls.iter().map(|url| url.as_str()).collect::<Vec<&str>>();
let db_url = format!("mysql://{}:{}@{}:{}/{}", clone_config1.username, clone_config1.password, clone_config1.host, clone_config1.port, clone_config1.database);
let res = get_data_from_500_com("pls", &db_url).await;
let msg;
match res {
Ok(val) => {
msg = format!("{}: 排列3数据采集完毕, 最新数据为: {}!", chrono::Local::now().format(fmt), val);
info!("{}", msg);
},
Err(e) => {
msg = format!("{}: 排列3数据采集失败: {}!", chrono::Local::now().format(fmt), e);
error!("{}", msg);
}
}
for notify_url in notify_urls {
let notify = reqwest::get(format!("{}/{}", notify_url, msg)).await.unwrap();
if notify.status().is_success() {
info!("{}: 排列3数据采集通知发送成功", chrono::Local::now().format(fmt));
} else {
error!("{}: 排列3数据采集通知发送失败", chrono::Local::now().format(fmt));
}
}
})
});
let clone_config2 = Arc::clone(&config);
scheduler.every(1.day()).at("00:01:15").run(move|| {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let notify_urls = clone_config2.notify_urls.iter().map(|url| url.as_str()).collect::<Vec<&str>>();
let db_url = format!("mysql://{}:{}@{}:{}/{}", clone_config2.username, clone_config2.password, clone_config2.host, clone_config2.port, clone_config2.database);
let res = get_data_from_500_com("plw", &db_url).await;
let msg;
match res {
Ok(val) => {
msg = format!("{}: 排列5数据采集完毕, 最新数据为: {}!", chrono::Local::now().format(fmt), val);
info!("{}", msg);
},
Err(e) => {
msg = format!("{}: 排列5数据采集失败: {}!", chrono::Local::now().format(fmt), e);
error!("{}", msg);
}
}
for notify_url in notify_urls {
let notify = reqwest::get(format!("{}/{}", notify_url, msg)).await.unwrap();
if notify.status().is_success() {
info!("{}: 排列5数据采集通知发送成功", chrono::Local::now().format(fmt));
} else {
error!("{}: 排列5数据采集通知发送失败", chrono::Local::now().format(fmt));
}
}
})
});
let clone_config3 = Arc::clone(&config);
scheduler.every(1.day()).at("00:01:30").run(move|| {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let notify_urls = clone_config3.notify_urls.iter().map(|url| url.as_str()).collect::<Vec<&str>>();
let db_url = format!("mysql://{}:{}@{}:{}/{}", clone_config3.username, clone_config3.password, clone_config3.host, clone_config3.port, clone_config3.database);
let res = get_data_from_500_com("sd", &db_url).await;
let msg;
match res {
Ok(val) => {
msg = format!("{}: 福彩3D数据采集完毕, 最新数据为: {}!", chrono::Local::now().format(fmt), val);
info!("{}", msg);
},
Err(e) => {
msg = format!("{}: 福彩3D数据采集失败: {}!", chrono::Local::now().format(fmt), e);
error!("{}", msg);
}
}
for notify_url in notify_urls {
let notify = reqwest::get(format!("{}/{}", notify_url, msg)).await.unwrap();
if notify.status().is_success() {
info!("{}: 福彩3D数据采集通知发送成功", chrono::Local::now().format(fmt));
} else {
error!("{}: 福彩3D数据采集通知发送失败", chrono::Local::now().format(fmt));
}
}
})
});
info!("{}: 开始运行定时任务", chrono::Local::now().format("%Y年%m月%d日 %H:%M:%S"));
loop {
scheduler.run_pending();
thread::sleep(Duration::from_millis(100));
}
}