diff options
| author | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-07-09 22:00:59 +0200 |
|---|---|---|
| committer | Benedikt Peetz <benedikt.peetz@b-peetz.de> | 2026-07-09 22:00:59 +0200 |
| commit | 620cf8fa33835c1ce1e56b1028ff6e2a6fbe0f39 (patch) | |
| tree | e97e54ff112c8e3fa9f88335bfd1275420a4d69f /crates/daemon/src/daemon.rs | |
| parent | chore: Separate daemon, client, server, and lib into crates (diff) | |
| download | atuin-620cf8fa33835c1ce1e56b1028ff6e2a6fbe0f39.zip | |
chore: Add more things
Diffstat (limited to 'crates/daemon/src/daemon.rs')
| -rw-r--r-- | crates/daemon/src/daemon.rs | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/crates/daemon/src/daemon.rs b/crates/daemon/src/daemon.rs index 80aaeef8..8f0a5957 100644 --- a/crates/daemon/src/daemon.rs +++ b/crates/daemon/src/daemon.rs @@ -17,7 +17,7 @@ use crate::atuin_client::{ use eyre::{Context, Result}; use tokio::sync::{RwLock, broadcast}; -use crate::atuin_daemon::events::DaemonEvent; +use crate::events::DaemonEvent; // ============================================================================ // DaemonState @@ -27,7 +27,7 @@ use crate::atuin_daemon::events::DaemonEvent; /// /// This contains all the resources that components and services need access to. /// The state is wrapped in an `Arc` and accessed via [`DaemonHandle`]. -pub(crate) struct DaemonState { +pub struct DaemonState { // Event bus event_tx: broadcast::Sender<DaemonEvent>, @@ -72,7 +72,7 @@ pub(crate) struct DaemonState { /// let history = handle.history_db().load(id).await?; /// ``` #[derive(Clone)] -pub(crate) struct DaemonHandle { +pub struct DaemonHandle { state: Arc<DaemonState>, } @@ -83,7 +83,7 @@ impl DaemonHandle { /// /// This is fire-and-forget - if no receivers are listening (which shouldn't /// happen in normal operation), the event is dropped silently. - pub(crate) fn emit(&self, event: DaemonEvent) { + pub fn emit(&self, event: DaemonEvent) { if let Err(e) = self.state.event_tx.send(event) { tracing::warn!("failed to emit event (no receivers?): {e}"); } @@ -94,12 +94,12 @@ impl DaemonHandle { /// Returns a receiver that will receive all events emitted after this call. /// Useful for components that need to listen for events outside of the /// normal `handle_event` callback flow. - pub(crate) fn subscribe(&self) -> broadcast::Receiver<DaemonEvent> { + pub fn subscribe(&self) -> broadcast::Receiver<DaemonEvent> { self.state.event_tx.subscribe() } /// Request graceful shutdown of the daemon. - pub(crate) fn shutdown(&self) { + pub fn shutdown(&self) { self.emit(DaemonEvent::ShutdownRequested); } @@ -109,7 +109,7 @@ impl DaemonHandle { /// /// This acquires a read lock on the settings. For most use cases, clone /// the settings if you need to hold onto them. - pub(crate) async fn settings(&self) -> tokio::sync::RwLockReadGuard<'_, Settings> { + pub async fn settings(&self) -> tokio::sync::RwLockReadGuard<'_, Settings> { self.state.settings.read().await } @@ -117,26 +117,26 @@ impl DaemonHandle { /// /// Use this when settings have already been loaded (e.g., from a file watcher) /// to avoid parsing the config file twice. - pub(crate) async fn apply_settings(&self, settings: Settings) { + pub async fn apply_settings(&self, settings: Settings) { *self.state.settings.write().await = settings; self.emit(DaemonEvent::SettingsReloaded); tracing::info!("settings applied"); } /// Get the encryption key. - pub(crate) fn encryption_key(&self) -> &[u8; 32] { + pub fn encryption_key(&self) -> &[u8; 32] { &self.state.encryption_key } // ---- Database ---- /// Get a reference to the history database. - pub(crate) fn history_db(&self) -> &HistoryDatabase { + pub fn history_db(&self) -> &HistoryDatabase { &self.state.history_db } /// Get a reference to the record store. - pub(crate) fn store(&self) -> &SqliteStore { + pub fn store(&self) -> &SqliteStore { &self.state.store } } @@ -171,7 +171,7 @@ impl std::fmt::Debug for DaemonHandle { /// # Example /// /// ```ignore -/// pub(crate) struct MyComponent { +/// pub struct MyComponent { /// handle: Option<DaemonHandle>, /// } /// @@ -203,7 +203,7 @@ impl std::fmt::Debug for DaemonHandle { /// } /// ``` #[tonic::async_trait] -pub(crate) trait Component: Send + Sync { +pub trait Component: Send + Sync { /// Human-readable name for logging and debugging. fn name(&self) -> &'static str; @@ -247,21 +247,21 @@ pub(crate) trait Component: Send + Sync { /// /// Events emitted during handling are queued and processed in subsequent /// iterations, ensuring the loop eventually drains. -pub(crate) struct Daemon { +pub struct Daemon { components: Vec<Box<dyn Component>>, handle: DaemonHandle, } impl Daemon { /// Create a new daemon builder. - pub(crate) fn builder(settings: Settings) -> DaemonBuilder { + pub fn builder(settings: Settings) -> DaemonBuilder { DaemonBuilder::new(settings) } /// Get a clone of the daemon handle. /// /// The handle can be used to emit events, access settings, etc. - pub(crate) fn handle(&self) -> DaemonHandle { + pub fn handle(&self) -> DaemonHandle { self.handle.clone() } @@ -269,7 +269,7 @@ impl Daemon { /// /// This must be called before `run_event_loop()`. It initializes all /// registered components with the daemon handle. - pub(crate) async fn start_components(&mut self) -> Result<()> { + pub async fn start_components(&mut self) -> Result<()> { for component in &mut self.components { tracing::info!(component = component.name(), "starting component"); component @@ -284,7 +284,7 @@ impl Daemon { /// /// This processes events until a [`ShutdownRequested`] event is received. /// Components must be started first via `start_components()`. - pub(crate) async fn run_event_loop(&mut self) -> Result<()> { + pub async fn run_event_loop(&mut self) -> Result<()> { let mut event_rx = self.handle.subscribe(); loop { match event_rx.recv().await { @@ -314,7 +314,7 @@ impl Daemon { /// Stop all components. /// /// This performs graceful shutdown of all components. - pub(crate) async fn stop_components(&mut self) { + pub async fn stop_components(&mut self) { for component in &mut self.components { tracing::info!(component = component.name(), "stopping component"); if let Err(e) = component.stop().await { @@ -361,7 +361,7 @@ impl Daemon { /// /// daemon.run().await?; /// ``` -pub(crate) struct DaemonBuilder { +pub struct DaemonBuilder { settings: Settings, store: Option<SqliteStore>, history_db: Option<HistoryDatabase>, @@ -370,7 +370,7 @@ pub(crate) struct DaemonBuilder { impl DaemonBuilder { /// Create a new daemon builder with the given settings. - pub(crate) fn new(settings: Settings) -> Self { + pub fn new(settings: Settings) -> Self { Self { settings, store: None, @@ -380,13 +380,13 @@ impl DaemonBuilder { } /// Set the record store. - pub(crate) fn store(mut self, store: SqliteStore) -> Self { + pub fn store(mut self, store: SqliteStore) -> Self { self.store = Some(store); self } /// Set the history database. - pub(crate) fn history_db(mut self, db: HistoryDatabase) -> Self { + pub fn history_db(mut self, db: HistoryDatabase) -> Self { self.history_db = Some(db); self } @@ -394,7 +394,7 @@ impl DaemonBuilder { /// Register a component. /// /// Components are started in registration order and stopped in reverse order. - pub(crate) fn component(mut self, component: impl Component + 'static) -> Self { + pub fn component(mut self, component: impl Component + 'static) -> Self { self.components.push(Box::new(component)); self } @@ -402,7 +402,7 @@ impl DaemonBuilder { /// Build the daemon. /// /// This loads the encryption key and creates the daemon state. - pub(crate) fn build(self) -> Result<Daemon> { + pub fn build(self) -> Result<Daemon> { let store = self.store.ok_or_else(|| eyre::eyre!("store is required"))?; let history_db = self .history_db |
