diff --git a/Cargo.lock b/Cargo.lock index 19caf01..f5175d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -605,7 +605,7 @@ dependencies = [ [[package]] name = "markov" -version = "0.3.1" +version = "0.3.2" dependencies = [ "cfg-if 1.0.0", "futures", diff --git a/Cargo.toml b/Cargo.toml index 71e6e0a..a16c3bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "markov" -version = "0.3.1" +version = "0.3.2" description = "Generate string of text from Markov chain fed by stdin" authors = ["Avril "] edition = "2018" diff --git a/Makefile b/Makefile index b2f0cfb..ccf8d3d 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ markov: strip target/release/markov install: - rc-service markov stop + -rc-service markov shutdown && sleep 0.6 cp -f target/release/markov /usr/local/bin/markov rc-service markov start diff --git a/src/save.rs b/src/save.rs index a824774..ec09cf8 100644 --- a/src/save.rs +++ b/src/save.rs @@ -45,16 +45,19 @@ pub async fn save_now(state: &State) -> io::Result<()> async fn save_now_to(chain: &Chain, to: impl AsRef) -> io::Result<()> { debug!("Saving chain to {:?}", to.as_ref()); - let file = OpenOptions::new() + let mut file = OpenOptions::new() .write(true) .create(true) .truncate(true) .open(to).await?; let chain = serde_cbor::to_vec(chain).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; - - let mut file = AsyncWriteCompressor::new(file, PreferencesBuilder::new() - .compression_level(lz4f::CLEVEL_HIGH).build())?; - file.write_all(&chain[..]).await?; + { + let mut file = AsyncWriteCompressor::new(&mut file, PreferencesBuilder::new() + .compression_level(lz4f::CLEVEL_HIGH).build())?; + file.write_all(&chain[..]).await?; + file.flush().await?; + file.shutdown().await?; + } file.flush().await?; file.shutdown().await?; Ok(()) @@ -94,6 +97,7 @@ pub async fn load(from: impl AsRef) -> io::Result> let mut whole = Vec::new(); let mut file = AsyncReadDecompressor::new(file)?; tokio::io::copy(&mut file, &mut whole).await?; + whole.flush().await?; serde_cbor::from_slice(&whole[..]) .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e)) }