tape: run label commands as WorkerTask (threads)

This commit is contained in:
Dietmar Maurer 2020-12-11 09:10:22 +01:00
parent bff7e3f3e4
commit 6dbad5b4b5
3 changed files with 110 additions and 33 deletions

View File

@ -1,4 +1,6 @@
use std::path::Path; use std::path::Path;
use std::sync::Arc;
use anyhow::{bail, Error}; use anyhow::{bail, Error};
use serde_json::Value; use serde_json::Value;
@ -10,6 +12,7 @@ use proxmox::{
sys::error::SysError, sys::error::SysError,
api::{ api::{
api, api,
RpcEnvironment,
Router, Router,
SubdirMap, SubdirMap,
}, },
@ -18,15 +21,18 @@ use proxmox::{
use crate::{ use crate::{
config, config,
api2::types::{ api2::types::{
UPID_SCHEMA,
DRIVE_ID_SCHEMA, DRIVE_ID_SCHEMA,
MEDIA_LABEL_SCHEMA, MEDIA_LABEL_SCHEMA,
MEDIA_POOL_NAME_SCHEMA, MEDIA_POOL_NAME_SCHEMA,
Authid,
LinuxTapeDrive, LinuxTapeDrive,
ScsiTapeChanger, ScsiTapeChanger,
TapeDeviceInfo, TapeDeviceInfo,
MediaLabelInfoFlat, MediaLabelInfoFlat,
LabelUuidMap, LabelUuidMap,
}, },
server::WorkerTask,
tape::{ tape::{
TAPE_STATUS_DIR, TAPE_STATUS_DIR,
TapeDriver, TapeDriver,
@ -255,6 +261,9 @@ pub fn eject_media(drive: String) -> Result<(), Error> {
}, },
}, },
}, },
returns: {
schema: UPID_SCHEMA,
},
)] )]
/// Label media /// Label media
/// ///
@ -266,7 +275,10 @@ pub fn label_media(
drive: String, drive: String,
pool: Option<String>, pool: Option<String>,
changer_id: String, changer_id: String,
) -> Result<(), Error> { rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
if let Some(ref pool) = pool { if let Some(ref pool) = pool {
let (pool_config, _digest) = config::media_pool::config()?; let (pool_config, _digest) = config::media_pool::config()?;
@ -278,6 +290,13 @@ pub fn label_media(
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
let upid_str = WorkerTask::new_thread(
"label-media",
Some(drive.clone()),
auth_id,
true,
move |worker| {
let mut drive = open_drive(&config, &drive)?; let mut drive = open_drive(&config, &drive)?;
drive.rewind()?; drive.rewind()?;
@ -301,10 +320,15 @@ pub fn label_media(
ctime, ctime,
}; };
write_media_label(&mut drive, label, pool) write_media_label(worker, &mut drive, label, pool)
}
)?;
Ok(upid_str.into())
} }
fn write_media_label( fn write_media_label(
worker: Arc<WorkerTask>,
drive: &mut Box<dyn TapeDriver>, drive: &mut Box<dyn TapeDriver>,
label: DriveLabel, label: DriveLabel,
pool: Option<String>, pool: Option<String>,
@ -316,13 +340,13 @@ fn write_media_label(
if let Some(ref pool) = pool { if let Some(ref pool) = pool {
// assign media to pool by writing special media set label // assign media to pool by writing special media set label
println!("Label media '{}' for pool '{}'", label.changer_id, pool); worker.log(format!("Label media '{}' for pool '{}'", label.changer_id, pool));
let set = MediaSetLabel::with_data(&pool, [0u8; 16].into(), 0, label.ctime); let set = MediaSetLabel::with_data(&pool, [0u8; 16].into(), 0, label.ctime);
drive.write_media_set_label(&set)?; drive.write_media_set_label(&set)?;
media_set_label = Some(set); media_set_label = Some(set);
} else { } else {
println!("Label media '{}' (no pool assignment)", label.changer_id); worker.log(format!("Label media '{}' (no pool assignment)", label.changer_id));
} }
let media_id = MediaId { label, media_set_label }; let media_id = MediaId { label, media_set_label };
@ -534,12 +558,16 @@ pub fn inventory(
}, },
}, },
}, },
returns: {
schema: UPID_SCHEMA,
},
)] )]
/// Label media with barcodes from changer device /// Label media with barcodes from changer device
pub fn barcode_label_media( pub fn barcode_label_media(
drive: String, drive: String,
pool: Option<String>, pool: Option<String>,
) -> Result<(), Error> { rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
if let Some(ref pool) = pool { if let Some(ref pool) = pool {
let (pool_config, _digest) = config::media_pool::config()?; let (pool_config, _digest) = config::media_pool::config()?;
@ -549,6 +577,27 @@ pub fn barcode_label_media(
} }
} }
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let upid_str = WorkerTask::new_thread(
"barcode-label-media",
Some(drive.clone()),
auth_id,
true,
move |worker| {
barcode_label_media_worker(worker, drive, pool)
}
)?;
Ok(upid_str.into())
}
fn barcode_label_media_worker(
worker: Arc<WorkerTask>,
drive: String,
pool: Option<String>,
) -> Result<(), Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
let (mut changer, changer_name) = media_changer(&config, &drive, false)?; let (mut changer, changer_name) = media_changer(&config, &drive, false)?;
@ -571,14 +620,14 @@ pub fn barcode_label_media(
inventory.reload()?; inventory.reload()?;
if inventory.find_media_by_changer_id(&changer_id).is_some() { if inventory.find_media_by_changer_id(&changer_id).is_some() {
println!("media '{}' already inventoried (already labeled)", changer_id); worker.log(format!("media '{}' already inventoried (already labeled)", changer_id));
continue; continue;
} }
println!("checking/loading media '{}'", changer_id); worker.log(format!("checking/loading media '{}'", changer_id));
if let Err(err) = changer.load_media(&changer_id) { if let Err(err) = changer.load_media(&changer_id) {
eprintln!("unable to load media '{}' - {}", changer_id, err); worker.warn(format!("unable to load media '{}' - {}", changer_id, err));
continue; continue;
} }
@ -587,7 +636,7 @@ pub fn barcode_label_media(
match drive.read_next_file() { match drive.read_next_file() {
Ok(Some(_file)) => { Ok(Some(_file)) => {
println!("media '{}' is not empty (erase first)", changer_id); worker.log(format!("media '{}' is not empty (erase first)", changer_id));
continue; continue;
} }
Ok(None) => { /* EOF mark at BOT, assume tape is empty */ }, Ok(None) => { /* EOF mark at BOT, assume tape is empty */ },
@ -595,7 +644,7 @@ pub fn barcode_label_media(
if err.is_errno(nix::errno::Errno::ENOSPC) || err.is_errno(nix::errno::Errno::EIO) { if err.is_errno(nix::errno::Errno::ENOSPC) || err.is_errno(nix::errno::Errno::EIO) {
/* assume tape is empty */ /* assume tape is empty */
} else { } else {
println!("media '{}' read error (maybe not empty - erase first)", changer_id); worker.warn(format!("media '{}' read error (maybe not empty - erase first)", changer_id));
continue; continue;
} }
} }
@ -608,7 +657,7 @@ pub fn barcode_label_media(
ctime, ctime,
}; };
write_media_label(&mut drive, label, pool.clone())? write_media_label(worker.clone(), &mut drive, label, pool.clone())?
} }
Ok(()) Ok(())

