summaryrefslogtreecommitdiffstats
path: root/src/usecase/bootstrap.rs
diff options
context:
space:
mode:
authordev <dev@localhost>2026-11-20 18:34:26 +0000
committerdev <dev@localhost>2026-11-20 18:34:26 +0000
commit895f3547043def08f0822f936ae34458315f6982 (patch)
tree9e7b08db7225786f1d1ec5b312314801e3d09c90 /src/usecase/bootstrap.rs
parentbbae6c8e83cf145f9af472f69591bae804d03048 (diff)
downloadaur-build-895f3547043def08f0822f936ae34458315f6982.tar.gz
aur-build-895f3547043def08f0822f936ae34458315f6982.zip
usecase: one implementation of each workflow, run by both front ends
`main` opened the repository inline before its command match. `serve` opened it again, four calls in an order that matters. The web routes wrote subscriptions, toggles, notices and build retries straight through `db::`, so the CLI and the UI were two independent guesses at the same product, and the guesses had already drifted: `sub_add` fell back to repo id 1 where the CLI reported the repository as uninitialised, and `build_retry` remembered to write the audit row while nothing else did. `src/usecase/` is the workflow layer now — bootstrap, detect, reviews, subscriptions, builds, maintenance. A function earns a place here by having more than one step in an order that matters: `approve` records the decision *and* queues the build it authorises, because an approval that silently fails to queue is the exact failure the review queue exists to prevent, and both front ends had to remember to pair them. Nothing in the layer formats output or knows a status code exists. Where the two front ends genuinely disagree, the result carries the disagreement instead of resolving it: `Approved::queued` is a `Result` because a failed enqueue is fatal in the CLI — an approved candidate with no build is a stall nobody notices — and a soft failure in the UI, which has already committed the decision and can retry from the package page. `src/http/routes/mutations.rs` now contains no `db::` call at all, and `main.rs` is 314 lines with no multi-step sequence and nothing exported. Rank map: usecase 9, http 10, composition roots 11. The layer test enforces that usecase cannot name http, which is why `serve` starts the scheduler and the build pool but leaves the HTTP server to `main`. One user-visible message moves: `aur-build reject <missing>` now says `not found: candidate N` (the review layer's wording) instead of `no candidate #N` (main's). Both exit 1; the check moved with the workflow. 280 passed / 0 failed, clippy -D warnings clean, architecture 8/8.
Diffstat (limited to 'src/usecase/bootstrap.rs')
-rw-r--r--src/usecase/bootstrap.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/usecase/bootstrap.rs b/src/usecase/bootstrap.rs
new file mode 100644
index 0000000..6793076
--- /dev/null
+++ b/src/usecase/bootstrap.rs
@@ -0,0 +1,58 @@
+//! Bringing a repository up, which every command does and every front end
+//! used to write out for itself.
+
+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.
+///
+/// Four calls in an order that matters — migrate before open, `ensure_repo`
+/// before anything reads `repo_id` — which is precisely the kind of sequence
+/// that quietly rots when it is copied.
+pub fn open(cfg: &Config) -> Result<Db> {
+ open_and_ensure(cfg).map(|(db, _)| db)
+}
+
+/// `open`, plus the id of the row named by `cfg.repo_name` — which is what
+/// `init` prints, and is not necessarily the lowest repo id if a root ever
+/// holds more than one repo.
+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<i64> {
+ 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.
+///
+/// The HTTP server is deliberately not started here. This module sits below the
+/// UI and must not be able to name it.
+pub fn serve(cfg: &Config) -> Result<Db> {
+ 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)
+}