diff --git a/Cargo.lock b/Cargo.lock index a625162..ec9914e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -183,9 +183,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" [[package]] name = "bytes" -version = "1.10.0" +version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f61dac84819c6588b558454b194026eb1f09c293b9036ae9b159e74e73ab6cf9" +checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" [[package]] name = "cc" @@ -222,6 +222,16 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" +[[package]] +name = "comrade" +version = "0.1.0" +source = "git+https://git.hydrar.de/jmarya/comrade#5cb4facc4872a961a3ad3b11d4b48d01016b6600" +dependencies = [ + "env_logger", + "log", + "tokio", +] + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -344,9 +354,9 @@ checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" [[package]] name = "either" -version = "1.14.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7914353092ddf589ad78f25c5c1c21b7f80b0ff8621e7c814c3485b5306da9d" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" dependencies = [ "serde", ] @@ -659,6 +669,7 @@ name = "hoard" version = "0.1.0" dependencies = [ "chrono", + "comrade", "env_logger", "log", "reqwest", @@ -702,9 +713,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.10.0" +version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2d708df4e7140240a16cd6ab0ab65c972d7433ab77819ea693fde9c43811e2a" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" [[package]] name = "httpdate" @@ -941,9 +952,9 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" [[package]] name = "itoa" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "js-sys" @@ -989,9 +1000,9 @@ dependencies = [ [[package]] name = "linux-raw-sys" -version = "0.4.15" +version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" +checksum = "6db9c683daf087dc577b7506e9695b3d556a9f3849903fa28186283afd6809e9" [[package]] name = "litemap" @@ -1259,9 +1270,9 @@ dependencies = [ [[package]] name = "pkg-config" -version = "0.3.31" +version = "0.3.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" [[package]] name = "ppv-lite86" @@ -1274,18 +1285,18 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.38" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +checksum = "c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801" dependencies = [ "proc-macro2", ] @@ -1322,9 +1333,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.9" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "82b568323e98e49e2a0899dcee453dd679fae22d69adf9b11dd508d1549b7e2f" +checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1" dependencies = [ "bitflags 2.9.0", ] @@ -1426,9 +1437,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustix" -version = "0.38.44" +version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +checksum = "17f8dcd64f141950290e45c99f7710ede1b600297c91818bb30b3667c0f45dc0" dependencies = [ "bitflags 2.9.0", "errno", @@ -1448,15 +1459,15 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" +checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" [[package]] name = "ryu" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" [[package]] name = "schannel" @@ -1518,9 +1529,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.139" +version = "1.0.140" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44f86c3acccc9c65b153fe1b85a3be07fe5515274ec9f0653b4a0875731c72a6" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" dependencies = [ "itoa", "memchr", @@ -1864,9 +1875,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.98" +version = "2.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +checksum = "e02e925281e18ffd9d640e234264753c43edc62d64b2d4cf898f1bc5e75f3fc2" dependencies = [ "proc-macro2", "quote", @@ -1913,9 +1924,9 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.17.1" +version = "3.18.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22e5a0acb1f3f55f65cc4a866c361b2fb2a0ff6366785ae6fbb5f85df07ba230" +checksum = "2c317e0a526ee6120d8dabad239c8dadca62b24b6f168914bbbc8e2fb1f0e567" dependencies = [ "cfg-if", "fastrand", @@ -1927,18 +1938,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.11" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "2.0.11" +version = "2.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" dependencies = [ "proc-macro2", "quote", @@ -1957,9 +1968,9 @@ dependencies = [ [[package]] name = "tinyvec" -version = "1.8.1" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "022db8904dfa342efe721985167e9fcd16c29b226db4397ed752a761cfce81e8" +checksum = "09b3661f17e86524eccd4371ab0429194e0d7c008abb45f7a7495b1719463c71" dependencies = [ "tinyvec_macros", ] @@ -2125,9 +2136,9 @@ checksum = "5c1cb5db39152898a79168971543b1cb5020dff7fe43c8dc468b0885f5e29df5" [[package]] name = "unicode-ident" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "00e2473a93778eb0bad35909dff6a10d28e63f792f16ed15e404fca9d5eeedbe" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" [[package]] name = "unicode-normalization" diff --git a/Cargo.toml b/Cargo.toml index 311717b..c9bc2df 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,3 +15,4 @@ toml = "0.8.10" reqwest = { version = "0.11.26", features = ["blocking", "json"] } sqlx = { version = "0.8", features = ["postgres", "sqlite", "runtime-tokio-native-tls", "derive", "uuid", "chrono", "json"] } tokio = { version = "1.42.0", features = ["full"] } +comrade = { git = "https://git.hydrar.de/jmarya/comrade" } diff --git a/src/lib.rs b/src/lib.rs index f48ac17..c495e08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,12 +13,26 @@ pub fn ensure_dir_exists(dir_path: &PathBuf) { } } -/// Generic module implementation -/// -/// Each module gets it's own thread to work for itself. -pub trait Module: Send { - /// friendly name for module - fn name(&self) -> String; - /// module main loop - fn run(&self); +pub enum Module { + Soundcloud(soundcloud::SoundCloudModule), + YouTube(youtube::YouTubeModule), + YtDlp(yt_dlp::YtDlpModule), +} + +impl Module { + pub fn name(&self) -> String { + match self { + Module::Soundcloud(sound_cloud_module) => sound_cloud_module.name(), + Module::YouTube(you_tube_module) => you_tube_module.name(), + Module::YtDlp(yt_dlp_module) => yt_dlp_module.name(), + } + } + + pub fn run(&self) { + match self { + Module::Soundcloud(sound_cloud_module) => sound_cloud_module.run(), + Module::YouTube(you_tube_module) => you_tube_module.run(), + Module::YtDlp(yt_dlp_module) => yt_dlp_module.run(), + } + } } diff --git a/src/main.rs b/src/main.rs index 5b8bf44..bace8d1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -#![feature(async_closure)] - use hoard::config::GlobalConfig; use hoard::{ensure_dir_exists, Module}; @@ -30,10 +28,10 @@ async fn main() { let db = hoard::db::DatabaseBackend::new(&config.hoard.database).await; - let mut modules: Vec> = vec![]; + let mut modules: Vec = vec![]; if let Some(yt_config) = config.youtube { - modules.push(Box::new(hoard::youtube::YouTubeModule::new( + modules.push(Module::YouTube(hoard::youtube::YouTubeModule::new( yt_config, db.take_db(), config.hoard.data_dir.join("youtube"), @@ -41,11 +39,13 @@ async fn main() { } if let Some(sc_config) = config.soundcloud { - modules.push(Box::new(hoard::soundcloud::SoundCloudModule::new( - sc_config, - db.take_db(), - config.hoard.data_dir.join("soundcloud"), - ))); + modules.push(Module::Soundcloud( + hoard::soundcloud::SoundCloudModule::new( + sc_config, + db.take_db(), + config.hoard.data_dir.join("soundcloud"), + ), + )); } for yt_dlp_mod in config.yt_dlp.unwrap_or_default() { @@ -53,24 +53,21 @@ async fn main() { .name .clone() .unwrap_or_else(|| "yt_dlp".to_string()); - modules.push(Box::new(hoard::yt_dlp::YtDlpModule::new( + modules.push(Module::YtDlp(hoard::yt_dlp::YtDlpModule::new( yt_dlp_mod, db.take_db(), config.hoard.data_dir.join(mod_name), ))); } - let threads: Vec<_> = modules - .into_iter() - .map(|x| { - std::thread::spawn(move || { - x.run(); - }) - }) - .collect(); + let mut sm = comrade::service::ServiceManager::new(); - for t in threads { - // todo : fix dying threads - t.join().unwrap(); + for module in modules { + sm.register(&module.name(), move |_| { + module.run(); + }); } + + let t = sm.spawn(); + t.join().unwrap(); } diff --git a/src/soundcloud/mod.rs b/src/soundcloud/mod.rs index b337e03..2854e40 100644 --- a/src/soundcloud/mod.rs +++ b/src/soundcloud/mod.rs @@ -2,10 +2,7 @@ use std::{collections::HashMap, path::PathBuf}; use serde::{Deserialize, Serialize}; -use crate::{ - yt_dlp::{config::YtDlpConfig, YtDlpModule}, - Module, -}; +use crate::yt_dlp::{config::YtDlpConfig, YtDlpModule}; /// Configuration for the `SoundCloud` Module #[derive(Debug, Clone, Serialize, Deserialize)] @@ -84,14 +81,12 @@ impl SoundCloudModule { ), } } -} -impl Module for SoundCloudModule { - fn name(&self) -> String { + pub fn name(&self) -> String { "SoundCloud".to_string() } - fn run(&self) { + pub fn run(&self) { self.yt_dlp.run(); } } diff --git a/src/youtube/mod.rs b/src/youtube/mod.rs index faca786..7a8d25f 100644 --- a/src/youtube/mod.rs +++ b/src/youtube/mod.rs @@ -2,7 +2,7 @@ use std::{collections::HashMap, path::PathBuf}; use serde::{Deserialize, Serialize}; -use crate::{yt_dlp::config::YtDlpConfig, yt_dlp::YtDlpModule, Module}; +use crate::{yt_dlp::config::YtDlpConfig, yt_dlp::YtDlpModule}; /// Configuration for the `YouTube` Module #[derive(Debug, Clone, Serialize, Deserialize)] @@ -85,14 +85,12 @@ impl YouTubeModule { ), } } -} -impl Module for YouTubeModule { - fn name(&self) -> String { + pub fn name(&self) -> String { "YouTube".to_string() } - fn run(&self) { + pub fn run(&self) { self.yt_dlp.run(); } } diff --git a/src/yt_dlp/mod.rs b/src/yt_dlp/mod.rs index 5daf0c6..532d574 100644 --- a/src/yt_dlp/mod.rs +++ b/src/yt_dlp/mod.rs @@ -7,7 +7,7 @@ use std::{ pub mod config; use config::YtDlpConfig; -use crate::{ensure_dir_exists, Module}; +use crate::ensure_dir_exists; #[derive(Clone)] pub struct YtDlpModule { @@ -54,17 +54,15 @@ impl YtDlpModule { } } } -} -impl Module for YtDlpModule { - fn name(&self) -> String { + pub fn name(&self) -> String { self.config .name .clone() .unwrap_or_else(|| "yt-dlp".to_string()) } - fn run(&self) { + pub fn run(&self) { loop { log::info!("Running {} Module", self.name()); log::info!("Checking {} items", self.config.items.len());