cleanup WorkerTaskContext

This commit is contained in:
Dietmar Maurer 2021-09-24 11:39:30 +02:00
parent 1ec0d70d09
commit 619cd5cbcb
5 changed files with 25 additions and 23 deletions

View File

@ -1,9 +1,20 @@
use anyhow::Error;
use anyhow::{bail, Error};
/// `WorkerTask` methods commonly used from contexts otherwise not related to the API server.
/// Worker task abstraction
///
/// A worker task is a long running task, which usually logs output into a separate file.
pub trait WorkerTaskContext {
/// 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>;
fn check_abort(&self) -> Result<(), Error> {
if self.abort_requested() {
bail!("abort requested - aborting task");
}
Ok(())
}
/// Create a log message for this task.
fn log(&self, level: log::Level, message: &std::fmt::Arguments);
@ -11,6 +22,10 @@ pub trait WorkerTaskContext {
/// 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)
}

View File

@ -829,19 +829,6 @@ impl WorkerTask {
}
}
/// Test if abort was requested.
pub fn abort_requested(&self) -> bool {
self.abort_requested.load(Ordering::SeqCst)
}
/// Fail if abort was requested.
pub fn check_abort(&self) -> Result<(), Error> {
if self.abort_requested() {
bail!("abort requested - aborting task");
}
Ok(())
}
/// Get a future which resolves on task abort
pub fn abort_future(&self) -> oneshot::Receiver<()> {
let (tx, rx) = oneshot::channel::<()>();
@ -861,8 +848,9 @@ impl WorkerTask {
}
impl WorkerTaskContext for WorkerTask {
fn check_abort(&self) -> Result<(), Error> {
self.check_abort()
fn abort_requested(&self) -> bool {
self.abort_requested.load(Ordering::SeqCst)
}
fn log(&self, level: log::Level, message: &std::fmt::Arguments) {

View File

@ -22,7 +22,7 @@ use pbs_api_types::{
use pbs_datastore::StoreProgress;
use pbs_datastore::backup_info::{BackupDir, BackupInfo};
use pbs_tools::{task_log, task_warn};
use pbs_tools::{task_log, task_warn, task::WorkerTaskContext};
use pbs_config::CachedUserInfo;
use proxmox_rest_server::WorkerTask;

View File

@ -45,7 +45,7 @@ use pbs_tape::{
TapeRead, BlockReadError, MediaContentHeader,
PROXMOX_BACKUP_CONTENT_HEADER_MAGIC_1_0,
};
use pbs_tools::{task_log, task_warn};
use pbs_tools::{task_log, task_warn, task::WorkerTaskContext};
use proxmox_rest_server::WorkerTask;
use crate::{

View File

@ -30,13 +30,12 @@ use proxmox::{
use pbs_api_types::{VirtualTapeDrive, LtoTapeDrive, Fingerprint};
use pbs_config::key_config::KeyConfig;
use pbs_tools::task_log;
use pbs_tools::{task_log, task::WorkerTaskContext};
use pbs_tape::{
TapeWrite, TapeRead, BlockReadError, MediaContentHeader,
sg_tape::TapeAlertFlags,
};
use proxmox_rest_server::WorkerTask;
use crate::{
server::send_load_media_email,
@ -355,7 +354,7 @@ impl std::fmt::Display for TapeRequestError {
///
/// Returns a handle to the opened drive and the media labels.
pub fn request_and_load_media(
worker: &WorkerTask,
worker: &dyn WorkerTaskContext,
config: &SectionConfigData,
drive: &str,
label: &MediaLabel,