service management
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
JMARyA 2025-03-07 15:26:18 +01:00
parent 4f0f1b6d5c
commit c4c54f78d6
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 99 additions and 85 deletions

View file

@ -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(),
}
}
}

View file

@ -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<Box<dyn Module>> = vec![];
let mut modules: Vec<Module> = 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();
}

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -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());