From 20661f014d09415d8484ae25d6ca9aa7d0e64ffb Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Fri, 19 Nov 2021 09:27:23 +0100 Subject: [PATCH] proxmox-sys: imported pbs-tools/src/task.rs (as worker_task_context.rs) Signed-off-by: Dietmar Maurer --- proxmox-sys/Cargo.toml | 1 + proxmox-sys/debian/changelog | 6 +- proxmox-sys/src/lib.rs | 1 + proxmox-sys/src/worker_task_context.rs | 92 ++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 proxmox-sys/src/worker_task_context.rs diff --git a/proxmox-sys/Cargo.toml b/proxmox-sys/Cargo.toml index 11b130a7..433e13dd 100644 --- a/proxmox-sys/Cargo.toml +++ b/proxmox-sys/Cargo.toml @@ -12,6 +12,7 @@ exclude = [ "debian" ] anyhow = "1.0" base64 = "0.12" libc = "0.2.107" +log = "0.4" nix = "0.19.1" zstd = { version = "0.6", features = [ "bindgen" ] } diff --git a/proxmox-sys/debian/changelog b/proxmox-sys/debian/changelog index 39a719df..487b2259 100644 --- a/proxmox-sys/debian/changelog +++ b/proxmox-sys/debian/changelog @@ -1,4 +1,4 @@ -rust-proxmox-sys (0.1.0) unstable; urgency=medium +rust-proxmox-sys (0.1.0) stable; urgency=medium * imported pbs-tools/src/crypt.rs @@ -6,7 +6,9 @@ rust-proxmox-sys (0.1.0) unstable; urgency=medium * imported pbs-tools/src/logrotate.rs (with various cleanups) + * imported pbs-tools/src/task.rs (as worker_task_context.rs) + * initial release - -- Proxmox Support Team Fri, 19 Nov 2021 07:19:19 +0100 + -- Proxmox Support Team Fri, 19 Nov 2021 09:44:01 +0100 diff --git a/proxmox-sys/src/lib.rs b/proxmox-sys/src/lib.rs index 057c22cf..20333849 100644 --- a/proxmox-sys/src/lib.rs +++ b/proxmox-sys/src/lib.rs @@ -1,3 +1,4 @@ pub mod crypt; pub mod logrotate; pub mod process_locker; +pub mod worker_task_context; diff --git a/proxmox-sys/src/worker_task_context.rs b/proxmox-sys/src/worker_task_context.rs new file mode 100644 index 00000000..0c2d05aa --- /dev/null +++ b/proxmox-sys/src/worker_task_context.rs @@ -0,0 +1,92 @@ +use anyhow::{bail, Error}; + +/// Worker task abstraction +/// +/// A worker task is a long running task, which usually logs output into a separate file. +pub trait WorkerTaskContext: Send + Sync { + + /// Test if there was a request to abort the task. + fn abort_requested(&self) -> bool; + + /// If the task should be aborted, this should fail with a reasonable error message. + fn check_abort(&self) -> Result<(), Error> { + if self.abort_requested() { + bail!("abort requested - aborting task"); + } + Ok(()) + } + + /// Test if there was a request to shutdown the server. + fn shutdown_requested(&self) -> bool; + + + /// This should fail with a reasonable error message if there was + /// a request to shutdown the server. + fn fail_on_shutdown(&self) -> Result<(), Error> { + if self.shutdown_requested() { + bail!("Server shutdown requested - aborting task"); + } + Ok(()) + } + + /// Create a log message for this task. + fn log(&self, level: log::Level, message: &std::fmt::Arguments); +} + +/// Convenience implementation: +impl WorkerTaskContext for std::sync::Arc { + fn abort_requested(&self) -> bool { + ::abort_requested(&*self) + } + + fn check_abort(&self) -> Result<(), Error> { + ::check_abort(&*self) + } + + fn shutdown_requested(&self) -> bool { + ::shutdown_requested(&*self) + } + + fn fail_on_shutdown(&self) -> Result<(), Error> { + ::fail_on_shutdown(&*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::worker_task_context::WorkerTaskContext::log(&*$task, log::Level::Error, &format_args!($($fmt)+)) + }}; +} + +#[macro_export] +macro_rules! task_warn { + ($task:expr, $($fmt:tt)+) => {{ + $crate::worker_task_context::WorkerTaskContext::log(&*$task, log::Level::Warn, &format_args!($($fmt)+)) + }}; +} + +#[macro_export] +macro_rules! task_log { + ($task:expr, $($fmt:tt)+) => {{ + $crate::worker_task_context::WorkerTaskContext::log(&*$task, log::Level::Info, &format_args!($($fmt)+)) + }}; +} + +#[macro_export] +macro_rules! task_debug { + ($task:expr, $($fmt:tt)+) => {{ + $crate::worker_task_context::WorkerTaskContext::log(&*$task, log::Level::Debug, &format_args!($($fmt)+)) + }}; +} + +#[macro_export] +macro_rules! task_trace { + ($task:expr, $($fmt:tt)+) => {{ + $crate::worker_task_context::WorkerTaskContext::log(&*$task, log::Level::Trace, &format_args!($($fmt)+)) + }}; +}