Compare commits

...

5 Commits
master ... dev

Author SHA1 Message Date
chenwj 27aa6a1de8 fix: 修改gitea action ci/cd文件
Continuous integration / Build (push) Successful in 4m2s Details
2024-09-05 10:45:33 +08:00
chenwj 32435d94ad fix: 更改gitea action文件 2024-08-21 13:32:30 +08:00
chenwj 60fe1a04f2 fix: 更改配置信息为全局静态变量存储 2024-08-21 10:31:55 +08:00
chenwj 347ea99eeb fix: 增加触发时间从文件读取的功能 2024-08-09 14:06:05 +08:00
chenwj 983020b55f fix: 更改配置信息从文件中读取 2024-08-09 11:14:43 +08:00
10 changed files with 449 additions and 259 deletions

1
.env
View File

@ -1 +1,2 @@
DATABASE_URL=mysql://root:Chenweijia113!@localhost/lottery
version = "1.0.8"

View File

@ -2,11 +2,69 @@ on: [push, pull_request]
name: Continuous integration
env:
VERSION_FILE: .env
REPO: sched_task
jobs:
check:
name: Check
build:
name: Build
runs-on: ubuntu-latest
continue-on-error: true
steps:
- uses: actions/checkout@v4
- uses: actions-rust-lang/setup-rust-toolchain@v1
- run: cargo check
- name: Checkout
uses: https://gitea.com/actions/checkout@v4
- name: Setup Rust
uses: https://git.airpig.cn/actions/setup-rust-toolchain@v1
- name: Add target for musl
run: |
apt update && apt install -y musl-tools
rustup target add x86_64-unknown-linux-musl
- name: Cache cargo registry and build
uses: https://gitea.com/actions/cache@v3
with:
path: |
~/.cargo/bin/
~/.cargo/registry/index/
~/.cargo/registry/cache/
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
restore-keys: |
${{ runner.os }}-cargo-
- name: Cargo build release for musl
run: |
cargo build -r --target x86_64-unknown-linux-musl
- name: Login to hub.airpig.cn
uses: https://gitea.com/docker/login-action@v3
with:
registry: hub.airpig.cn
username: admin
password: Chenweijia113!
- name: Build Docker image and push
run: |
version=$(grep -E '^version' $VERSION_FILE | awk -F'"' '{print $2}')
new_version="v${version}"
docker build -t hub.airpig.cn/library/$REPO:$new_version .
docker push hub.airpig.cn/library/$REPO:$new_version
docker rmi hub.airpig.cn/library/$REPO:$new_version
- name: Bark Notify if success
if: success()
run: |
version=$(grep -E '^version' $VERSION_FILE | awk -F'"' '{print $2}')
msg="Build job: $REPO, version: v$version succeeded\!\!\!"
curl https://ding.airpig.cn/QfagvFa7u323xUS5yRbFR3/"${msg// /%20}"
- name: Bark Notify if failure
if: failure()
run: |
version=$(grep -E '^version' $VERSION_FILE | awk -F'"' '{print $2}')
msg="Build job: $REPO, version: v$version failed\!\!\!"
curl https://ding.airpig.cn/QfagvFa7u323xUS5yRbFR3/"${msg// /%20}"

414
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -14,3 +14,6 @@ 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"
lazy_static = "1.5.0"

View File

@ -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"]

9
config.toml Normal file
View File

@ -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",
]

30
src/config/config.rs Normal file
View File

@ -0,0 +1,30 @@
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 pls_cron: Option<String>,
pub plw_cron: Option<String>,
pub sd_cron: Option<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())
}

3
src/config/mod.rs Normal file
View File

@ -0,0 +1,3 @@
pub mod config;
pub use config::read_config;

View File

@ -1,86 +1,62 @@
use std::{thread, time::Duration};
use std::{env, thread, time::Duration};
use clokwerk::{Scheduler, TimeUnits, Job};
use log::{info, error};
use get_data::notify_msg;
use log::info;
use lazy_static::lazy_static;
mod tasks;
use tasks::*;
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() {
env_logger::init();
let mut scheduler = Scheduler::new();
let fmt = "%Y年%m月%d日 %H:%M:%S";
let notify_url = "https://ding.airpig.cn/QfagvFa7u323xUS5yRbFR3";
scheduler.every(1.day()).at("00:01:00").run(move|| {
// let fmt = "%Y年%m月%d日 %H:%M:%S";
// 在多个定时任务之间分享配置信息
// 排列三数据采集
let pls_cron = CONFIG.clone().pls_cron.unwrap_or("00:01:00".to_string());
scheduler.every(1.day()).at(&pls_cron).run(move|| {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let res = get_data_from_500_com("pls").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);
}
}
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 res = get_data_from_500_com("pls", &DB_URL).await;
let _ = notify_msg("pls", &NOTIFY_URLS, res).await;
})
});
scheduler.every(1.day()).at("00:01:15").run(move|| {
// 排列五数据采集
let plw_cron = CONFIG.clone().plw_cron.unwrap_or("00:01:15".to_string());
scheduler.every(1.day()).at(&plw_cron).run(move|| {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let res = get_data_from_500_com("plw").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);
}
}
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 res = get_data_from_500_com("plw", &DB_URL).await;
let _ = notify_msg("plw", &NOTIFY_URLS, res).await;
})
});
scheduler.every(1.day()).at("00:01:30").run(move|| {
// 福彩3D数据采集
let sd_cron = CONFIG.clone().sd_cron.unwrap_or("00:01:30".to_string());
scheduler.every(1.day()).at(&sd_cron).run(move|| {
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
let res = get_data_from_500_com("sd").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);
}
}
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));
}
let res = get_data_from_500_com("sd", &DB_URL).await;
let _ = notify_msg("sd", &NOTIFY_URLS, res).await;
})
});

View File

@ -1,6 +1,6 @@
use std::{env, time::Duration};
use std::time::Duration;
use log::{info, error};
use chrono::{Local, NaiveDate};
use regex::Regex;
use scraper::{Html, Selector};
@ -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)
@ -157,3 +157,36 @@ pub async fn get_data_from_500_com(issue_type: &str) -> Result<String, String> {
}
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(())
}