aboutsummaryrefslogtreecommitdiffstats
path: root/crates/atuin-daemon/src
diff options
context:
space:
mode:
authorEllie Huxtable <ellie@elliehuxtable.com>2024-05-08 17:28:52 +0100
committerGitHub <noreply@github.com>2024-05-08 17:28:52 +0100
commit0da534d5249a9111f8e1af0ff517526e2c66cdb0 (patch)
tree0da2b5ac39a7231e421870e1723fad27b03785e4 /crates/atuin-daemon/src
parentfix: add protobuf compiler to docker image (#2009) (diff)
downloadatuin-0da534d5249a9111f8e1af0ff517526e2c66cdb0.zip
fix: add incremental rebuild to daemon loop (#2010)
Diffstat (limited to 'crates/atuin-daemon/src')
-rw-r--r--crates/atuin-daemon/src/server.rs9
-rw-r--r--crates/atuin-daemon/src/server/sync.rs35
2 files changed, 37 insertions, 7 deletions
diff --git a/crates/atuin-daemon/src/server.rs b/crates/atuin-daemon/src/server.rs
index 42ef1701..72305737 100644
--- a/crates/atuin-daemon/src/server.rs
+++ b/crates/atuin-daemon/src/server.rs
@@ -166,7 +166,7 @@ pub async fn listen(
let host_id = Settings::host_id().expect("failed to get host_id");
let history_store = HistoryStore::new(store.clone(), host_id, encryption_key);
- let history = HistoryService::new(history_store, history_db);
+ let history = HistoryService::new(history_store.clone(), history_db.clone());
let socket = settings.daemon.socket_path.clone();
let uds = UnixListener::bind(socket.clone())?;
@@ -175,7 +175,12 @@ pub async fn listen(
tracing::info!("listening on unix socket {:?}", socket);
// start services
- tokio::spawn(sync::worker(settings.clone(), store));
+ tokio::spawn(sync::worker(
+ settings.clone(),
+ store,
+ history_store,
+ history_db,
+ ));
Server::builder()
.add_service(HistoryServer::new(history))
diff --git a/crates/atuin-daemon/src/server/sync.rs b/crates/atuin-daemon/src/server/sync.rs
index de34779c..ba037d4e 100644
--- a/crates/atuin-daemon/src/server/sync.rs
+++ b/crates/atuin-daemon/src/server/sync.rs
@@ -2,14 +2,32 @@ use eyre::Result;
use rand::Rng;
use tokio::time::{self, MissedTickBehavior};
+use atuin_client::database::Sqlite as HistoryDatabase;
use atuin_client::{
+ encryption,
+ history::store::HistoryStore,
record::{sqlite_store::SqliteStore, sync},
settings::Settings,
};
-pub async fn worker(settings: Settings, store: SqliteStore) -> Result<()> {
+use atuin_dotfiles::store::{var::VarStore, AliasStore};
+
+pub async fn worker(
+ settings: Settings,
+ store: SqliteStore,
+ history_store: HistoryStore,
+ history_db: HistoryDatabase,
+) -> Result<()> {
tracing::info!("booting sync worker");
+ let encryption_key: [u8; 32] = encryption::load_key(&settings)?.into();
+ let host_id = Settings::host_id().expect("failed to get host_id");
+ let alias_store = AliasStore::new(store.clone(), host_id, encryption_key);
+ let var_store = VarStore::new(store.clone(), host_id, encryption_key);
+
+ // 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));
// IMPORTANT: without this, if we miss ticks because a sync takes ages or is otherwise delayed,
@@ -24,13 +42,13 @@ pub async fn worker(settings: Settings, store: SqliteStore) -> Result<()> {
if let Err(e) = res {
tracing::error!("sync tick failed with {e}");
+
let mut rng = rand::thread_rng();
- let new_interval = ticker.period().as_secs_f64() * rng.gen_range(2.0..2.2);
+ let mut new_interval = ticker.period().as_secs_f64() * rng.gen_range(2.0..2.2);
- // Don't backoff by more than 30 mins
- if new_interval > 60.0 * 30.0 {
- continue;
+ if new_interval > max_interval {
+ new_interval = max_interval;
}
ticker = time::interval(time::Duration::from_secs(new_interval as u64));
@@ -46,6 +64,13 @@ pub async fn worker(settings: Settings, store: SqliteStore) -> Result<()> {
"sync complete"
);
+ history_store
+ .incremental_build(&history_db, &downloaded)
+ .await?;
+
+ alias_store.build().await?;
+ var_store.build().await?;
+
// Reset backoff on success
if ticker.period().as_secs() != settings.daemon.sync_frequency {
ticker = time::interval(time::Duration::from_secs(settings.daemon.sync_frequency));