Skip to content

Commit

Permalink
feat: more progress on sync, added wip cli options sync_dir, start_da…
Browse files Browse the repository at this point in the history
…te, buckets
  • Loading branch information
ErikBjare committed Mar 23, 2022
1 parent fcf629e commit e741278
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 22 deletions.
9 changes: 7 additions & 2 deletions aw-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct Opts {
#[clap(long)]
port: Option<String>,
/// Path to database override
/// Also implies --no-legacy-import if no db found
#[clap(long)]
dbpath: Option<String>,
/// Device ID override
Expand Down Expand Up @@ -66,7 +67,7 @@ async fn main() -> Result<(), rocket::Error> {
}

// Set db path if overridden
let db_path: String = if let Some(dbpath) = opts.dbpath {
let db_path: String = if let Some(dbpath) = opts.dbpath.clone() {
dbpath
} else {
dirs::db_path(testing)
Expand All @@ -80,7 +81,11 @@ async fn main() -> Result<(), rocket::Error> {
let asset_path = get_asset_path();
info!("Using aw-webui assets at path {:?}", asset_path);

let legacy_import = !opts.no_legacy_import;
// Only use legacy import if opts.dbpath is not set
let legacy_import = !opts.no_legacy_import && opts.dbpath.is_none();
if opts.dbpath.is_some() {
info!("Since custom dbpath is set, --no-legacy-import is implied");
}

let device_id: String = if let Some(id) = opts.device_id {
id
Expand Down
2 changes: 1 addition & 1 deletion aw-sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ name = "aw_sync"
path = "src/lib.rs"

[[bin]]
name = "aw-sync-rust"
name = "aw-sync"
path = "src/main.rs"

[dependencies]
Expand Down
19 changes: 15 additions & 4 deletions aw-sync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ Works by syncing local buckets with a special folder, which in turn should be sy

## Usage

TODO: Write usage instructions for how to use with a normal testing/staging (5666) instance, to actually sync between devices.

```
cargo run --bin aw-sync-rust -- --port 5666 --help
```


## Testing

**Note:** this documents usage for testing, it is not yet ready for production usage.

### Pushing to the sync directory
It assumes you're running temporary aw-server instances.

First start your aw-server instance.
### Pushing to the sync directory

For example:
First start the sending aw-server instance. For example:

```sh
PORT=5667
Expand All @@ -27,7 +36,9 @@ Then run `cargo run --bin aw-sync-rust -- --port 5667` to sync your instance's b

### Pulling from the sync directory

Now to sync things back from the sync directory onto another instance. First, lets start another instance:
Now to sync things back from the sync directory into another instance.

First, lets start another instance:

```sh
PORT=5668
Expand Down
69 changes: 55 additions & 14 deletions aw-sync/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// What needs to be done:
// - [x] Setup local sync bucket
// - [x] Import local buckets and sync events from aw-server (either through API or through creating a read-only Datastore)
// - [x] Import buckets and sync events from remotes
// - [ ] Add CLI arguments
// - [x] For which local server to use
// - [x] For which sync dir to use
// - [ ] Date to start syncing from

#[macro_use]
extern crate log;
extern crate chrono;
Expand All @@ -6,48 +15,80 @@ extern crate serde_json;

use std::path::Path;

use chrono::{DateTime, Utc};
use clap::Parser;

use aw_client_rust::AwClient;

mod sync;

const DEFAULT_PORT: &str = "5600";

#[derive(Parser)]
#[clap(version = "0.1", author = "Erik Bjäreholt")]
struct Opts {
/// Host of instance to connect to
#[clap(long, default_value = "127.0.0.1")]
host: String,
/// Port of instance to connect to
#[clap(long, default_value = "5666")]
#[clap(long, default_value = DEFAULT_PORT)]
port: String,
/// Convenience option for using the default testing host and port.
#[clap(long)]
testing: bool,
/// Path to sync directory
/// If not specified, exit
#[clap(long)]
sync_dir: String,
/// Date to start syncing from
/// If not specified, start from beginning
/// NOTE: might be unstable, as count cannot be used to verify integrity of sync.
/// Format: YYYY-MM-DD
#[clap(long)]
start_date: Option<String>,
/// Specify buckets to sync
/// If not specified, all buckets will be synced
#[clap(long)]
buckets: Vec<String>,
}

fn main() {
// What needs to be done:
// - [x] Setup local sync bucket
// - [x] Import local buckets and sync events from aw-server (either through API or through creating a read-only Datastore)
// - [x] Import buckets and sync events from remotes
// - [ ] Add CLI arguments
// - [ ] For which local server to use
// - [ ] For which sync dir to use

fn main() -> std::io::Result<()> {
let opts: Opts = Opts::parse();

println!("Started aw-sync-rust...");

aw_server::logging::setup_logger(true).expect("Failed to setup logging");

// TODO: Get path using dirs module
let sync_directory = Path::new("sync-testing");
let sync_directory = if opts.sync_dir.is_empty() {
println!("No sync directory specified, exiting...");
std::process::exit(1);
} else {
Path::new(&opts.sync_dir)
};

let client = AwClient::new(opts.host.as_str(), opts.port.as_str(), "aw-sync-rust");
let port = if opts.testing && opts.port == DEFAULT_PORT {
"5666"
} else {
&opts.port
};

sync::sync_run(sync_directory, client);
let client = AwClient::new(opts.host.as_str(), port, "aw-sync-rust");

let start: Option<DateTime<Utc>> = opts.start_date.map(|date| {
chrono::DateTime::parse_from_rfc3339(&date)
.unwrap()
.with_timezone(&chrono::Utc)
});
sync::sync_run(sync_directory, client, opts.buckets, start).map_err(|e| {
println!("Error: {}", e);
std::io::Error::new(std::io::ErrorKind::Other, e)
})?;
info!("Finished successfully, exiting...");

// Needed to give the datastores some time to commit before program is shut down.
// 100ms isn't actually needed, seemed to work fine with as little as 10ms, but I'd rather give
// it some wiggleroom.
std::thread::sleep(std::time::Duration::from_millis(100));

Ok(())
}
9 changes: 8 additions & 1 deletion aw-sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,12 @@ impl AccessMethod for AwClient {
}

/// Performs a single sync pass
pub fn sync_run(sync_directory: &Path, client: AwClient) {
pub fn sync_run(
sync_directory: &Path,
client: AwClient,
buckets: Vec<String>,
start: Option<DateTime<Utc>>,
) -> Result<(), String> {
fs::create_dir_all(sync_directory).unwrap();

let info = client.get_info().unwrap();
Expand Down Expand Up @@ -166,6 +171,8 @@ pub fn sync_run(sync_directory: &Path, client: AwClient) {
for ds_from in &ds_remotes {
log_buckets(ds_from);
}

Ok(())
}

/// Returns a list of all remote dbs
Expand Down

0 comments on commit e741278

Please sign in to comment.