fix: 更改配置信息从文件中读取
This commit is contained in:
parent
c94d68ac62
commit
983020b55f
|
|
@ -1651,7 +1651,7 @@ version = "3.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284"
|
||||
dependencies = [
|
||||
"toml_edit",
|
||||
"toml_edit 0.21.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -2050,7 +2050,9 @@ dependencies = [
|
|||
"reqwest",
|
||||
"scraper",
|
||||
"sea-orm",
|
||||
"serde",
|
||||
"tokio",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -2253,6 +2255,15 @@ dependencies = [
|
|||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_spanned"
|
||||
version = "0.6.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb5b1b31579f3811bf615c144393417496f152e12ac8b7663bf664f4a815306d"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_urlencoded"
|
||||
version = "0.7.1"
|
||||
|
|
@ -2898,10 +2909,25 @@ dependencies = [
|
|||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.6.7"
|
||||
name = "toml"
|
||||
version = "0.8.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f8fb9f64314842840f1d940ac544da178732128f1c78c21772e876579e0da1db"
|
||||
checksum = "a1ed1f98e3fdc28d6d910e6737ae6ab1a93bf1985935a1193e68f93eeb68d24e"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"toml_edit 0.22.20",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_datetime"
|
||||
version = "0.6.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0dd7358ecb8fc2f8d014bf86f6f638ce72ba252a2c3a2572f2a795f1d23efb41"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_edit"
|
||||
|
|
@ -2911,7 +2937,20 @@ checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1"
|
|||
dependencies = [
|
||||
"indexmap",
|
||||
"toml_datetime",
|
||||
"winnow",
|
||||
"winnow 0.5.40",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "toml_edit"
|
||||
version = "0.22.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "583c44c02ad26b0c3f3066fe629275e50627026c51ac2e595cca4c230ce1ce1d"
|
||||
dependencies = [
|
||||
"indexmap",
|
||||
"serde",
|
||||
"serde_spanned",
|
||||
"toml_datetime",
|
||||
"winnow 0.6.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -3381,6 +3420,15 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winnow"
|
||||
version = "0.6.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68a9bda4691f099d435ad181000724da8e5899daa10713c2d432552b9ccd3a6f"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winreg"
|
||||
version = "0.52.0"
|
||||
|
|
|
|||
|
|
@ -14,3 +14,5 @@ reqwest = { version = "0.12.5", features = ["gzip"] }
|
|||
scraper = "0.19.1"
|
||||
sea-orm = { version = "0.12", features = [ "sqlx-mysql", "runtime-tokio-rustls", "macros" ]}
|
||||
tokio = { version = "1.39.2", features = ["full"] }
|
||||
serde = { version = "1.0.204", features = ["derive", "serde_derive"] }
|
||||
toml = "0.8.19"
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ ENV RUST_LOG=info
|
|||
|
||||
ENV RUST_ENV=prod
|
||||
|
||||
RUN mkdir -p /etc/sched_rs
|
||||
COPY ./target/x86_64-unknown-linux-musl/release/sched_tasks_rs /bin/sched_tasks_rs
|
||||
|
||||
COPY ./config.toml /etc/sched_rs/config.toml
|
||||
|
||||
ENTRYPOINT ["/bin/sched_tasks_rs"]
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
db_user = "root"
|
||||
db_passwd = "Chenweijia113!"
|
||||
db_host = "mysql"
|
||||
db_port = 3306
|
||||
db_name = "lottery"
|
||||
notifyUrls = [
|
||||
"https://ding.airpig.cn/QfagvFa7u323xUS5yRbFR3",
|
||||
]
|
||||
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use serde::Deserialize;
|
||||
use toml;
|
||||
|
||||
#[derive(Clone, Deserialize)]
|
||||
pub struct Config {
|
||||
#[serde(rename = "db_user")]
|
||||
pub username: String,
|
||||
#[serde(rename = "db_passwd")]
|
||||
pub password: String,
|
||||
#[serde(rename = "db_host")]
|
||||
pub host: String,
|
||||
#[serde(rename = "db_port")]
|
||||
pub port: u16,
|
||||
#[serde(rename = "db_name")]
|
||||
pub database: String,
|
||||
#[serde(rename = "notifyUrls")]
|
||||
pub notify_urls: Vec<String>,
|
||||
}
|
||||
|
||||
pub fn read_config(file_path: &str) -> Result<Config, Box<dyn Error>> {
|
||||
let mut buf = String::new();
|
||||
File::open(file_path)?.read_to_string(&mut buf)?;
|
||||
toml::from_str(&buf).map_err(|e| e.into())
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
pub mod config;
|
||||
|
||||
pub use config::read_config;
|
||||
35
src/main.rs
35
src/main.rs
|
|
@ -1,4 +1,4 @@
|
|||
use std::{thread, time::Duration};
|
||||
use std::{env, sync::Arc, thread, time::Duration};
|
||||
|
||||
use clokwerk::{Scheduler, TimeUnits, Job};
|
||||
|
||||
|
|
@ -6,16 +6,27 @@ 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 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 res = get_data_from_500_com("pls").await;
|
||||
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) => {
|
||||
|
|
@ -27,19 +38,23 @@ fn main() {
|
|||
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 res = get_data_from_500_com("plw").await;
|
||||
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) => {
|
||||
|
|
@ -51,19 +66,23 @@ fn main() {
|
|||
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 res = get_data_from_500_com("sd").await;
|
||||
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) => {
|
||||
|
|
@ -75,12 +94,14 @@ fn main() {
|
|||
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,5 +1,5 @@
|
|||
|
||||
use std::{env, time::Duration};
|
||||
use std::time::Duration;
|
||||
|
||||
use chrono::{Local, NaiveDate};
|
||||
use regex::Regex;
|
||||
|
|
@ -10,15 +10,15 @@ use sea_orm::{ActiveModelTrait, ConnectOptions, Database};
|
|||
use crate::model::prelude::*;
|
||||
use crate::model::{sd, pls, plw};
|
||||
|
||||
pub async fn get_data_from_500_com(issue_type: &str) -> Result<String, String> {
|
||||
pub async fn get_data_from_500_com(issue_type: &str, db_url:&str) -> Result<String, String> {
|
||||
|
||||
// 数据库连接操作
|
||||
let db_url;
|
||||
if env::var("RUST_ENV").unwrap_or_else(|_|"dev".to_string()) == "dev" {
|
||||
db_url = "mysql://root:Chenweijia113!@localhost/lottery";
|
||||
} else {
|
||||
db_url = "mysql://root:Chenweijia113!@mysql/lottery";
|
||||
}
|
||||
// let db_url;
|
||||
// if env::var("RUST_ENV").unwrap_or_else(|_|"dev".to_string()) == "dev" {
|
||||
// db_url = "mysql://root:Chenweijia113!@localhost/lottery";
|
||||
// } else {
|
||||
// db_url = "mysql://root:Chenweijia113!@mysql/lottery";
|
||||
// }
|
||||
let mut opt = ConnectOptions::new(db_url);
|
||||
opt.max_connections(100)
|
||||
.min_connections(5)
|
||||
|
|
|
|||
Loading…
Reference in New Issue