diff --git a/one/src/daemon.rs b/one/src/daemon.rs index 1f1229af..f06a197f 100644 --- a/one/src/daemon.rs +++ b/one/src/daemon.rs @@ -323,11 +323,33 @@ fn spawn_database_optimizer( mut shutdown: tokio::sync::broadcast::Receiver<()>, ) -> tokio::task::JoinHandle<()> { tokio::spawn(async move { + // Recommended practice is that applications with long-lived database connections should run "PRAGMA optimize=0x10002" + // when the database connection first opens, then run "PRAGMA optimize" again at periodic intervals - perhaps once per day + // https://www.sqlite.org/pragma.html#pragma_optimize + match sqlite_pool.run_statement("PRAGMA optimize=0x10002").await { + Ok(_) => { + info!("successfully ran sqlite optimize=0x10002"); + } + Err(e) => { + warn!(error=?e, "failed to run initial database optimize statement"); + } + } + let mut duration = std::time::Duration::from_secs(60 * 60 * 24); // once daily loop { - let mut duration = std::time::Duration::from_secs(60 * 60 * 24); // once daily + let mut interval = tokio::time::interval(duration); + interval.tick().await; // first tick is immediate + tokio::select! { + _ = shutdown.recv() => { + break; + } + _ = interval.tick() => { + // start the loop over + } + } match sqlite_pool.optimize().await { Ok(_) => { info!("successfully executed database optimize"); + duration = std::time::Duration::from_secs(60 * 60 * 24); } Err(e) => { duration = std::time::Duration::from_secs(60 * 5); // try again in 5 minutes @@ -337,16 +359,6 @@ fn spawn_database_optimizer( ); } } - let mut interval = tokio::time::interval(duration); - interval.tick().await; // first tick is immediate - tokio::select! { - _ = shutdown.recv() => { - break; - } - _ = interval.tick() => { - // start the loop over - } - } } }) } diff --git a/sql/src/sqlite.rs b/sql/src/sqlite.rs index 66ec69d1..706e522f 100644 --- a/sql/src/sqlite.rs +++ b/sql/src/sqlite.rs @@ -39,17 +39,12 @@ impl SqlitePool { .foreign_keys(true); let ro_opts = conn_opts.clone().read_only(true); - let write_opts = conn_opts - // Recommended practice is that applications with long-lived database connections should run "PRAGMA optimize=0x10002" - // when the database connection first opens, then run "PRAGMA optimize" again at periodic intervals - perhaps once per day - // https://www.sqlite.org/pragma.html#pragma_optimize - .pragma("optimize", "0x10002"); let writer = SqlitePoolOptions::new() .min_connections(1) .max_connections(1) .acquire_timeout(std::time::Duration::from_secs(5)) - .connect_with(write_opts) + .connect_with(conn_opts) .await?; let reader = SqlitePoolOptions::new() .min_connections(1)