mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-12-01 10:40:14 +08:00
refactor(profile): separate subscription refresh and scheduling policy
Separate download and scheduling policy from the subscription use case while retaining the existing write path.
This commit is contained in:
@@ -8,6 +8,7 @@ use crate::{
|
||||
use anyhow::{Context as _, Result};
|
||||
use clash_verge_draft::Draft;
|
||||
use clash_verge_logging::{Type, logging, logging_error};
|
||||
use clash_verge_profile::SubscriptionSchedule as TaskSchedule;
|
||||
use futures::{FutureExt as _, StreamExt as _};
|
||||
use std::{collections::HashMap, panic::AssertUnwindSafe, time::Duration};
|
||||
use tokio::{
|
||||
@@ -24,28 +25,6 @@ enum TimerCommand {
|
||||
TaskFinished(String),
|
||||
}
|
||||
|
||||
/// Next update is due at `updated + interval`, but the queue only takes a delay from now.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct TaskSchedule {
|
||||
interval_minutes: u64,
|
||||
first_delay: Duration,
|
||||
}
|
||||
|
||||
impl TaskSchedule {
|
||||
/// No recorded `updated` -> full interval. Deadline already past -> `ZERO`, fires at once.
|
||||
fn new(interval_minutes: u64, updated: Option<usize>, now: i64) -> Self {
|
||||
let full = Timer::interval_duration(interval_minutes);
|
||||
let elapsed = updated
|
||||
.filter(|updated| *updated > 0)
|
||||
.map_or(0, |updated| now.saturating_sub(updated as i64).max(0) as u64);
|
||||
|
||||
Self {
|
||||
interval_minutes,
|
||||
first_delay: full.saturating_sub(Duration::from_secs(elapsed)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct TaskState {
|
||||
key: Option<Key>,
|
||||
interval_minutes: u64,
|
||||
@@ -122,16 +101,9 @@ impl Timer {
|
||||
let mut new_map = HashMap::new();
|
||||
|
||||
for item in items {
|
||||
if let Some(option) = item.option.as_ref()
|
||||
&& let (Some(interval), Some(uid)) = (option.update_interval, &item.uid)
|
||||
&& option.allow_auto_update.unwrap_or(true)
|
||||
&& interval > 0
|
||||
if let Some(uid) = &item.uid
|
||||
&& let Some(schedule) = TaskSchedule::for_profile(item, initializing, now)
|
||||
{
|
||||
let mut schedule = TaskSchedule::new(interval, item.updated, now);
|
||||
// Startup historically treats an explicit zero timestamp as overdue.
|
||||
if initializing && item.updated == Some(0) {
|
||||
schedule.first_delay = Duration::ZERO;
|
||||
}
|
||||
new_map.insert(uid.clone(), schedule);
|
||||
}
|
||||
}
|
||||
@@ -317,7 +289,7 @@ impl Timer {
|
||||
|
||||
// Full interval, not from `updated`: a failed run leaves `updated` in the past, so
|
||||
// counting from it would retry at zero delay and spin.
|
||||
let key = Self::schedule_task(queue, &uid, Self::interval_duration(state.interval_minutes));
|
||||
let key = Self::schedule_task(queue, &uid, TaskSchedule::interval_duration(state.interval_minutes));
|
||||
state.key = Some(key);
|
||||
}
|
||||
|
||||
@@ -330,10 +302,6 @@ impl Timer {
|
||||
});
|
||||
}
|
||||
|
||||
const fn interval_duration(interval_minutes: u64) -> Duration {
|
||||
Duration::from_secs(interval_minutes.saturating_mul(60))
|
||||
}
|
||||
|
||||
fn schedule_task(queue: &mut DelayQueue<String>, uid: &str, delay: Duration) -> Key {
|
||||
queue.insert(String::from(uid), delay)
|
||||
}
|
||||
@@ -348,10 +316,8 @@ impl Timer {
|
||||
|
||||
fn next_update_time(tasks: &HashMap<String, TaskState>, profiles: &IProfiles, uid: &str) -> Option<i64> {
|
||||
let task = tasks.get(uid).filter(|task| !task.retired)?;
|
||||
let updated = profiles.get_item(uid).ok()?.updated.filter(|updated| *updated > 0)?;
|
||||
let updated = i64::try_from(updated).ok()?;
|
||||
let interval = i64::try_from(task.interval_minutes).ok()?.checked_mul(60)?;
|
||||
updated.checked_add(interval)
|
||||
let updated = profiles.get_item(uid).ok()?.updated;
|
||||
TaskSchedule::reported_deadline(task.interval_minutes, updated)
|
||||
}
|
||||
|
||||
fn emit_update_event(uid: &str, is_start: bool) {
|
||||
|
||||
Reference in New Issue
Block a user