//! Bringing a repository up, which every command does. use std::sync::atomic::AtomicBool; use std::sync::Arc; use crate::config::Config; use crate::db::{self, Db}; use crate::error::Result; use crate::pipeline; /// Directories exist, the schema is current, and the repo row is present. /// Order matters: migrate before open, `ensure_repo` before anything reads /// `repo_id`. pub fn open(cfg: &Config) -> Result { open_and_ensure(cfg).map(|(db, _)| db) } /// `open`, plus the id of the row named by `cfg.repo_name`. fn open_and_ensure(cfg: &Config) -> Result<(Db, i64)> { cfg.ensure_dirs()?; db::migrate(&cfg.db_path())?; let db = Db::open_migrated(&cfg.db_path())?; let rid = db::ensure_repo( &db.writer, &cfg.repo_name, &cfg.repo_arch_dir().display().to_string(), &cfg.arch, &cfg.signing_key_id, )?; Ok((db, rid)) } /// A first run: persist the config, bring the repository up, and hand back the /// repo id so the operator can paste it into `pacman.conf`. pub fn init(cfg: &Config) -> Result { cfg.save()?; open_and_ensure(cfg).map(|(_, rid)| rid) } /// Everything `serve` does before control passes to the UI: bring the /// repository up, finish anything a previous run left half-published, and start /// the two background loops. This module sits below the UI, so it cannot start /// the HTTP server. pub fn serve(cfg: &Config) -> Result { let db = open(cfg)?; let _ = pipeline::publish::reconcile(cfg, &db); let stop = Arc::new(AtomicBool::new(false)); crate::watch::scheduler::spawn_scheduler(Arc::new(cfg.clone()), db.clone(), stop.clone()); let _pool = pipeline::dispatch::Pool::spawn(Arc::new(cfg.clone()), db.clone()); Ok(db) }