2025-08-01 23:02:11 +08:00
|
|
|
|
use crate::{config::Config, feat, logging, logging_error, singleton, utils::logging::Type};
|
2022-09-11 20:58:55 +08:00
|
|
|
|
use anyhow::{Context, Result};
|
2022-04-20 20:37:16 +08:00
|
|
|
|
use delay_timer::prelude::{DelayTimer, DelayTimerBuilder, TaskBuilder};
|
2025-08-01 23:02:11 +08:00
|
|
|
|
use parking_lot::RwLock;
|
2025-10-22 16:25:44 +08:00
|
|
|
|
use smartstring::alias::String;
|
2025-08-01 23:02:11 +08:00
|
|
|
|
use std::{
|
|
|
|
|
|
collections::HashMap,
|
2025-08-26 01:49:51 +08:00
|
|
|
|
pin::Pin,
|
2025-08-01 23:02:11 +08:00
|
|
|
|
sync::{
|
2025-09-08 21:48:09 +08:00
|
|
|
|
Arc,
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
|
atomic::{AtomicBool, AtomicU64, Ordering},
|
2025-08-01 23:02:11 +08:00
|
|
|
|
},
|
|
|
|
|
|
};
|
2022-04-19 01:41:20 +08:00
|
|
|
|
|
2022-04-20 20:37:16 +08:00
|
|
|
|
type TaskID = u64;
|
|
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
#[derive(Debug, Clone)]
|
2025-03-20 06:01:38 +08:00
|
|
|
|
pub struct TimerTask {
|
|
|
|
|
|
pub task_id: TaskID,
|
|
|
|
|
|
pub interval_minutes: u64,
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
|
|
pub last_run: i64, // Timestamp of last execution
|
2025-03-15 18:58:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-19 01:41:20 +08:00
|
|
|
|
pub struct Timer {
|
2022-11-12 11:37:23 +08:00
|
|
|
|
/// cron manager
|
2025-03-20 06:01:38 +08:00
|
|
|
|
pub delay_timer: Arc<RwLock<DelayTimer>>,
|
2022-04-19 01:41:20 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
/// save the current state - using RwLock for better read concurrency
|
2025-03-20 06:01:38 +08:00
|
|
|
|
pub timer_map: Arc<RwLock<HashMap<String, TimerTask>>>,
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-08-01 23:02:11 +08:00
|
|
|
|
/// increment id - atomic counter for better performance
|
|
|
|
|
|
pub timer_count: AtomicU64,
|
2025-03-12 22:36:25 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
/// Flag to mark if timer is initialized - atomic for better performance
|
2025-08-01 23:02:11 +08:00
|
|
|
|
pub initialized: AtomicBool,
|
2022-04-19 01:41:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-01 23:02:11 +08:00
|
|
|
|
// Use singleton macro
|
|
|
|
|
|
singleton!(Timer, TIMER_INSTANCE);
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
2025-08-01 23:02:11 +08:00
|
|
|
|
impl Timer {
|
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
|
Timer {
|
2025-03-15 18:58:12 +08:00
|
|
|
|
delay_timer: Arc::new(RwLock::new(DelayTimerBuilder::default().build())),
|
|
|
|
|
|
timer_map: Arc::new(RwLock::new(HashMap::new())),
|
2025-08-01 23:02:11 +08:00
|
|
|
|
timer_count: AtomicU64::new(1),
|
|
|
|
|
|
initialized: AtomicBool::new(false),
|
|
|
|
|
|
}
|
2022-11-14 01:26:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
/// Initialize timer with better error handling and atomic operations
|
2025-08-26 01:49:51 +08:00
|
|
|
|
pub async fn init(&self) -> Result<()> {
|
2025-03-15 18:58:12 +08:00
|
|
|
|
// Use compare_exchange for thread-safe initialization check
|
|
|
|
|
|
if self
|
|
|
|
|
|
.initialized
|
2025-08-01 23:02:11 +08:00
|
|
|
|
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
|
2025-03-15 18:58:12 +08:00
|
|
|
|
.is_err()
|
|
|
|
|
|
{
|
2025-04-05 11:34:51 +08:00
|
|
|
|
logging!(debug, Type::Timer, "Timer already initialized, skipping...");
|
2025-03-12 22:36:25 +08:00
|
|
|
|
return Ok(());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
// Initialize timer tasks
|
2025-08-26 01:49:51 +08:00
|
|
|
|
if let Err(e) = self.refresh().await {
|
2025-03-15 18:58:12 +08:00
|
|
|
|
// Reset initialization flag on error
|
2025-08-01 23:02:11 +08:00
|
|
|
|
self.initialized.store(false, Ordering::SeqCst);
|
Refactor logging macros to remove print control parameter
- Updated logging macros to eliminate the boolean parameter for print control, simplifying the logging calls throughout the codebase.
- Adjusted all logging calls in various modules (lib.rs, lightweight.rs, help.rs, init.rs, logging.rs, resolve/mod.rs, resolve/scheme.rs, resolve/ui.rs, resolve/window.rs, server.rs, singleton.rs, window_manager.rs) to reflect the new macro structure.
- Ensured consistent logging behavior across the application by standardizing the logging format.
2025-10-10 13:05:01 +08:00
|
|
|
|
logging_error!(Type::Timer, "Failed to initialize timer: {}", e);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
return Err(e);
|
|
|
|
|
|
}
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
// Log timer info first
|
|
|
|
|
|
{
|
|
|
|
|
|
let timer_map = self.timer_map.read();
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Timer,
|
2025-08-26 01:49:51 +08:00
|
|
|
|
"已注册的定时任务数量: {}",
|
|
|
|
|
|
timer_map.len()
|
2025-04-25 17:17:34 +08:00
|
|
|
|
);
|
2025-08-26 01:49:51 +08:00
|
|
|
|
|
|
|
|
|
|
for (uid, task) in timer_map.iter() {
|
|
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"注册了定时任务 - uid={}, interval={}min, task_id={}",
|
|
|
|
|
|
uid,
|
|
|
|
|
|
task.interval_minutes,
|
|
|
|
|
|
task.task_id
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-04-25 17:17:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-14 01:26:33 +08:00
|
|
|
|
let cur_timestamp = chrono::Local::now().timestamp();
|
|
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
// Collect profiles that need immediate update
|
2025-08-26 01:49:51 +08:00
|
|
|
|
let profiles_to_update =
|
|
|
|
|
|
if let Some(items) = Config::profiles().await.latest_ref().get_items() {
|
|
|
|
|
|
items
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
.filter_map(|item| {
|
|
|
|
|
|
let interval = item.option.as_ref()?.update_interval? as i64;
|
|
|
|
|
|
let updated = item.updated? as i64;
|
|
|
|
|
|
let uid = item.uid.as_ref()?;
|
|
|
|
|
|
|
|
|
|
|
|
if interval > 0 && cur_timestamp - updated >= interval * 60 {
|
|
|
|
|
|
logging!(info, Type::Timer, "需要立即更新的配置: uid={}", uid);
|
|
|
|
|
|
Some(uid.clone())
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
.collect::<Vec<String>>()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Vec::new()
|
|
|
|
|
|
};
|
2025-03-15 18:58:12 +08:00
|
|
|
|
|
|
|
|
|
|
// Advance tasks outside of locks to minimize lock contention
|
|
|
|
|
|
if !profiles_to_update.is_empty() {
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"需要立即更新的配置数量: {}",
|
|
|
|
|
|
profiles_to_update.len()
|
|
|
|
|
|
);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
let timer_map = self.timer_map.read();
|
|
|
|
|
|
let delay_timer = self.delay_timer.write();
|
|
|
|
|
|
|
|
|
|
|
|
for uid in profiles_to_update {
|
|
|
|
|
|
if let Some(task) = timer_map.get(&uid) {
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(info, Type::Timer, "立即执行任务: uid={}", uid);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
if let Err(e) = delay_timer.advance_task(task.task_id) {
|
2025-04-05 11:34:51 +08:00
|
|
|
|
logging!(warn, Type::Timer, "Failed to advance task {}: {}", uid, e);
|
2022-11-14 01:26:33 +08:00
|
|
|
|
}
|
2025-03-15 18:58:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-01-10 17:36:35 +08:00
|
|
|
|
}
|
2022-11-14 01:26:33 +08:00
|
|
|
|
|
2025-04-05 11:34:51 +08:00
|
|
|
|
logging!(info, Type::Timer, "Timer initialization completed");
|
2022-11-14 01:26:33 +08:00
|
|
|
|
Ok(())
|
2022-04-20 20:37:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-08 12:32:40 +08:00
|
|
|
|
/// 每 3 秒更新系统托盘菜单,总共执行 3 次
|
|
|
|
|
|
pub fn add_update_tray_menu_task(&self) -> Result<()> {
|
|
|
|
|
|
let tid = self.timer_count.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
|
let delay_timer = self.delay_timer.write();
|
|
|
|
|
|
let task = TaskBuilder::default()
|
|
|
|
|
|
.set_task_id(tid)
|
|
|
|
|
|
.set_maximum_parallel_runnable_num(1)
|
|
|
|
|
|
.set_frequency_count_down_by_seconds(3, 3)
|
|
|
|
|
|
.spawn_async_routine(|| async move {
|
|
|
|
|
|
logging!(info, Type::Timer, "Updating tray menu");
|
|
|
|
|
|
crate::core::tray::Tray::global()
|
|
|
|
|
|
.update_tray_display()
|
|
|
|
|
|
.await
|
|
|
|
|
|
})
|
|
|
|
|
|
.context("failed to create update tray menu timer task")?;
|
|
|
|
|
|
delay_timer
|
|
|
|
|
|
.add_task(task)
|
|
|
|
|
|
.context("failed to add update tray menu timer task")?;
|
|
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
/// Refresh timer tasks with better error handling
|
2025-08-26 01:49:51 +08:00
|
|
|
|
pub async fn refresh(&self) -> Result<()> {
|
2025-03-15 18:58:12 +08:00
|
|
|
|
// Generate diff outside of lock to minimize lock contention
|
2025-08-26 01:49:51 +08:00
|
|
|
|
let diff_map = self.gen_diff().await;
|
2022-11-12 11:37:23 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
if diff_map.is_empty() {
|
2025-04-05 11:34:51 +08:00
|
|
|
|
logging!(debug, Type::Timer, "No timer changes needed");
|
2025-03-15 18:58:12 +08:00
|
|
|
|
return Ok(());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-05 11:34:51 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"Refreshing {} timer tasks",
|
|
|
|
|
|
diff_map.len()
|
|
|
|
|
|
);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
// Apply changes - first collect operations to perform without holding locks
|
|
|
|
|
|
let mut operations_to_add: Vec<(String, TaskID, u64)> = Vec::new();
|
|
|
|
|
|
let _operations_to_remove: Vec<String> = Vec::new();
|
|
|
|
|
|
|
|
|
|
|
|
// Perform sync operations while holding locks
|
|
|
|
|
|
{
|
|
|
|
|
|
let mut timer_map = self.timer_map.write();
|
|
|
|
|
|
let delay_timer = self.delay_timer.write();
|
|
|
|
|
|
|
|
|
|
|
|
for (uid, diff) in diff_map {
|
|
|
|
|
|
match diff {
|
|
|
|
|
|
DiffFlag::Del(tid) => {
|
|
|
|
|
|
timer_map.remove(&uid);
|
|
|
|
|
|
if let Err(e) = delay_timer.remove_task(tid) {
|
|
|
|
|
|
logging!(
|
|
|
|
|
|
warn,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"Failed to remove task {} for uid {}: {}",
|
|
|
|
|
|
tid,
|
|
|
|
|
|
uid,
|
|
|
|
|
|
e
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
logging!(debug, Type::Timer, "Removed task {} for uid {}", tid, uid);
|
|
|
|
|
|
}
|
2025-03-15 18:58:12 +08:00
|
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
|
DiffFlag::Add(tid, interval) => {
|
|
|
|
|
|
let task = TimerTask {
|
|
|
|
|
|
task_id: tid,
|
|
|
|
|
|
interval_minutes: interval,
|
|
|
|
|
|
last_run: chrono::Local::now().timestamp(),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
timer_map.insert(uid.clone(), task);
|
|
|
|
|
|
operations_to_add.push((uid, tid, interval));
|
2025-03-15 18:58:12 +08:00
|
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
|
DiffFlag::Mod(tid, interval) => {
|
|
|
|
|
|
// Remove old task first
|
|
|
|
|
|
if let Err(e) = delay_timer.remove_task(tid) {
|
|
|
|
|
|
logging!(
|
|
|
|
|
|
warn,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"Failed to remove old task {} for uid {}: {}",
|
|
|
|
|
|
tid,
|
|
|
|
|
|
uid,
|
|
|
|
|
|
e
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-03-15 18:58:12 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
// Then add the new one
|
|
|
|
|
|
let task = TimerTask {
|
|
|
|
|
|
task_id: tid,
|
|
|
|
|
|
interval_minutes: interval,
|
|
|
|
|
|
last_run: chrono::Local::now().timestamp(),
|
|
|
|
|
|
};
|
2025-03-15 18:58:12 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
timer_map.insert(uid.clone(), task);
|
|
|
|
|
|
operations_to_add.push((uid, tid, interval));
|
2025-03-15 18:58:12 +08:00
|
|
|
|
}
|
2022-11-12 11:37:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-26 01:49:51 +08:00
|
|
|
|
} // Locks are dropped here
|
|
|
|
|
|
|
|
|
|
|
|
// Now perform async operations without holding locks
|
|
|
|
|
|
for (uid, tid, interval) in operations_to_add {
|
|
|
|
|
|
// Re-acquire locks for individual operations
|
|
|
|
|
|
let mut delay_timer = self.delay_timer.write();
|
|
|
|
|
|
if let Err(e) = self.add_task(&mut delay_timer, uid.clone(), tid, interval) {
|
|
|
|
|
|
logging_error!(Type::Timer, "Failed to add task for uid {}: {}", uid, e);
|
|
|
|
|
|
|
|
|
|
|
|
// Rollback on failure - remove from timer_map
|
|
|
|
|
|
self.timer_map.write().remove(&uid);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
logging!(debug, Type::Timer, "Added task {} for uid {}", tid, uid);
|
|
|
|
|
|
}
|
2022-08-23 15:19:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-12 11:37:23 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
2022-08-21 13:33:12 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
/// Generate map of profile UIDs to update intervals
|
2025-08-26 01:49:51 +08:00
|
|
|
|
async fn gen_map(&self) -> HashMap<String, u64> {
|
2022-11-12 11:37:23 +08:00
|
|
|
|
let mut new_map = HashMap::new();
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
if let Some(items) = Config::profiles().await.latest_ref().get_items() {
|
2022-11-12 11:37:23 +08:00
|
|
|
|
for item in items.iter() {
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
|
if let Some(option) = item.option.as_ref()
|
2025-10-18 17:36:42 +08:00
|
|
|
|
&& let Some(allow_auto_update) = option.allow_auto_update
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
|
&& let (Some(interval), Some(uid)) = (option.update_interval, &item.uid)
|
2025-10-18 17:36:42 +08:00
|
|
|
|
&& allow_auto_update
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
|
&& interval > 0
|
|
|
|
|
|
{
|
|
|
|
|
|
logging!(
|
|
|
|
|
|
debug,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"找到定时更新配置: uid={}, interval={}min",
|
|
|
|
|
|
uid,
|
|
|
|
|
|
interval
|
|
|
|
|
|
);
|
|
|
|
|
|
new_map.insert(uid.clone(), interval);
|
2022-11-12 11:37:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-04-20 20:37:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
debug,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"生成的定时更新配置数量: {}",
|
|
|
|
|
|
new_map.len()
|
|
|
|
|
|
);
|
2022-11-12 11:37:23 +08:00
|
|
|
|
new_map
|
|
|
|
|
|
}
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
/// Generate differences between current and new timer configuration
|
2025-08-26 01:49:51 +08:00
|
|
|
|
async fn gen_diff(&self) -> HashMap<String, DiffFlag> {
|
2022-11-12 11:37:23 +08:00
|
|
|
|
let mut diff_map = HashMap::new();
|
2025-08-26 01:49:51 +08:00
|
|
|
|
let new_map = self.gen_map().await;
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
// Read lock for comparing current state
|
|
|
|
|
|
let timer_map = self.timer_map.read();
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
debug,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"当前 timer_map 大小: {}",
|
|
|
|
|
|
timer_map.len()
|
|
|
|
|
|
);
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
// Find tasks to modify or delete
|
|
|
|
|
|
for (uid, task) in timer_map.iter() {
|
|
|
|
|
|
match new_map.get(uid) {
|
|
|
|
|
|
Some(&interval) if interval != task.interval_minutes => {
|
|
|
|
|
|
// Task exists but interval changed
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
debug,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"定时任务间隔变更: uid={}, 旧={}, 新={}",
|
|
|
|
|
|
uid,
|
|
|
|
|
|
task.interval_minutes,
|
|
|
|
|
|
interval
|
|
|
|
|
|
);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
diff_map.insert(uid.clone(), DiffFlag::Mod(task.task_id, interval));
|
|
|
|
|
|
}
|
|
|
|
|
|
None => {
|
|
|
|
|
|
// Task no longer needed
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(debug, Type::Timer, "定时任务已删除: uid={}", uid);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
diff_map.insert(uid.clone(), DiffFlag::Del(task.task_id));
|
|
|
|
|
|
}
|
|
|
|
|
|
_ => {
|
|
|
|
|
|
// Task exists with same interval, no change needed
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(debug, Type::Timer, "定时任务保持不变: uid={}", uid);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
}
|
2022-11-12 11:37:23 +08:00
|
|
|
|
}
|
2025-03-15 18:58:12 +08:00
|
|
|
|
}
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
// Find new tasks to add
|
2025-08-01 23:02:11 +08:00
|
|
|
|
let mut next_id = self.timer_count.load(Ordering::Relaxed);
|
|
|
|
|
|
let original_id = next_id;
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
for (uid, &interval) in new_map.iter() {
|
|
|
|
|
|
if !timer_map.contains_key(uid) {
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
debug,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"新增定时任务: uid={}, interval={}min",
|
|
|
|
|
|
uid,
|
|
|
|
|
|
interval
|
|
|
|
|
|
);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
diff_map.insert(uid.clone(), DiffFlag::Add(next_id, interval));
|
|
|
|
|
|
next_id += 1;
|
2022-11-12 11:37:23 +08:00
|
|
|
|
}
|
2025-03-15 18:58:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Update counter only if we added new tasks
|
2025-08-01 23:02:11 +08:00
|
|
|
|
if next_id > original_id {
|
|
|
|
|
|
self.timer_count.store(next_id, Ordering::Relaxed);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
}
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(debug, Type::Timer, "定时任务变更数量: {}", diff_map.len());
|
2022-11-12 11:37:23 +08:00
|
|
|
|
diff_map
|
|
|
|
|
|
}
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
/// Add a timer task with better error handling
|
2022-11-14 01:45:58 +08:00
|
|
|
|
fn add_task(
|
|
|
|
|
|
&self,
|
|
|
|
|
|
delay_timer: &mut DelayTimer,
|
|
|
|
|
|
uid: String,
|
|
|
|
|
|
tid: TaskID,
|
|
|
|
|
|
minutes: u64,
|
|
|
|
|
|
) -> Result<()> {
|
2025-04-05 11:34:51 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"Adding task: uid={}, id={}, interval={}min",
|
|
|
|
|
|
uid,
|
|
|
|
|
|
tid,
|
|
|
|
|
|
minutes
|
|
|
|
|
|
);
|
2025-03-12 22:36:25 +08:00
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
// Create a task with reasonable retries and backoff
|
2022-11-12 11:37:23 +08:00
|
|
|
|
let task = TaskBuilder::default()
|
|
|
|
|
|
.set_task_id(tid)
|
|
|
|
|
|
.set_maximum_parallel_runnable_num(1)
|
|
|
|
|
|
.set_frequency_repeated_by_minutes(minutes)
|
2025-03-12 22:36:25 +08:00
|
|
|
|
.spawn_async_routine(move || {
|
|
|
|
|
|
let uid = uid.clone();
|
2025-08-26 01:49:51 +08:00
|
|
|
|
Box::pin(async move {
|
2025-03-12 22:36:25 +08:00
|
|
|
|
Self::async_task(uid).await;
|
2025-08-26 01:49:51 +08:00
|
|
|
|
}) as Pin<Box<dyn std::future::Future<Output = ()> + Send>>
|
2025-03-12 22:36:25 +08:00
|
|
|
|
})
|
2022-11-12 11:37:23 +08:00
|
|
|
|
.context("failed to create timer task")?;
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2022-11-14 01:45:58 +08:00
|
|
|
|
delay_timer
|
2022-11-12 11:37:23 +08:00
|
|
|
|
.add_task(task)
|
|
|
|
|
|
.context("failed to add timer task")?;
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2022-11-12 11:37:23 +08:00
|
|
|
|
Ok(())
|
|
|
|
|
|
}
|
2022-04-20 20:37:16 +08:00
|
|
|
|
|
2025-04-25 17:17:34 +08:00
|
|
|
|
/// Get next update time for a profile
|
2025-08-26 01:49:51 +08:00
|
|
|
|
pub async fn get_next_update_time(&self, uid: &str) -> Option<i64> {
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(info, Type::Timer, "获取下次更新时间,uid={}", uid);
|
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
// First extract timer task data without holding the lock across await
|
|
|
|
|
|
let task_interval = {
|
|
|
|
|
|
let timer_map = self.timer_map.read();
|
|
|
|
|
|
match timer_map.get(uid) {
|
|
|
|
|
|
Some(t) => t.interval_minutes,
|
|
|
|
|
|
None => {
|
|
|
|
|
|
logging!(warn, Type::Timer, "找不到对应的定时任务,uid={}", uid);
|
|
|
|
|
|
return None;
|
|
|
|
|
|
}
|
2025-04-25 17:17:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
// Get the profile updated timestamp - now safe to await
|
edition 2024 (#4702)
* feat: update Cargo.toml for 2024 edition and optimize release profiles
* feat: refactor environment variable settings for Linux and improve code organization
* Refactor conditional statements to use `&&` for improved readability
- Updated multiple files to combine nested `if let` statements using `&&` for better clarity and conciseness.
- This change enhances the readability of the code by reducing indentation levels and making the conditions more straightforward.
- Affected files include: media_unlock_checker.rs, profile.rs, clash.rs, profiles.rs, async_proxy_query.rs, core.rs, handle.rs, hotkey.rs, service.rs, timer.rs, tray/mod.rs, merge.rs, seq.rs, config.rs, proxy.rs, window.rs, general.rs, dirs.rs, i18n.rs, init.rs, network.rs, and window.rs in the resolve module.
* refactor: streamline conditional checks using `&&` for improved readability
* fix: update release profile settings for panic behavior and optimization
* fix: adjust optimization level in Cargo.toml and reorder imports in lightweight.rs
2025-09-10 09:49:06 +08:00
|
|
|
|
let config_profiles = Config::profiles().await;
|
|
|
|
|
|
let profiles = config_profiles.data_ref().clone();
|
2025-04-25 17:17:34 +08:00
|
|
|
|
let items = match profiles.get_items() {
|
|
|
|
|
|
Some(i) => i,
|
|
|
|
|
|
None => {
|
|
|
|
|
|
logging!(warn, Type::Timer, "获取配置列表失败");
|
|
|
|
|
|
return None;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2025-05-16 00:09:34 +08:00
|
|
|
|
let profile = match items.iter().find(|item| item.uid.as_deref() == Some(uid)) {
|
2025-04-25 17:17:34 +08:00
|
|
|
|
Some(p) => p,
|
|
|
|
|
|
None => {
|
|
|
|
|
|
logging!(warn, Type::Timer, "找不到对应的配置,uid={}", uid);
|
|
|
|
|
|
return None;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let updated = profile.updated.unwrap_or(0) as i64;
|
|
|
|
|
|
|
|
|
|
|
|
// Calculate next update time
|
2025-08-26 01:49:51 +08:00
|
|
|
|
if updated > 0 && task_interval > 0 {
|
|
|
|
|
|
let next_time = updated + (task_interval as i64 * 60);
|
2025-04-25 17:17:34 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"计算得到下次更新时间: {}, uid={}",
|
|
|
|
|
|
next_time,
|
|
|
|
|
|
uid
|
|
|
|
|
|
);
|
|
|
|
|
|
Some(next_time)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
logging!(
|
|
|
|
|
|
warn,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"更新时间或间隔无效,updated={}, interval={}",
|
|
|
|
|
|
updated,
|
2025-08-26 01:49:51 +08:00
|
|
|
|
task_interval
|
2025-04-25 17:17:34 +08:00
|
|
|
|
);
|
|
|
|
|
|
None
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Emit update events for frontend notification
|
|
|
|
|
|
fn emit_update_event(_uid: &str, _is_start: bool) {
|
|
|
|
|
|
{
|
2025-05-18 20:58:59 +08:00
|
|
|
|
if _is_start {
|
2025-10-14 09:24:39 +08:00
|
|
|
|
super::handle::Handle::notify_profile_update_started(_uid.into());
|
2025-04-25 17:17:34 +08:00
|
|
|
|
} else {
|
2025-10-14 09:24:39 +08:00
|
|
|
|
super::handle::Handle::notify_profile_update_completed(_uid.into());
|
2025-04-25 17:17:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-15 18:58:12 +08:00
|
|
|
|
/// Async task with better error handling and logging
|
2022-11-14 01:26:33 +08:00
|
|
|
|
async fn async_task(uid: String) {
|
2025-03-15 18:58:12 +08:00
|
|
|
|
let task_start = std::time::Instant::now();
|
2025-04-05 11:34:51 +08:00
|
|
|
|
logging!(info, Type::Timer, "Running timer task for profile: {}", uid);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
|
2025-05-03 09:38:25 +08:00
|
|
|
|
match tokio::time::timeout(std::time::Duration::from_secs(40), async {
|
|
|
|
|
|
Self::emit_update_event(&uid, true);
|
2025-04-25 17:17:34 +08:00
|
|
|
|
|
2025-08-26 01:49:51 +08:00
|
|
|
|
let is_current = Config::profiles().await.latest_ref().current.as_ref() == Some(&uid);
|
2025-05-03 09:38:25 +08:00
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"配置 {} 是否为当前激活配置: {}",
|
|
|
|
|
|
uid,
|
|
|
|
|
|
is_current
|
|
|
|
|
|
);
|
2025-03-15 18:58:12 +08:00
|
|
|
|
|
2025-05-03 09:38:25 +08:00
|
|
|
|
feat::update_profile(uid.clone(), None, Some(is_current)).await
|
|
|
|
|
|
})
|
|
|
|
|
|
.await
|
|
|
|
|
|
{
|
|
|
|
|
|
Ok(result) => match result {
|
|
|
|
|
|
Ok(_) => {
|
|
|
|
|
|
let duration = task_start.elapsed().as_millis();
|
|
|
|
|
|
logging!(
|
|
|
|
|
|
info,
|
|
|
|
|
|
Type::Timer,
|
|
|
|
|
|
"Timer task completed successfully for uid: {} (took {}ms)",
|
|
|
|
|
|
uid,
|
|
|
|
|
|
duration
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
Err(e) => {
|
|
|
|
|
|
logging_error!(Type::Timer, "Failed to update profile uid {}: {}", uid, e);
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
Err(_) => {
|
Refactor logging macros to remove print control parameter
- Updated logging macros to eliminate the boolean parameter for print control, simplifying the logging calls throughout the codebase.
- Adjusted all logging calls in various modules (lib.rs, lightweight.rs, help.rs, init.rs, logging.rs, resolve/mod.rs, resolve/scheme.rs, resolve/ui.rs, resolve/window.rs, server.rs, singleton.rs, window_manager.rs) to reflect the new macro structure.
- Ensured consistent logging behavior across the application by standardizing the logging format.
2025-10-10 13:05:01 +08:00
|
|
|
|
logging_error!(Type::Timer, "Timer task timed out for uid: {}", uid);
|
2025-03-12 22:36:25 +08:00
|
|
|
|
}
|
2025-02-23 10:53:09 +08:00
|
|
|
|
}
|
2025-04-25 17:17:34 +08:00
|
|
|
|
|
|
|
|
|
|
// Emit completed event
|
|
|
|
|
|
Self::emit_update_event(&uid, false);
|
2022-11-12 11:37:23 +08:00
|
|
|
|
}
|
2022-04-20 20:37:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-04-21 14:26:41 +08:00
|
|
|
|
#[derive(Debug)]
|
2022-04-20 20:37:16 +08:00
|
|
|
|
enum DiffFlag {
|
2022-11-12 11:37:23 +08:00
|
|
|
|
Del(TaskID),
|
|
|
|
|
|
Add(TaskID, u64),
|
|
|
|
|
|
Mod(TaskID, u64),
|
2022-04-19 01:41:20 +08:00
|
|
|
|
}
|