summaryrefslogtreecommitdiffstats
path: root/atuin-daemon/src/server/sync.rs
blob: 363e20eb138df2311098cb21b78cda8c89ba759c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use eyre::{Result, WrapErr};
use tokio::time::{self, MissedTickBehavior};

use atuin_client::{
    encryption,
    history::store::HistoryStore,
    record::{sqlite_store::SqliteStore, sync},
    settings::Settings,
};

pub async fn worker(settings: Settings, store: SqliteStore) -> Result<()> {
    tracing::info!("booting sync worker");
    let mut ticker = time::interval(time::Duration::from_secs(5));

    // 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. No bueno!
    ticker.set_missed_tick_behavior(MissedTickBehavior::Skip);

    loop {
        ticker.tick().await;
        tracing::info!("sync worker tick");

        let (uploaded, downloaded) = sync::sync(&settings, &store).await?;

        tracing::info!(
            uploaded = ?uploaded,
            downloaded = ?downloaded,
            "sync complete"
        );
    }
}