From 41dacd5d3dcf8b07985526d56bf28e8fefc846a7 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 28 Jan 2021 16:46:28 +0100 Subject: [PATCH] tape: use worker task for eject-media api --- src/api2/tape/drive.rs | 41 ++++++++++++++++++++++++++++++----------- src/bin/proxmox-tape.rs | 23 +++++++++++++---------- www/Utils.js | 1 + 3 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/api2/tape/drive.rs b/src/api2/tape/drive.rs index 1e792f58..4b2377b5 100644 --- a/src/api2/tape/drive.rs +++ b/src/api2/tape/drive.rs @@ -296,21 +296,40 @@ pub fn rewind( }, }, }, + returns: { + schema: UPID_SCHEMA, + }, )] /// Eject/Unload drive media -pub async fn eject_media(drive: String) -> Result<(), Error> { +pub fn eject_media( + drive: String, + rpcenv: &mut dyn RpcEnvironment, +) -> Result { let (config, _digest) = config::drive::config()?; - tokio::task::spawn_blocking(move || { - if let Some((mut changer, _)) = media_changer(&config, &drive)? { - changer.unload_media(None)?; - } else { - let mut drive = open_drive(&config, &drive)?; - drive.eject_media()?; - } - Ok(()) - }).await? + check_drive_exists(&config, &drive)?; // early check before starting worker + + let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?; + + let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI; + + let upid_str = WorkerTask::new_thread( + "eject-media", + Some(drive.clone()), + auth_id, + to_stdout, + move |_worker| { + if let Some((mut changer, _)) = media_changer(&config, &drive)? { + changer.unload_media(None)?; + } else { + let mut drive = open_drive(&config, &drive)?; + drive.eject_media()?; + } + Ok(()) + })?; + + Ok(upid_str.into()) } #[api( @@ -1176,7 +1195,7 @@ pub const SUBDIRS: SubdirMap = &sorted!([ ( "eject-media", &Router::new() - .put(&API_METHOD_EJECT_MEDIA) + .post(&API_METHOD_EJECT_MEDIA) ), ( "erase-media", diff --git a/src/bin/proxmox-tape.rs b/src/bin/proxmox-tape.rs index 76658e67..49c34cff 100644 --- a/src/bin/proxmox-tape.rs +++ b/src/bin/proxmox-tape.rs @@ -204,25 +204,28 @@ async fn rewind(param: Value) -> Result<(), Error> { schema: DRIVE_NAME_SCHEMA, optional: true, }, + "output-format": { + schema: OUTPUT_FORMAT, + optional: true, + }, }, }, )] /// Eject/Unload drive media -async fn eject_media( - mut param: Value, - rpcenv: &mut dyn RpcEnvironment, -) -> Result<(), Error> { +async fn eject_media(param: Value) -> Result<(), Error> { + + let output_format = get_output_format(¶m); let (config, _digest) = config::drive::config()?; - param["drive"] = lookup_drive_name(¶m, &config)?.into(); + let drive = lookup_drive_name(¶m, &config)?; - let info = &api2::tape::drive::API_METHOD_EJECT_MEDIA; + let mut client = connect_to_localhost()?; - match info.handler { - ApiHandler::Async(handler) => (handler)(param, info, rpcenv).await?, - _ => unreachable!(), - }; + let path = format!("api2/json/tape/drive/{}/eject-media", drive); + let result = client.post(&path, Some(param)).await?; + + view_task_result(client, result, &output_format).await?; Ok(()) } diff --git a/www/Utils.js b/www/Utils.js index 14b58a25..e6ea64ea 100644 --- a/www/Utils.js +++ b/www/Utils.js @@ -279,6 +279,7 @@ Ext.define('PBS.Utils', { "barcode-label-media": [gettext('Drive'), gettext('Barcode label media')], dircreate: [gettext('Directory Storage'), gettext('Create')], dirremove: [gettext('Directory'), gettext('Remove')], + "eject-media": [gettext('Drive'), gettext('Eject media')], "erase-media": [gettext('Drive'), gettext('Erase media')], garbage_collection: ['Datastore', gettext('Garbage collect')], "inventory-update": [gettext('Drive'), gettext('Inventory update')],