aboutsummaryrefslogtreecommitdiffstats
path: root/crates/turtle/src/atuin_daemon/components/sync.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/turtle/src/atuin_daemon/components/sync.rs')
-rw-r--r--crates/turtle/src/atuin_daemon/components/sync.rs37
1 files changed, 21 insertions, 16 deletions
diff --git a/crates/turtle/src/atuin_daemon/components/sync.rs b/crates/turtle/src/atuin_daemon/components/sync.rs
index fbfbbd67..8b5b621d 100644
--- a/crates/turtle/src/atuin_daemon/components/sync.rs
+++ b/crates/turtle/src/atuin_daemon/components/sync.rs
@@ -27,9 +27,9 @@ enum SyncCommand {
/// Sync state - tracks whether we're in normal operation or retrying after failure.
#[derive(Clone, Copy, PartialEq, Eq)]
enum SyncState {
- /// Normal operation. Periodic syncs only run if auto_sync is enabled.
+ /// Normal operation. Periodic syncs only run if [`auto_sync`] is enabled.
Idle,
- /// Retrying after a sync failure. Retries continue regardless of auto_sync
+ /// Retrying after a sync failure. Retries continue regardless of [`auto_sync`]
/// until the sync succeeds.
Retrying,
}
@@ -39,7 +39,7 @@ enum SyncState {
/// This component:
/// - Runs a background sync loop on a configurable interval
/// - Implements exponential backoff on sync failures
-/// - Responds to ForceSync events for immediate sync
+/// - Responds to [`ForceSync`] events for immediate sync
/// - Emits SyncCompleted/SyncFailed events
pub(crate) struct SyncComponent {
task_handle: Option<tokio::task::JoinHandle<()>>,
@@ -80,22 +80,28 @@ impl Component for SyncComponent {
}
async fn handle_event(&mut self, event: &DaemonEvent) -> Result<()> {
- if let DaemonEvent::ForceSync = event {
- tracing::info!("force sync requested");
- if let Some(tx) = &self.command_tx {
- let _ = tx.send(SyncCommand::ForceSync).await;
+ match event {
+ DaemonEvent::ForceSync => {
+ tracing::info!("force sync requested");
+ if let Some(tx) = &self.command_tx {
+ drop(tx.send(SyncCommand::ForceSync).await);
+ }
+ }
+ DaemonEvent::SyncFailed { error } => {
+ tracing::error!(?error, "Sync failed.");
}
+ _ => (),
}
Ok(())
}
async fn stop(&mut self) -> Result<()> {
if let Some(tx) = &self.command_tx {
- let _ = tx.send(SyncCommand::Stop).await;
+ drop(tx.send(SyncCommand::Stop).await);
}
if let Some(handle) = self.task_handle.take() {
// Give the task a moment to shut down gracefully
- let _ = tokio::time::timeout(std::time::Duration::from_secs(5), handle).await;
+ drop(time::timeout(Duration::from_secs(5), handle).await);
}
tracing::info!("sync component stopped");
Ok(())
@@ -126,7 +132,7 @@ async fn sync_loop(handle: DaemonHandle, mut cmd_rx: mpsc::Receiver<SyncCommand>
// Don't backoff by more than 30 mins (with a random jitter of up to 1 min)
let max_interval: f64 = 60.0 * 30.0 + rand::thread_rng().gen_range(0.0..60.0);
- let mut ticker = time::interval(time::Duration::from_secs(settings.daemon.sync_frequency));
+ let mut ticker = time::interval(Duration::from_secs(settings.daemon.sync_frequency));
// IMPORTANT: without this, if we miss ticks because a sync takes ages or is otherwise delayed,
// we may end up running a lot of syncs in a hot loop.
@@ -224,10 +230,10 @@ async fn do_sync_tick(
}
*ticker = time::interval_at(
- tokio::time::Instant::now() + Duration::from_secs(new_interval as u64),
- time::Duration::from_secs(new_interval as u64),
+ time::Instant::now() + Duration::from_secs(new_interval as u64),
+ Duration::from_secs(new_interval as u64),
);
- ticker.reset_after(time::Duration::from_secs(new_interval as u64));
+ ticker.reset_after(Duration::from_secs(new_interval as u64));
ticker.set_missed_tick_behavior(MissedTickBehavior::Skip);
tracing::error!("backing off, next sync tick in {new_interval}");
@@ -261,9 +267,8 @@ async fn do_sync_tick(
// Reset backoff on success
if ticker.period().as_secs() != settings.daemon.sync_frequency {
*ticker = time::interval_at(
- tokio::time::Instant::now()
- + Duration::from_secs(settings.daemon.sync_frequency),
- time::Duration::from_secs(settings.daemon.sync_frequency),
+ time::Instant::now() + Duration::from_secs(settings.daemon.sync_frequency),
+ Duration::from_secs(settings.daemon.sync_frequency),
);
ticker.set_missed_tick_behavior(MissedTickBehavior::Skip);
}