Skip to content

Commit

Permalink
Stop syncing remotes all the time
Browse files Browse the repository at this point in the history
  • Loading branch information
mtizim committed Nov 12, 2024
1 parent 9224b11 commit 129d385
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/entities/remotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub name: String,
pub last_sync_timestamp: i32,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
Expand Down
29 changes: 26 additions & 3 deletions src/launch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ use adw::{
};
use file_lock::{FileLock, FileOptions};
use indexmap::IndexMap;
use sea_orm::{entity::prelude::*, ActiveValue, Database, DatabaseConnection};
use sea_orm::{
entity::prelude::*, ActiveValue, Database, DatabaseConnection, IntoActiveModel, Set,
};

use std::{
boxed,
Expand Down Expand Up @@ -1078,6 +1080,8 @@ pub fn launch(app: &Application, background: bool) {
error_count
});

let mut first_sync_of_launch = true;

'main: loop {
// Break the loop if the user requested to quit the application.
if *(*CLOSE_REQUEST).lock().unwrap() {
Expand Down Expand Up @@ -1111,8 +1115,26 @@ pub fn launch(app: &Application, background: bool) {
}

util::run_in_background(|| thread::sleep(Duration::from_millis(500)));
let now = time::OffsetDateTime::now_utc();
for remote in remotes.into_iter() {
#[allow(clippy::nonminimal_bool)] //Easier to read nonsimplified
if !first_sync_of_launch
&& !((time::OffsetDateTime::from_unix_timestamp(remote.last_sync_timestamp.into())
.expect("Very very bad timestamp?")
// TODO make configurable
+ time::Duration::hours(4))
< now)
{
continue;
}

util::await_future(async {
let mut remote_am = remote.into_active_model();
remote_am.last_sync_timestamp =
Set(now.unix_timestamp().try_into().expect("2038 yet?"));
remote_am.update(&db).await.unwrap()
});

for remote in remotes {
// Process any remote deletion requests.
{
let mut remote_queue = remote_deletion_queue.get_mut_ref();
Expand Down Expand Up @@ -2602,6 +2624,7 @@ pub fn launch(app: &Application, background: bool) {
drop(item_ptr);
}
}
first_sync_of_launch = false;

// Notify that we've finished checking all remotes for changes.
let error_count = sync_errors_count();
Expand All @@ -2610,7 +2633,7 @@ pub fn launch(app: &Application, background: bool) {
let error_msg = if error_count == 1 {
"Finished sync checks with 1 error.".to_string()
} else {
tr::tr!("Finished sync checks with {} errors.", error_count)
tr::tr!("Finished sync checks with {} errors.", &error_count)
};
handle.update(|tray| tray.set_msg(error_msg));
} else {
Expand Down
28 changes: 28 additions & 0 deletions src/migrations/m20241112_193043_add_last_synced_remote.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use sea_orm::{ConnectionTrait, Statement};
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let sql = r#"
ALTER TABLE remotes
ADD COLUMN last_sync_timestamp INTEGER DEFAULT 0;
UPDATE remotes
SET last_sync_timestamp = 0
WHERE last_sync_timestamp IS NULL;
"#;
let stmt = Statement::from_string(manager.get_database_backend(), sql.to_owned());
manager.get_connection().execute(stmt).await.map(|_| ())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
let sql = r#"
ALTER TABLE remotes
DROP COLUMN last_sync_timestamp;
"#;
let stmt = Statement::from_string(manager.get_database_backend(), sql.to_owned());
manager.get_connection().execute(stmt).await.map(|_| ())
}
}
2 changes: 2 additions & 0 deletions src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub use sea_orm_migration::prelude::*;
mod m20220101_000001_create_table;
mod m20230207_204909_sync_dirs_remove_slash_suffix;
mod m20230220_215840_remote_sync_items_fix;
mod m20241112_193043_add_last_synced_remote;

pub struct Migrator;

Expand All @@ -13,6 +14,7 @@ impl MigratorTrait for Migrator {
Box::new(m20220101_000001_create_table::Migration),
Box::new(m20230207_204909_sync_dirs_remove_slash_suffix::Migration),
Box::new(m20230220_215840_remote_sync_items_fix::Migration),
Box::new(m20241112_193043_add_last_synced_remote::Migration),
]
}
}

0 comments on commit 129d385

Please sign in to comment.