aboutsummaryrefslogtreecommitdiffstats
path: root/crates/daemon/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/daemon/src/main.rs')
-rw-r--r--crates/daemon/src/main.rs45
1 files changed, 9 insertions, 36 deletions
diff --git a/crates/daemon/src/main.rs b/crates/daemon/src/main.rs
index 59d4c7ff..1d94b5fa 100644
--- a/crates/daemon/src/main.rs
+++ b/crates/daemon/src/main.rs
@@ -4,14 +4,13 @@ use std::{
fs::{self, File, OpenOptions},
io::Write,
path::{Path, PathBuf},
- time::{Duration, Instant},
};
use clap::Parser;
use eyre::WrapErr;
-use eyre::{Context, Result, bail, eyre};
+use eyre::{Result, bail};
use fs4::fs_std::FileExt;
-use tokio::time::sleep;
+use tracing_subscriber::util::SubscriberInitExt;
use crate::{
aclient::{database::ClientSqlite, record::sqlite_store::SqliteStore, settings::Settings},
@@ -40,6 +39,10 @@ enum Cmd {
#[tokio::main]
async fn main() -> Result<()> {
+ if let Err(e) = tracing_subscriber::registry().try_init() {
+ eprintln!("failed to initialize logging: {e}");
+ }
+
let settings = Settings::new().wrap_err("could not load client settings")?;
let db_path = PathBuf::from(settings.db_path.as_str());
let record_store_path = PathBuf::from(settings.record_store_path.as_str());
@@ -62,7 +65,7 @@ async fn boot(settings: Settings, store: SqliteStore, history_db: ClientSqlite)
let mut daemon = Daemon::builder(settings.clone())
.store(store)
- .history_db(history_db.clone())
+ .history_db(history_db)
.build()?;
let handle = {
@@ -79,7 +82,7 @@ async fn boot(settings: Settings, store: SqliteStore, history_db: ClientSqlite)
handle
};
- let history_service = HistoryService::new(handle.clone(), history_db).await?;
+ let history_service = HistoryService::new(handle.clone()).await?;
let control_service = ControlService::new(handle.clone());
server::run_grpc_server(
@@ -89,7 +92,7 @@ async fn boot(settings: Settings, store: SqliteStore, history_db: ClientSqlite)
handle,
)?;
- daemon.run_event_loop().await?;
+ daemon.wait_for_shutdown().await?;
tracing::info!("daemon shut down complete");
Ok(())
@@ -154,33 +157,3 @@ fn open_lock_file(path: &Path) -> Result<File> {
.open(path)
.wrap_err_with(|| format!("could not open lock file {}", path.display()))
}
-
-async fn wait_for_lock(path: &Path, timeout: Duration) -> Result<File> {
- const LOCK_POLL: Duration = Duration::from_millis(20);
-
- let file = open_lock_file(path)?;
- let start = Instant::now();
-
- loop {
- match file.try_lock_exclusive() {
- Ok(true) => return Ok(file),
- Ok(false) => {
- if start.elapsed() >= timeout {
- bail!("timed out waiting for lock at {}", path.display());
- }
-
- sleep(LOCK_POLL).await;
- }
- Err(err) => {
- return Err(eyre!("could not lock {}: {err}", path.display()));
- }
- }
- }
-}
-
-async fn wait_for_pidfile_available(path: &Path, timeout: Duration) -> Result<()> {
- let file = wait_for_lock(path, timeout).await?;
- file.unlock()
- .wrap_err_with(|| format!("failed to unlock {}", path.display()))?;
- Ok(())
-}