use tokios async fs api instead of blocking fs

This commit is contained in:
2020-12-19 15:55:42 +01:00
parent acb0c5a0e9
commit 29a4ca3220
3 changed files with 26 additions and 14 deletions

8
Cargo.lock generated
View File

@@ -86,6 +86,12 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "bytes"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e0dcbc35f504eb6fc275a6d20e4ebcda18cf50d40ba6fabff8c711fa16cb3b16"
[[package]] [[package]]
name = "cassowary" name = "cassowary"
version = "0.3.0" version = "0.3.0"
@@ -945,6 +951,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "720ba21c25078711bf456d607987d95bce90f7c3bea5abe1db587862e7a1e87c" checksum = "720ba21c25078711bf456d607987d95bce90f7c3bea5abe1db587862e7a1e87c"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"bytes",
"memchr",
"pin-project-lite", "pin-project-lite",
"slab", "slab",
"tokio-macros", "tokio-macros",

View File

@@ -26,4 +26,4 @@ tui = { version = "0.13", default-features = false, features = ["crossterm"] }
[dependencies.tokio] [dependencies.tokio]
version = "0.3" version = "0.3"
default-features = false default-features = false
features = ["sync", "rt", "macros", "time"] features = ["sync", "rt", "macros", "time", "fs", "io-util"]

View File

@@ -12,13 +12,12 @@ use std::{
error::Error as StdError, error::Error as StdError,
ffi, fmt, ffi, fmt,
fmt::Write as FmtWrite, fmt::Write as FmtWrite,
io::Write as IoWrite,
path::{Path, PathBuf}, path::{Path, PathBuf},
result::Result as StdResult, result::Result as StdResult,
sync::Arc, sync::Arc,
time::Duration, time::Duration,
}; };
use tokio::{task, time::interval}; use tokio::{fs, io::AsyncWriteExt, task, time::interval};
#[derive(Clone, Debug, GBoxed)] #[derive(Clone, Debug, GBoxed)]
#[gboxed(type_name = "GBoxErrorWrapper")] #[gboxed(type_name = "GBoxErrorWrapper")]
@@ -189,10 +188,11 @@ async fn main_loop(ui_queue: ui::MsgQueue) -> Result<()> {
args.rel_from_path.display() args.rel_from_path.display()
)); ));
let mut log_file = match std::fs::OpenOptions::new() let mut log_file = match fs::OpenOptions::new()
.create(true) .create(true)
.append(true) .append(true)
.open(log_path) .open(log_path)
.await
{ {
Ok(log_file) => log_file, Ok(log_file) => log_file,
Err(fs_err) => { Err(fs_err) => {
@@ -204,12 +204,15 @@ async fn main_loop(ui_queue: ui::MsgQueue) -> Result<()> {
let mut err_str = String::new(); let mut err_str = String::new();
write!(&mut err_str, "{:?}\n", err).context("TODO")?; write!(&mut err_str, "{:?}\n", err).context("TODO")?;
log_file.write_all(err_str.as_ref()).map_err(|fs_err| { log_file
.write_all(err_str.as_ref())
.map_err(|fs_err| {
err.context(format!( err.context(format!(
"Unable to write transcoding error to log file (fs error: {})", "Unable to write transcoding error to log file (fs error: {})",
fs_err fs_err
)) ))
})?; })
.await?;
msg_queue.push(ui::Msg::TaskError { id: i }); msg_queue.push(ui::Msg::TaskError { id: i });
} }
@@ -362,11 +365,12 @@ async fn transcode(
let bus = pipeline.get_bus().context("pipe get bus")?; let bus = pipeline.get_bus().context("pipe get bus")?;
std::fs::create_dir_all( fs::create_dir_all(
to_path to_path
.parent() .parent()
.with_context(|| format!("could not get parent dir for {}", to_path.display()))?, .with_context(|| format!("could not get parent dir for {}", to_path.display()))?,
)?; )
.await?;
rm_file_on_err(&to_path_tmp, async { rm_file_on_err(&to_path_tmp, async {
pipeline pipeline
@@ -475,7 +479,7 @@ async fn transcode(
.set_state(gstreamer::State::Null) .set_state(gstreamer::State::Null)
.context("Unable to set the pipeline to the `Null` state")?; .context("Unable to set the pipeline to the `Null` state")?;
std::fs::rename(&to_path_tmp, &to_path)?; fs::rename(&to_path_tmp, &to_path).await?;
Ok(()) Ok(())
}) })
@@ -487,7 +491,7 @@ where
F: Future<Output = Result<T>>, F: Future<Output = Result<T>>,
{ {
match f.await { match f.await {
Err(err) => match std::fs::remove_file(path) { Err(err) => match fs::remove_file(path).await {
Ok(..) => Err(err), Ok(..) => Err(err),
Err(fs_err) if fs_err.kind() == std::io::ErrorKind::NotFound => Err(err), Err(fs_err) if fs_err.kind() == std::io::ErrorKind::NotFound => Err(err),
Err(fs_err) => { Err(fs_err) => {