View File

@ -13,6 +13,10 @@ use proxmox::{
use proxmox_backup::{ use proxmox_backup::{
tools::format::render_epoch, tools::format::render_epoch,
server::{
UPID,
worker_is_active_local,
},
api2::{ api2::{
self, self,
types::{ types::{
@ -34,6 +38,24 @@ use proxmox_backup::{
mod proxmox_tape; mod proxmox_tape;
use proxmox_tape::*; use proxmox_tape::*;
// Note: local workers should print logs to stdout, so there is no need
// to fetch/display logs. We just wait for the worker to finish.
pub async fn wait_for_local_worker(upid_str: &str) -> Result<(), Error> {
let upid: UPID = upid_str.parse()?;
let sleep_duration = core::time::Duration::new(0, 100_000_000);
loop {
if worker_is_active_local(&upid) {
tokio::time::delay_for(sleep_duration).await;
} else {
break;
}
}
Ok(())
}
fn lookup_drive_name( fn lookup_drive_name(
param: &Value, param: &Value,
config: &SectionConfigData, config: &SectionConfigData,
@ -211,7 +233,7 @@ fn load_media(
}, },
)] )]
/// Label media /// Label media
fn label_media( async fn label_media(
mut param: Value, mut param: Value,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -222,11 +244,13 @@ fn label_media(
let info = &api2::tape::drive::API_METHOD_LABEL_MEDIA; let info = &api2::tape::drive::API_METHOD_LABEL_MEDIA;
match info.handler { let result = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(), _ => unreachable!(),
}; };
wait_for_local_worker(result.as_str().unwrap()).await?;
Ok(()) Ok(())
} }
@ -341,7 +365,7 @@ fn inventory(
}, },
)] )]
/// Label media with barcodes from changer device /// Label media with barcodes from changer device
fn barcode_label_media( async fn barcode_label_media(
mut param: Value, mut param: Value,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> { ) -> Result<(), Error> {
@ -352,11 +376,13 @@ fn barcode_label_media(
let info = &api2::tape::drive::API_METHOD_BARCODE_LABEL_MEDIA; let info = &api2::tape::drive::API_METHOD_BARCODE_LABEL_MEDIA;
match info.handler { let result = match info.handler {
ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?, ApiHandler::Sync(handler) => (handler)(param, info, rpcenv)?,
_ => unreachable!(), _ => unreachable!(),
}; };
wait_for_local_worker(result.as_str().unwrap()).await?;
Ok(()) Ok(())
} }

View File

@ -274,9 +274,11 @@ Ext.define('PBS.Utils', {
// do whatever you want here // do whatever you want here
Proxmox.Utils.override_task_descriptions({ Proxmox.Utils.override_task_descriptions({
backup: (type, id) => PBS.Utils.render_datastore_worker_id(id, gettext('Backup')), backup: (type, id) => PBS.Utils.render_datastore_worker_id(id, gettext('Backup')),
"barcode-label-media": [gettext('Drive'), gettext('Barcode label media')],
dircreate: [gettext('Directory Storage'), gettext('Create')], dircreate: [gettext('Directory Storage'), gettext('Create')],
dirremove: [gettext('Directory'), gettext('Remove')], dirremove: [gettext('Directory'), gettext('Remove')],
garbage_collection: ['Datastore', gettext('Garbage collect')], garbage_collection: ['Datastore', gettext('Garbage collect')],
"label-media": [gettext('Drive'), gettext('Label media')],
logrotate: [null, gettext('Log Rotation')], logrotate: [null, gettext('Log Rotation')],
prune: (type, id) => PBS.Utils.render_datastore_worker_id(id, gettext('Prune')), prune: (type, id) => PBS.Utils.render_datastore_worker_id(id, gettext('Prune')),
reader: (type, id) => PBS.Utils.render_datastore_worker_id(id, gettext('Read objects')), reader: (type, id) => PBS.Utils.render_datastore_worker_id(id, gettext('Read objects')),