redux of the worker queue

This commit is contained in:
Connor Johnstone
2026-03-23 18:37:46 -04:00
parent 59a26c18f3
commit 36345b12ee
12 changed files with 813 additions and 516 deletions

View File

@@ -1,59 +1,22 @@
//! Background task that periodically refreshes YouTube cookies via headless Firefox.
//! YouTube cookie refresh via headless Firefox.
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use std::process::Stdio;
use std::sync::Arc;
use std::time::Duration;
use tokio::process::Command;
use tokio::sync::RwLock;
use crate::config::AppConfig;
/// Run a headless cookie refresh. Returns success message or error.
pub async fn run_refresh() -> Result<String, String> {
let profile_dir = shanty_config::data_dir().join("firefox-profile");
let cookies_path = shanty_config::data_dir().join("cookies.txt");
/// Spawn the cookie refresh background loop.
///
/// This task runs forever, sleeping for `cookie_refresh_hours` between refreshes.
/// It reads the current config on each iteration so changes take effect without restart.
pub fn spawn(config: Arc<RwLock<AppConfig>>) {
tokio::spawn(async move {
loop {
let (enabled, hours) = {
let cfg = config.read().await;
(
cfg.download.cookie_refresh_enabled,
cfg.download.cookie_refresh_hours.max(1),
)
};
if !profile_dir.exists() {
return Err(format!(
"no Firefox profile at {}",
profile_dir.display()
));
}
// Sleep for the configured interval
tokio::time::sleep(Duration::from_secs(u64::from(hours) * 3600)).await;
if !enabled {
continue;
}
let profile_dir = shanty_config::data_dir().join("firefox-profile");
let cookies_path = shanty_config::data_dir().join("cookies.txt");
if !profile_dir.exists() {
tracing::warn!(
"cookie refresh skipped: no Firefox profile at {}",
profile_dir.display()
);
continue;
}
tracing::info!("starting cookie refresh");
match run_refresh(&profile_dir, &cookies_path).await {
Ok(msg) => tracing::info!("cookie refresh complete: {msg}"),
Err(e) => tracing::error!("cookie refresh failed: {e}"),
}
}
});
}
async fn run_refresh(profile_dir: &Path, cookies_path: &Path) -> Result<String, String> {
let script = find_script()?;
let output = Command::new("python3")