fix: 更改配置信息为全局静态变量存储
This commit is contained in:
parent
347ea99eeb
commit
60fe1a04f2
|
|
@ -2044,6 +2044,7 @@ dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"clokwerk",
|
"clokwerk",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
"lazy_static",
|
||||||
"log",
|
"log",
|
||||||
"openssl",
|
"openssl",
|
||||||
"regex",
|
"regex",
|
||||||
|
|
|
||||||
|
|
@ -16,3 +16,4 @@ sea-orm = { version = "0.12", features = [ "sqlx-mysql", "runtime-tokio-rustls",
|
||||||
tokio = { version = "1.39.2", features = ["full"] }
|
tokio = { version = "1.39.2", features = ["full"] }
|
||||||
serde = { version = "1.0.204", features = ["derive", "serde_derive"] }
|
serde = { version = "1.0.204", features = ["derive", "serde_derive"] }
|
||||||
toml = "0.8.19"
|
toml = "0.8.19"
|
||||||
|
lazy_static = "1.5.0"
|
||||||
|
|
|
||||||
122
src/main.rs
122
src/main.rs
|
|
@ -1,114 +1,62 @@
|
||||||
use std::{env, sync::Arc, thread, time::Duration};
|
use std::{env, thread, time::Duration};
|
||||||
|
|
||||||
use clokwerk::{Scheduler, TimeUnits, Job};
|
use clokwerk::{Scheduler, TimeUnits, Job};
|
||||||
|
use get_data::notify_msg;
|
||||||
use log::{info, error};
|
use log::info;
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
mod tasks;
|
mod tasks;
|
||||||
use tasks::*;
|
use tasks::*;
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
static ref CONFIG: config::config::Config = {
|
||||||
|
let file_path = if env::var("RUST_ENV").unwrap_or_else(|_|"dev".to_string()) == "dev" {
|
||||||
|
"./config.toml"
|
||||||
|
} else {
|
||||||
|
"/etc/sched_rs/config.toml"
|
||||||
|
};
|
||||||
|
config::read_config(file_path).unwrap()
|
||||||
|
};
|
||||||
|
static ref DB_URL: String = {
|
||||||
|
format!("mysql://{}:{}@{}:{}/{}", CONFIG.username, CONFIG.password, CONFIG.host, CONFIG.port, CONFIG.database)
|
||||||
|
};
|
||||||
|
static ref NOTIFY_URLS : Vec<String> = {
|
||||||
|
CONFIG.notify_urls.iter().map(|url| url.to_string()).collect()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
let mut scheduler = Scheduler::new();
|
let mut scheduler = Scheduler::new();
|
||||||
let fmt = "%Y年%m月%d日 %H:%M:%S";
|
// 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_clone = config.clone();
|
|
||||||
let pls_cron = config_clone.pls_cron.unwrap_or("00:01:00".to_string());
|
|
||||||
let plw_cron = config_clone.plw_cron.unwrap_or("00:01:15".to_string());
|
|
||||||
let sd_cron = config_clone.sd_cron.unwrap_or("00:01:30".to_string());
|
|
||||||
|
|
||||||
// 在多个定时任务之间分享配置信息
|
// 在多个定时任务之间分享配置信息
|
||||||
let config = Arc::new(config);
|
// 排列三数据采集
|
||||||
let clone_config1 = Arc::clone(&config);
|
let pls_cron = CONFIG.clone().pls_cron.unwrap_or("00:01:00".to_string());
|
||||||
scheduler.every(1.day()).at(&pls_cron).run(move|| {
|
scheduler.every(1.day()).at(&pls_cron).run(move|| {
|
||||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
rt.block_on(async {
|
rt.block_on(async {
|
||||||
let notify_urls = clone_config1.notify_urls.iter().map(|url| url.as_str()).collect::<Vec<&str>>();
|
let res = get_data_from_500_com("pls", &DB_URL).await;
|
||||||
let db_url = format!("mysql://{}:{}@{}:{}/{}", clone_config1.username, clone_config1.password, clone_config1.host, clone_config1.port, clone_config1.database);
|
let _ = notify_msg("pls", &NOTIFY_URLS, res).await;
|
||||||
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);
|
// 排列五数据采集
|
||||||
|
let plw_cron = CONFIG.clone().plw_cron.unwrap_or("00:01:15".to_string());
|
||||||
scheduler.every(1.day()).at(&plw_cron).run(move|| {
|
scheduler.every(1.day()).at(&plw_cron).run(move|| {
|
||||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
rt.block_on(async {
|
rt.block_on(async {
|
||||||
let notify_urls = clone_config2.notify_urls.iter().map(|url| url.as_str()).collect::<Vec<&str>>();
|
let res = get_data_from_500_com("plw", &DB_URL).await;
|
||||||
let db_url = format!("mysql://{}:{}@{}:{}/{}", clone_config2.username, clone_config2.password, clone_config2.host, clone_config2.port, clone_config2.database);
|
let _ = notify_msg("plw", &NOTIFY_URLS, res).await;
|
||||||
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);
|
// 福彩3D数据采集
|
||||||
|
let sd_cron = CONFIG.clone().sd_cron.unwrap_or("00:01:30".to_string());
|
||||||
scheduler.every(1.day()).at(&sd_cron).run(move|| {
|
scheduler.every(1.day()).at(&sd_cron).run(move|| {
|
||||||
let rt = tokio::runtime::Runtime::new().unwrap();
|
let rt = tokio::runtime::Runtime::new().unwrap();
|
||||||
rt.block_on(async {
|
rt.block_on(async {
|
||||||
let notify_urls = clone_config3.notify_urls.iter().map(|url| url.as_str()).collect::<Vec<&str>>();
|
let res = get_data_from_500_com("sd", &DB_URL).await;
|
||||||
let db_url = format!("mysql://{}:{}@{}:{}/{}", clone_config3.username, clone_config3.password, clone_config3.host, clone_config3.port, clone_config3.database);
|
let _ = notify_msg("sd", &NOTIFY_URLS, res).await;
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use log::{info, error};
|
||||||
use chrono::{Local, NaiveDate};
|
use chrono::{Local, NaiveDate};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use scraper::{Html, Selector};
|
use scraper::{Html, Selector};
|
||||||
|
|
@ -20,15 +20,15 @@ pub async fn get_data_from_500_com(issue_type: &str, db_url:&str) -> Result<Stri
|
||||||
// db_url = "mysql://root:Chenweijia113!@mysql/lottery";
|
// db_url = "mysql://root:Chenweijia113!@mysql/lottery";
|
||||||
// }
|
// }
|
||||||
let mut opt = ConnectOptions::new(db_url);
|
let mut opt = ConnectOptions::new(db_url);
|
||||||
opt.max_connections(100)
|
opt.max_connections(100)
|
||||||
.min_connections(5)
|
.min_connections(5)
|
||||||
.connect_timeout(Duration::from_secs(8))
|
.connect_timeout(Duration::from_secs(8))
|
||||||
.acquire_timeout(Duration::from_secs(8))
|
.acquire_timeout(Duration::from_secs(8))
|
||||||
.idle_timeout(Duration::from_secs(8))
|
.idle_timeout(Duration::from_secs(8))
|
||||||
.max_lifetime(Duration::from_secs(8))
|
.max_lifetime(Duration::from_secs(8))
|
||||||
.sqlx_logging(true)
|
.sqlx_logging(true)
|
||||||
.sqlx_logging_level(log::LevelFilter::Info)
|
.sqlx_logging_level(log::LevelFilter::Info)
|
||||||
.set_schema_search_path("my_schema");
|
.set_schema_search_path("my_schema");
|
||||||
let db = Database::connect(opt).await.expect("Failed to connect to database");
|
let db = Database::connect(opt).await.expect("Failed to connect to database");
|
||||||
|
|
||||||
let url = format!("https://kaijiang.500.com/{}.shtml", issue_type);
|
let url = format!("https://kaijiang.500.com/{}.shtml", issue_type);
|
||||||
|
|
@ -157,3 +157,36 @@ pub async fn get_data_from_500_com(issue_type: &str, db_url:&str) -> Result<Stri
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn notify_msg(issue_type: &str, notify_urls: &Vec<String>, res: Result<String, String>) -> Result<(), String> {
|
||||||
|
let lottery_type = match issue_type {
|
||||||
|
"ssq" => "双色球",
|
||||||
|
"dlt" => "大乐透",
|
||||||
|
"pls" => "排列3",
|
||||||
|
"plw" => "排列5",
|
||||||
|
"sd" => "福彩3D",
|
||||||
|
_ => "未知",
|
||||||
|
};
|
||||||
|
let fmt = "%Y年%m月%d日 %H:%M:%S";
|
||||||
|
let msg = match res {
|
||||||
|
Ok(val) => {
|
||||||
|
let msg = format!("{}: {}数据采集完毕, 最新数据为: {}!", chrono::Local::now().format(fmt), lottery_type, val);
|
||||||
|
info!("{}", msg);
|
||||||
|
msg
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
let msg = format!("{}: {}数据采集失败: {}!", chrono::Local::now().format(fmt), lottery_type, e);
|
||||||
|
error!("{}", msg);
|
||||||
|
msg
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for notify_url in notify_urls {
|
||||||
|
let notify = reqwest::get(format!("{}/{}", notify_url, msg)).await.unwrap();
|
||||||
|
if notify.status().is_success() {
|
||||||
|
info!("{}: {}数据采集通知发送成功", chrono::Local::now().format(fmt), lottery_type);
|
||||||
|
} else {
|
||||||
|
error!("{}: {}数据采集通知发送失败", chrono::Local::now().format(fmt), lottery_type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue