Skip to main content

max / makenotwork

2.0 KB · 51 lines History Blame Raw
1 use anyhow::Result;
2 use sando_daemon::{config, db, events, git, metrics, routes, state, sync, topology};
3 use std::net::SocketAddr;
4 use std::path::Path;
5 use std::sync::Arc;
6
7 #[tokio::main]
8 async fn main() -> Result<()> {
9 tracing_subscriber::fmt()
10 // stdout is block-buffered under systemd (no TTY) so events never
11 // reach journald until the buffer fills or the process exits. stderr
12 // is line-buffered, which is what we want for a long-running service.
13 .with_writer(std::io::stderr)
14 .with_env_filter(
15 tracing_subscriber::EnvFilter::try_from_default_env()
16 // Modules live under the library crate `sando_daemon` (since
17 // the step-5 lib/bin split). `sandod` is kept for any
18 // top-level events that originate in main.rs itself.
19 .unwrap_or_else(|_| "sando_daemon=info,sandod=info,tower_http=info".into()),
20 )
21 .init();
22
23 let cfg = Arc::new(config::Config::load()?);
24 let topo = Arc::new(topology::Topology::load(&cfg.topology_path)?);
25 tokio::fs::create_dir_all(&cfg.workdir).await?;
26 tokio::fs::create_dir_all(&cfg.release_root).await?;
27 git::ensure_bare_repo(Path::new(&topo.repo.bare_path)).await?;
28 let pool = db::connect(&cfg.db_path).await?;
29 db::migrate(&pool).await?;
30 sync::sync(&pool, &*topo).await?;
31 tracing::info!(tiers = topo.tiers.len(), bare = %topo.repo.bare_path, "topology synced");
32
33 let prom = metrics::init();
34 let addr: SocketAddr = cfg.listen.parse()?;
35 let executors = Arc::new(state::build_executors(&topo));
36 let app_state = state::AppState {
37 pool,
38 topo,
39 cfg,
40 prom,
41 active_build: Arc::new(tokio::sync::Mutex::new(None)),
42 events: events::channel(),
43 executors,
44 };
45 let app = routes::router(app_state);
46 tracing::info!(%addr, "sando daemon listening");
47 let listener = tokio::net::TcpListener::bind(addr).await?;
48 axum::serve(listener, app).await?;
49 Ok(())
50 }
51