Experiment with futures

This commit is contained in:
Adrian Heine 2020-08-16 11:08:22 +02:00
parent 907fbf95db
commit 2d3e3688fa
44 changed files with 2081 additions and 1242 deletions

View file

@ -1,9 +1,9 @@
use std::error::Error;
use std::str::FromStr;
use crate::command_runner::CommandRunner;
use crate::storage::Storage;
use crate::symbols::Symbol;
use async_trait::async_trait;
use std::error::Error;
use std::str::FromStr;
#[derive(Debug)]
pub struct Dump<'a, N, C, S> {
@ -21,34 +21,39 @@ impl<'a, N, C: CommandRunner, S> Dump<'a, N, C, S> {
}
}
fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
async fn run_sql(&self, sql: &str) -> Result<String, Box<dyn Error>> {
let b = self
.command_runner
.get_output("mariadb", args!["--skip-column-names", "-B", "-e", sql])?;
.get_output("mariadb", args!["--skip-column-names", "-B", "-e", sql])
.await?;
Ok(String::from_utf8(b)?)
}
}
#[async_trait(?Send)]
impl<N: AsRef<str>, C: CommandRunner, S: Storage> Symbol for Dump<'_, N, C, S> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
async fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let dump_date = self.storage.recent_date()?;
let _modified_date = self.run_sql(&format!("select UNIX_TIMESTAMP(MAX(UPDATE_TIME)) from information_schema.tables WHERE table_schema = '{}'", self.db_name.as_ref()))?;
let _modified_date = self.run_sql(&format!("select UNIX_TIMESTAMP(MAX(UPDATE_TIME)) from information_schema.tables WHERE table_schema = '{}'", self.db_name.as_ref())).await?;
let modified_date = _modified_date.trim_end();
Ok(modified_date != "NULL" && u64::from_str(modified_date)? <= dump_date)
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
self.command_runner.run_successfully(
"sh",
args![
"-c",
format!(
"mysqldump '{}' > {}",
self.db_name.as_ref(),
self.storage.write_filename().to_str().unwrap()
),
],
)
async fn execute(&self) -> Result<(), Box<dyn Error>> {
self
.command_runner
.run_successfully(
"sh",
args![
"-c",
format!(
"mysqldump '{}' > {}",
self.db_name.as_ref(),
self.storage.write_filename().to_str().unwrap()
),
],
)
.await
}
}