Skip to content

Commit 6e50e3f

Browse files
committed
Stopping the synchronizers when needed to avoid sync issues
1 parent c44481a commit 6e50e3f

File tree

4 files changed

+116
-121
lines changed

4 files changed

+116
-121
lines changed

src/asynch/dropbox.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@
1616

1717
use std::io::Read;
1818
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
19-
use std::sync::{LazyLock, mpsc};
19+
use std::sync::{mpsc, LazyLock};
2020
use std::sync::{Arc, Mutex};
2121
use std::time::{self, Duration};
2222

2323
use async_trait::async_trait;
2424
use http::{StatusCode, Uri};
2525
use http_body_util::BodyExt;
2626
use reqwest::Body;
27-
use tokio::time::sleep;
2827
use std::convert::Infallible;
28+
use tokio::time::sleep;
2929

3030
use hyper::server::conn::http1;
3131
use hyper::service::service_fn;
@@ -73,7 +73,7 @@ const HTTP_GET_RESPONSE_BODY: &str = r#"
7373
</html>
7474
"#;
7575

76-
static STOP_SYNCHRONIZATION: LazyLock<Mutex<bool>> = LazyLock::new(|| { Mutex::new(false) });
76+
static STOP_SYNCHRONIZATION: LazyLock<Mutex<bool>> = LazyLock::new(|| Mutex::new(false));
7777

7878
/// A Dropbox synchronizer
7979
#[derive(Clone)]
@@ -286,12 +286,25 @@ impl Synchronizer {
286286
debug!("DBX: Uploaded all");
287287
Ok(())
288288
}
289+
290+
fn mark_stop_synchronization() -> errors::Result<()> {
291+
let mut s = STOP_SYNCHRONIZATION.lock()?;
292+
*s = true;
293+
Ok(())
294+
}
295+
296+
fn mark_start_synchronization() -> errors::Result<()> {
297+
let mut s = STOP_SYNCHRONIZATION.lock()?;
298+
*s = false;
299+
Ok(())
300+
}
289301
}
290302

291303
#[async_trait]
292304
impl super::AsyncTask for Synchronizer {
293-
async fn init(&mut self) {
305+
async fn init(&mut self) -> errors::Result<()> {
294306
if self.conf.is_filled() {
307+
Self::mark_start_synchronization()?;
295308
match self.get_short_lived_token().await {
296309
Ok(short_lived_token) => {
297310
if let Err(error) = self.conf.set_short_lived_token(short_lived_token) {
@@ -303,6 +316,7 @@ impl super::AsyncTask for Synchronizer {
303316
}
304317
}
305318
}
319+
Ok(())
306320
}
307321

308322
async fn execute(&self) -> errors::Result<SyncStatus> {
@@ -317,15 +331,10 @@ impl super::AsyncTask for Synchronizer {
317331
}
318332
sleep(Duration::from_millis(10000)).await;
319333
match self.do_execute().await {
320-
Ok(SyncStatus::None) => {
321-
322-
},
323-
Ok(sync_status) => {
324-
return Ok(sync_status)
325-
}
334+
Ok(SyncStatus::None) => {}
335+
Ok(sync_status) => return Ok(sync_status),
326336
Err(error) => {
327-
let mut stop_sync = STOP_SYNCHRONIZATION.lock()?;
328-
*stop_sync = true;
337+
Self::mark_stop_synchronization()?;
329338
return Err(error);
330339
}
331340
}
@@ -334,6 +343,10 @@ impl super::AsyncTask for Synchronizer {
334343
return Ok(SyncStatus::None);
335344
}
336345
}
346+
347+
fn stop(&mut self) -> errors::Result<()> {
348+
return Self::mark_stop_synchronization()
349+
}
337350
}
338351

339352
fn parse_version_str(s: &str) -> errors::Result<ServerVersionData> {
@@ -460,7 +473,10 @@ pub(crate) async fn retrieve_token(url_string: String) -> errors::Result<Zeroizi
460473
let listener = TcpListener::bind(addr)
461474
.await
462475
.expect("Cannot bind TcpListener to retrive the dropbox token");
463-
debug!("Server started. Serving connections at {}", addr.to_string());
476+
debug!(
477+
"Server started. Serving connections at {}",
478+
addr.to_string()
479+
);
464480

465481
// Spawn the server loop
466482
let handle = tokio::task::spawn(async move {
@@ -499,8 +515,7 @@ pub(crate) async fn retrieve_token(url_string: String) -> errors::Result<Zeroizi
499515
.unwrap()
500516
} else {
501517
let tx = shared_tx.lock().unwrap();
502-
let _ =
503-
tx.send(Err(RustKeylockError::GeneralError("error".to_string())));
518+
let _ = tx.send(Err(RustKeylockError::GeneralError("error".to_string())));
504519
resp_builder
505520
.status(StatusCode::BAD_REQUEST)
506521
.body(reqwest::Body::from(Vec::new()))
@@ -532,7 +547,6 @@ pub(crate) async fn retrieve_token(url_string: String) -> errors::Result<Zeroizi
532547
)
533548
}
534549
});
535-
536550
}
537551
});
538552

src/asynch/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ pub(crate) type BoxedRklHttpAsyncClient = Box<dyn RklHttpAsyncClient<ResType=Vec
3434
#[async_trait]
3535
pub trait AsyncTask: Send {
3636
/// Initializes a task
37-
async fn init(&mut self);
37+
async fn init(&mut self) -> errors::Result<()>;
3838
/// Executes the task
3939
async fn execute(&self) -> errors::Result<SyncStatus>;
40+
/// Stops a task
41+
fn stop(&mut self) -> errors::Result<()>;
4042
}
4143

