Skip to content

Commit

Permalink
fix: Check internet connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulrahman1s committed Sep 3, 2022
1 parent 60ea826 commit 93e1031
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 120 deletions.
110 changes: 7 additions & 103 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ edition = "2021"

[dependencies]
anyhow = "1.0.62"
config = "0.13.2"
config = { version = "0.13.2", default-features = false, features = ["toml"] }
dirs = "4.0.0"
lazy_static = "1.4.0"
notify = "5.0.0"
rust-s3 = { version = "0.32.3", default-features = false, features = ["sync", "tags", "sync-rustls-tls"] }
spinners = "4.1.0"
online = { version = "3.0.1", default-features = false, features = ["sync"] }
3 changes: 2 additions & 1 deletion config.example.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[main]
interval = 60000
provider = "s3"
interval = 600000
path = "/home/abdulrahman/Sync"

[s3]
Expand Down
60 changes: 46 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ mod util;

use cloud::{adapters::*, CloudAdapter};
use config::Config;
use notify::{event::*, EventKind, RecursiveMode, Watcher};
use notify::{event::*, recommended_watcher, RecursiveMode, Watcher};
use std::{
collections::HashSet,
path::PathBuf,
process::exit,
sync::{mpsc::channel, Arc, Mutex},
thread,
time::Duration,
Expand All @@ -28,35 +29,60 @@ lazy_static! {
.expect("Missing main.path env")
.parse()
.expect("Invalid path string");
pub static ref IS_INTERNET_AVAILABLE: Mutex<bool> = Mutex::new(false);
pub static ref SYNCING: Mutex<bool> = Mutex::new(false);
}

fn main() -> Result<()> {
let cloud_ref = Arc::new(s3::Cloud::new());
let syncing_ref = Arc::new(Mutex::new(false));
let cloud_ref = Arc::new(
match SETTINGS
.get_string("main.provider")
.expect("Missing cloud provider in config/settings")
.replace('_', "")
.replace('-', "")
.as_str()
{
"s3" => s3::Cloud::new(),
// TODO: Support more cloud
// "googledrive" | "gdrive" => googledrive::Cloud::new(),
// "dropbox" => dropbox::Cloud::new(),
// "mega" => mega::Cloud::new(),
// "onedrive" => onedrive::Cloud::new(),
// "protondrive" => protondrive::Cloud::new(),
x => {
println!("Unspported cloud provider: {}", x);
exit(1);
}
},
);

let interval: u64 = SETTINGS
.get("main.interval")
.expect("Missing/Invalid interval value");

let cloud = cloud_ref.clone();
let syncing = syncing_ref.clone();

let online_task = thread::spawn(move || loop {
*syncing.lock().unwrap() = true;
spinner("Syncing...", "Synced!", || cloud.sync().map(|_| {}));
*syncing.lock().unwrap() = false;
thread::sleep(Duration::from_millis(interval));
check_connectivity();

if *IS_INTERNET_AVAILABLE.lock().unwrap() {
*SYNCING.lock().unwrap() = true;
spinner("Syncing...", "Synced!", || cloud.sync().map(|_| {}));
*SYNCING.lock().unwrap() = false;
thread::sleep(Duration::from_millis(interval));
} else {
println!("Skip syncing.. there are no internet connection");
}
});

let cloud = cloud_ref;
let syncing = syncing_ref;
let local_task = thread::spawn(move || {
let (tx, rx) = channel();
let mut watcher = notify::recommended_watcher(tx)?;
let mut watcher = recommended_watcher(tx)?;
let mut changes = HashSet::new();

watcher.watch(&SYNC_DIR, RecursiveMode::Recursive)?;

let mut changes = HashSet::<PathBuf>::new();

while let Ok(event) = rx.recv() {
let event = match event {
Ok(e) => e,
Expand All @@ -66,11 +92,17 @@ fn main() -> Result<()> {
}
};

if *syncing.lock().unwrap() {
println!("{:?}", event);

if !*IS_INTERNET_AVAILABLE.lock().unwrap() {
println!("Skip local syncing.. there are no internet connection");
continue;
}

println!("{:?}", event);
if *SYNCING.lock().unwrap() {
println!("Ignore event since online syncing is working");
continue;
}

match event.kind {
EventKind::Create(CreateKind::File) => {
Expand Down
6 changes: 5 additions & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::SYNC_DIR;
use crate::{IS_INTERNET_AVAILABLE, SYNC_DIR};
pub use anyhow::Result;
use spinners::{Spinner, Spinners};
use std::{
Expand Down Expand Up @@ -94,3 +94,7 @@ pub fn settings_file() -> Result<PathBuf> {

Ok(path)
}

pub fn check_connectivity() {
*IS_INTERNET_AVAILABLE.lock().unwrap() = online::sync::check(None).is_ok();
}

0 comments on commit 93e1031

Please sign in to comment.