tape: use worker task for eject-media api

This commit is contained in:
Dietmar Maurer 2021-01-28 16:46:28 +01:00
parent eb1dfb02b5
commit 41dacd5d3d
3 changed files with 44 additions and 21 deletions

View File

@ -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<Value, Error> {
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",

View File

@ -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(&param);
let (config, _digest) = config::drive::config()?;
param["drive"] = lookup_drive_name(&param, &config)?.into();
let drive = lookup_drive_name(&param, &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(())
}

View File

@ -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')],