|
@@ -6,7 +6,7 @@ use strategy::{exchange_disguise, core};
|
|
|
use std::sync::Arc;
|
|
use std::sync::Arc;
|
|
|
use std::sync::atomic::{AtomicBool};
|
|
use std::sync::atomic::{AtomicBool};
|
|
|
use std::time::Duration;
|
|
use std::time::Duration;
|
|
|
-use chrono::{Local, Timelike, Utc};
|
|
|
|
|
|
|
+use chrono::{Datelike, Local, TimeZone, Utc};
|
|
|
use tokio::sync::{mpsc, Mutex};
|
|
use tokio::sync::{mpsc, Mutex};
|
|
|
use tokio::time::{sleep_until, Instant};
|
|
use tokio::time::{sleep_until, Instant};
|
|
|
use tracing::{error, info};
|
|
use tracing::{error, info};
|
|
@@ -127,34 +127,36 @@ pub async fn init(params: Params,
|
|
|
// 定时交易量更新
|
|
// 定时交易量更新
|
|
|
let trade_volume_core_arc = core_arc.clone();
|
|
let trade_volume_core_arc = core_arc.clone();
|
|
|
tokio::spawn(async move {
|
|
tokio::spawn(async move {
|
|
|
- info!("1周交易量统计定时任务启动(1hour)...");
|
|
|
|
|
|
|
+ info!("交易量统计定时任务启动(每天早上8:30)...");
|
|
|
loop {
|
|
loop {
|
|
|
- // 获取当前时间并计算下一个整点
|
|
|
|
|
let now = Local::now();
|
|
let now = Local::now();
|
|
|
- let truncated_now = now
|
|
|
|
|
- .with_minute(0)
|
|
|
|
|
- .unwrap()
|
|
|
|
|
- .with_second(0)
|
|
|
|
|
- .unwrap()
|
|
|
|
|
- .with_nanosecond(0)
|
|
|
|
|
- .unwrap();
|
|
|
|
|
-
|
|
|
|
|
- let next_hour = truncated_now + chrono::Duration::hours(1);
|
|
|
|
|
-
|
|
|
|
|
- // 计算需要等待的时间
|
|
|
|
|
- let wait_duration = next_hour - now;
|
|
|
|
|
- let wait_secs = wait_duration.num_seconds() as u64;
|
|
|
|
|
-
|
|
|
|
|
- if wait_secs > 0 {
|
|
|
|
|
- sleep_until(Instant::now() + Duration::from_secs(wait_secs)).await;
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 使用 and_hms_opt 方法构建今天早上8:30的时间
|
|
|
|
|
+ let target_time_opt = Local.with_ymd_and_hms(now.year(), now.month(), now.day(), 14, 0, 0);
|
|
|
|
|
+
|
|
|
|
|
+ // 检查时间构建是否成功
|
|
|
|
|
+ let mut target_time = match target_time_opt.single() {
|
|
|
|
|
+ Some(time) => time,
|
|
|
|
|
+ None => panic!("Invalid time!"), // 处理无效时间的情况
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // 如果当前时间已经超过今天的8:30,则调整为明天的8:30
|
|
|
|
|
+ if now >= target_time {
|
|
|
|
|
+ target_time = target_time + chrono::Duration::days(1);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- // 更新近7天的交易量统计
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 将目标时间转换为 Instant
|
|
|
|
|
+ let duration_to_wait = (target_time - now).to_std().unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ sleep_until(Instant::now() + duration_to_wait).await;
|
|
|
|
|
+ // 防止过快循环,等待一分钟后再继续下一次循环
|
|
|
|
|
+
|
|
|
|
|
+ // 更新近1天(昨8点~今8点)的交易量统计
|
|
|
let mut core = trade_volume_core_arc.lock().await;
|
|
let mut core = trade_volume_core_arc.lock().await;
|
|
|
core.update_trade_volume().await;
|
|
core.update_trade_volume().await;
|
|
|
|
|
|
|
|
// 等待一分钟以避免过快地循环
|
|
// 等待一分钟以避免过快地循环
|
|
|
- sleep_until(Instant::now() + Duration::from_secs(60)).await;
|
|
|
|
|
|
|
+ sleep_until(Instant::now() + Duration::from_secs(5*60)).await;
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|