diff options
Diffstat (limited to 'import/src/lib.rs')
| -rw-r--r-- | import/src/lib.rs | 65 |
1 files changed, 28 insertions, 37 deletions
diff --git a/import/src/lib.rs b/import/src/lib.rs index 4ad7f25..7980451 100644 --- a/import/src/lib.rs +++ b/import/src/lib.rs @@ -39,15 +39,14 @@ use std::{ }; use tokio::{runtime::Handle, sync::Semaphore, task::spawn_blocking}; -#[rustfmt::skip] -#[derive(Debug, Deserialize, Serialize, Default)] +#[derive(Debug, Deserialize, Serialize, Default, Clone)] pub struct Config { media_path: PathBuf, api: ApiSecrets, - num_threads: usize + num_threads: usize, } -#[derive(Serialize, Deserialize, Debug, Default)] +#[derive(Serialize, Deserialize, Debug, Default, Clone)] pub struct ApiSecrets { pub acoustid: Option<String>, pub tmdb: Option<String>, @@ -58,15 +57,6 @@ pub struct ApiSecrets { pub trakt: Option<String>, } -pub static CONF_PRELOAD: Mutex<Option<Config>> = Mutex::new(None); -static CONF: LazyLock<Config> = LazyLock::new(|| { - CONF_PRELOAD - .lock() - .unwrap() - .take() - .expect("import config not preloaded. logic error") -}); - pub const USER_AGENT: &str = concat!( "jellything/", env!("CARGO_PKG_VERSION"), @@ -81,6 +71,7 @@ pub fn is_importing() -> bool { #[derive(Clone)] pub struct ImportConfig { + pub config: Config, pub cache: Arc<Cache>, pub db: Arc<dyn Database>, } @@ -141,19 +132,19 @@ impl ImportConfig { } } -pub async fn import_wrap(db: ImportConfig, incremental: bool) -> Result<()> { +pub async fn import_wrap(ic: ImportConfig, incremental: bool) -> Result<()> { let _sem = IMPORT_SEM.try_acquire().context("already importing")?; let rt = Handle::current(); let tp = ThreadPoolBuilder::new() .thread_name(|n| format!("import #{n}")) - .num_threads(CONF.num_threads) + .num_threads(ic.config.num_threads) .build()?; let jh = spawn_blocking(move || { tp.install(move || { reporting::start_import(); - reporting::catch(import(db, &rt, incremental)); + reporting::catch(import(ic, &rt, incremental)); reporting::end_import(); }) }); @@ -163,12 +154,12 @@ pub async fn import_wrap(db: ImportConfig, incremental: bool) -> Result<()> { Ok(()) } -fn import(dba: ImportConfig, rt: &Handle, incremental: bool) -> Result<()> { - let plugins = init_plugins(&CONF.api); +fn import(ic: ImportConfig, rt: &Handle, incremental: bool) -> Result<()> { + let plugins = init_plugins(&ic.config.api); let files = Mutex::new(Vec::new()); import_traverse( - &CONF.media_path, - &dba, + &ic.config.media_path, + &ic, incremental, None, InheritedFlags::default(), @@ -180,7 +171,7 @@ fn import(dba: ImportConfig, rt: &Handle, incremental: bool) -> Result<()> { files.into_par_iter().for_each(|(path, parent, iflags)| { reporting::set_task(format!("unknown: {path:?}")); - import_file(&dba, &rt, &nodes, &plugins, &path, parent, iflags); + import_file(&ic, &rt, &nodes, &plugins, &path, parent, iflags); IMPORT_PROGRESS .blocking_write() .as_mut() @@ -201,7 +192,7 @@ fn import(dba: ImportConfig, rt: &Handle, incremental: bool) -> Result<()> { swap(nodes.get_mut().unwrap(), &mut cur_nodes); cur_nodes.into_par_iter().for_each(|node| { reporting::set_task(format!("unknown: {node}")); - process_node(&dba, &rt, &plugins, &nodes, node); + process_node(&ic, &rt, &plugins, &nodes, node); IMPORT_PROGRESS .blocking_write() .as_mut() @@ -223,7 +214,7 @@ pub struct InheritedFlags { fn import_traverse( path: &Path, - dba: &ImportConfig, + ic: &ImportConfig, incremental: bool, parent: Option<RowNum>, mut iflags: InheritedFlags, @@ -232,7 +223,7 @@ fn import_traverse( if path.is_dir() { reporting::set_task(format!("indexing {path:?}")); - let slug = get_node_slug(path).unwrap(); + let slug = get_node_slug(ic, path).unwrap(); // Some flags need to applied immediatly because they are inherited if let Ok(content) = read_to_string(path.join("flags")) { @@ -246,7 +237,7 @@ fn import_traverse( } } - let row = dba.update_node_slug(&slug, |mut node| { + let row = ic.update_node_slug(&slug, |mut node| { if let Some(parent) = parent { node = node.as_object().extend(NO_PARENT, [parent]); } @@ -262,7 +253,7 @@ fn import_traverse( path.read_dir()?.par_bridge().try_for_each(|e| { let path = e?.path(); reporting::catch( - import_traverse(&path, dba, incremental, Some(row), iflags, out) + import_traverse(&path, ic, incremental, Some(row), iflags, out) .context(anyhow!("index {:?}", path.file_name().unwrap())), ); anyhow::Ok(()) @@ -274,7 +265,7 @@ fn import_traverse( && let Some(parent) = parent { if incremental { - if compare_mtime(dba, path)? { + if compare_mtime(ic, path)? { return Ok(()); } } @@ -290,7 +281,7 @@ fn import_traverse( } fn import_file( - dba: &ImportConfig, + ic: &ImportConfig, rt: &Handle, pending_nodes: &Mutex<HashSet<RowNum>>, plugins: &[Box<dyn ImportPlugin>], @@ -300,7 +291,7 @@ fn import_file( ) { let mut all_ok = true; let ct = PluginContext { - dba, + ic, rt, iflags, pending_nodes, @@ -329,9 +320,9 @@ fn import_file( } if filename.ends_with("mkv") || filename.ends_with("mka") || filename.ends_with("mks") { - let slug = get_node_slug(path).unwrap(); + let slug = get_node_slug(ic, path).unwrap(); - let Some(row) = reporting::catch(dba.update_node_slug(&slug, |mut node| { + let Some(row) = reporting::catch(ic.update_node_slug(&slug, |mut node| { node = node.as_object().extend(NO_PARENT, [parent]); if iflags.hidden { node = node.as_object().insert(NO_VISIBILITY, VISI_HIDDEN); @@ -370,7 +361,7 @@ fn import_file( reporting::set_task(format!("demuxer meta: {path:?}")); let Some(seg) = reporting::catch( - read_media_metadata(&ct.dba.cache, path).context(anyhow!("media {path:?}")), + read_media_metadata(&ct.ic.cache, path).context(anyhow!("media {path:?}")), ) else { return; }; @@ -400,7 +391,7 @@ fn import_file( } if all_ok { - reporting::catch(update_mtime(dba, path).context("updating mtime")); + reporting::catch(update_mtime(ic, path).context("updating mtime")); } } @@ -427,7 +418,7 @@ fn process_node( reporting::catch( p.process( &PluginContext { - dba, + ic: dba, rt, iflags: InheritedFlags::default(), pending_nodes, @@ -491,13 +482,13 @@ fn update_mtime(dba: &ImportConfig, path: &Path) -> Result<()> { Ok(()) } -fn get_node_slug(path: &Path) -> Option<String> { - if path == CONF.media_path { +fn get_node_slug(ic: &ImportConfig, path: &Path) -> Option<String> { + if path == ic.config.media_path { return Some("library".to_string()); } let filename = path.file_name()?.to_string_lossy(); let filestem = filename.split_once(".").unwrap_or((&filename, "")).0; - if path.parent()? == &CONF.media_path { + if path.parent()? == &ic.config.media_path { Some(format!("{filestem}")) } else { let parent_filename = path.parent()?.file_name()?.to_string_lossy(); |