From d1993187b6256cc33728b0ef8119e71ee2539a9d Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Mon, 12 Oct 2020 11:28:03 +0200 Subject: [PATCH] introduce TaskState trait Used to not require access to the WorkerTask struct outside the `server` and `api2` module, so it'll be easier to separate those backup/server/client parts into separate crates. Signed-off-by: Wolfgang Bumiller --- src/lib.rs | 2 ++ src/server/worker_task.rs | 16 +++++++++++ src/task.rs | 56 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/task.rs diff --git a/src/lib.rs b/src/lib.rs index 4125e32e..c44c9ae1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +pub mod task; + #[macro_use] pub mod buildcfg; diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs index 409e7b49..bd19782c 100644 --- a/src/server/worker_task.rs +++ b/src/server/worker_task.rs @@ -851,3 +851,19 @@ impl WorkerTask { &self.upid } } + +impl crate::task::TaskState for WorkerTask { + fn check_abort(&self) -> Result<(), Error> { + self.fail_on_abort() + } + + fn log(&self, level: log::Level, message: &std::fmt::Arguments) { + match level { + log::Level::Error => self.warn(&message.to_string()), + log::Level::Warn => self.warn(&message.to_string()), + log::Level::Info => self.log(&message.to_string()), + log::Level::Debug => self.log(&format!("DEBUG: {}", message)), + log::Level::Trace => self.log(&format!("TRACE: {}", message)), + } + } +} diff --git a/src/task.rs b/src/task.rs new file mode 100644 index 00000000..8cfd6fe8 --- /dev/null +++ b/src/task.rs @@ -0,0 +1,56 @@ +use anyhow::Error; + +/// `WorkerTask` methods commonly used from contexts otherwise not related to the API server. +pub trait TaskState { + /// If the task should be aborted, this should fail with a reasonable error message. + fn check_abort(&self) -> Result<(), Error>; + + /// Create a log message for this task. + fn log(&self, level: log::Level, message: &std::fmt::Arguments); +} + +/// Convenience implementation: +impl TaskState for std::sync::Arc { + fn check_abort(&self) -> Result<(), Error> { + ::check_abort(&*self) + } + + fn log(&self, level: log::Level, message: &std::fmt::Arguments) { + ::log(&*self, level, message) + } +} + +#[macro_export] +macro_rules! task_error { + ($task:expr, $($fmt:tt)+) => {{ + $crate::task::TaskState::log(&*$task, log::Level::Error, &format_args!($($fmt)+)) + }}; +} + +#[macro_export] +macro_rules! task_warn { + ($task:expr, $($fmt:tt)+) => {{ + $crate::task::TaskState::log(&*$task, log::Level::Warn, &format_args!($($fmt)+)) + }}; +} + +#[macro_export] +macro_rules! task_log { + ($task:expr, $($fmt:tt)+) => {{ + $crate::task::TaskState::log(&*$task, log::Level::Info, &format_args!($($fmt)+)) + }}; +} + +#[macro_export] +macro_rules! task_debug { + ($task:expr, $($fmt:tt)+) => {{ + $crate::task::TaskState::log(&*$task, log::Level::Debug, &format_args!($($fmt)+)) + }}; +} + +#[macro_export] +macro_rules! task_trace { + ($task:expr, $($fmt:tt)+) => {{ + $crate::task::TaskState::log(&*$task, log::Level::Trace, &format_args!($($fmt)+)) + }}; +}