4244
#[derive(PartialEq, Debug)]

src/asynch/nextcloud.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
use async_trait::async_trait;
1818
use reqwest::{Body, Client, Request, Response};
19-
use tokio::time::sleep;
2019
use std::io::prelude::*;
20+
use std::sync::{LazyLock, Mutex};
2121
use std::time::Duration;
22+
use tokio::time::sleep;
2223
use url::Url;
23-
use std::sync::{LazyLock, Mutex};
2424

2525
use http::{Method, StatusCode};
2626
use log::*;
@@ -35,7 +35,7 @@ use crate::errors::RustKeylockError;
3535
use crate::SystemConfiguration;
3636
use crate::{errors, file_handler};
3737

38-
static STOP_SYNCHRONIZATION: LazyLock<Mutex<bool>> = LazyLock::new(|| { Mutex::new(false) });
38+
static STOP_SYNCHRONIZATION: LazyLock<Mutex<bool>> = LazyLock::new(|| Mutex::new(false));
3939

4040
/// A (Next/Own)cloud synchronizer
4141
#[derive(Clone)]
@@ -90,7 +90,7 @@ impl Synchronizer {
9090
let s = STOP_SYNCHRONIZATION.lock()?;
9191
Ok(*s)
9292
}
93-
93+
9494
fn stop_synchronization(&self) -> errors::Result<()> {
9595
if !self.never_stop_synchronization {
9696
let mut stop_sync = STOP_SYNCHRONIZATION.lock()?;
@@ -532,11 +532,26 @@ impl Synchronizer {
532532

533533
res
534534
}
535+
536+
fn mark_stop_synchronization() -> errors::Result<()> {
537+
let mut s = STOP_SYNCHRONIZATION.lock()?;
538+
*s = true;
539+
Ok(())
540+
}
541+
542+
fn mark_start_synchronization() -> errors::Result<()> {
543+
let mut s = STOP_SYNCHRONIZATION.lock()?;
544+
*s = false;
545+
Ok(())
546+
}
535547
}
536548

537549
#[async_trait]
538550
impl super::AsyncTask for Synchronizer {
539-
async fn init(&mut self) {}
551+
async fn init(&mut self) -> errors::Result<()> {
552+
Self::mark_start_synchronization()?;
553+
Ok(())
554+
}
540555

541556
async fn execute(&self) -> errors::Result<SyncStatus> {
542557
if self.conf.is_filled() {
@@ -572,6 +587,10 @@ impl super::AsyncTask for Synchronizer {
572587
return Ok(SyncStatus::None);
573588
}
574589
}
590+
591+
fn stop(&mut self) -> errors::Result<()> {
592+
return Self::mark_stop_synchronization();
593+
}
575594
}
576595

577596
/// The configuration that is retrieved from the rust-keylock encrypted file
@@ -794,7 +813,8 @@ mod nextcloud_tests {
794813
use super::super::super::{errors, file_handler, SystemConfiguration};
795814
use super::super::AsyncTask;
796815

797-
static TXMAP: LazyLock<Mutex<HashMap<String, SyncSender<bool>>>> = LazyLock::new(|| { Mutex::new(HashMap::new()) });
816+
static TXMAP: LazyLock<Mutex<HashMap<String, SyncSender<bool>>>> =
817+
LazyLock::new(|| Mutex::new(HashMap::new()));
798818

799819
fn get_tx_for(command: &str) -> SyncSender<bool> {
800820
let map = TXMAP.lock().unwrap();
@@ -817,8 +837,8 @@ mod nextcloud_tests {
817837
false,
818838
)
819839
.unwrap();
820-
let nc =
821-
super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), "filename", true).unwrap();
840+
let nc = super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), "filename", true)
841+
.unwrap();
822842

823843
assert!(nc.conf.decrypted_password().unwrap().as_str() == password)
824844
}
@@ -980,7 +1000,8 @@ mod nextcloud_tests {
9801000
thread::spawn(move || {
9811001
let rt = tokio::runtime::Runtime::new().unwrap();
9821002
let nc =
983-
super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), filename, true).unwrap();
1003+
super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), filename, true)
1004+
.unwrap();
9841005
let res = rt.block_on(nc.execute());
9851006
let _ = tx.send(res);
9861007
});
@@ -1036,7 +1057,8 @@ mod nextcloud_tests {
10361057
thread::spawn(move || {
10371058
let rt = tokio::runtime::Runtime::new().unwrap();
10381059
let nc =
1039-
super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), filename, true).unwrap();
1060+
super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), filename, true)
1061+
.unwrap();
10401062
let res = rt.block_on(nc.execute());
10411063
let _ = tx.send(res);
10421064
});
@@ -1185,7 +1207,8 @@ mod nextcloud_tests {
11851207
thread::spawn(move || {
11861208
let rt = tokio::runtime::Runtime::new().unwrap();
11871209
let nc =
1188-
super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), filename, true).unwrap();
1210+
super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), filename, true)
1211+
.unwrap();
11891212
let res = rt.block_on(nc.execute());
11901213
let _ = tx.send(res);
11911214
});
@@ -1226,7 +1249,8 @@ mod nextcloud_tests {
12261249
thread::spawn(move || {
12271250
let rt = tokio::runtime::Runtime::new().unwrap();
12281251
let nc =
1229-
super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), filename, true).unwrap();
1252+
super::Synchronizer::new2(&ncc, &SystemConfiguration::default(), filename, true)
1253+
.unwrap();
12301254
let res = rt.block_on(nc.execute());
12311255
let _ = tx.send(res);
12321256
});

0 commit comments

Comments
 (0)