proxmox-sys: imported pbs-tools/src/task.rs (as worker_task_context.rs)

Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
Dietmar Maurer 2021-11-19 09:27:23 +01:00
parent 202641757e
commit 20661f014d
4 changed files with 98 additions and 2 deletions

View File

@ -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" ] }

View File

@ -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 <support@proxmox.com> Fri, 19 Nov 2021 07:19:19 +0100
-- Proxmox Support Team <support@proxmox.com> Fri, 19 Nov 2021 09:44:01 +0100

View File

@ -1,3 +1,4 @@
pub mod crypt;
pub mod logrotate;
pub mod process_locker;
pub mod worker_task_context;

View File

@ -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<T: WorkerTaskContext + ?Sized> WorkerTaskContext for std::sync::Arc<T> {
fn abort_requested(&self) -> bool {
<T as WorkerTaskContext>::abort_requested(&*self)
}
fn check_abort(&self) -> Result<(), Error> {
<T as WorkerTaskContext>::check_abort(&*self)
}
fn shutdown_requested(&self) -> bool {
<T as WorkerTaskContext>::shutdown_requested(&*self)
}
fn fail_on_shutdown(&self) -> Result<(), Error> {
<T as WorkerTaskContext>::fail_on_shutdown(&*self)
}
fn log(&self, level: log::Level, message: &std::fmt::Arguments) {
<T as WorkerTaskContext>::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)+))
}};
}