tape: use worker tasks for media load/unload

This commit is contained in:
Dietmar Maurer 2021-02-18 09:04:51 +01:00
parent bbe06f97be
commit d0647e5a02
2 changed files with 79 additions and 18 deletions

View File

@ -78,21 +78,47 @@ use crate::{
}, },
}, },
}, },
returns: {
schema: UPID_SCHEMA,
},
)] )]
/// Load media with specified label /// Load media with specified label
/// ///
/// Issue a media load request to the associated changer device. /// Issue a media load request to the associated changer device.
pub async fn load_media(drive: String, label_text: String) -> Result<(), Error> { pub fn load_media(
drive: String,
label_text: String,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
// early check/lock before starting worker
let lock_guard = lock_tape_device(&config, &drive)?; let lock_guard = lock_tape_device(&config, &drive)?;
tokio::task::spawn_blocking(move || { let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let _lock_guard = lock_guard; // keep lock guard
let (mut changer, _) = required_media_changer(&config, &drive)?; let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
changer.load_media(&label_text)
}).await? let job_id = format!("{}:{}", drive, label_text);
let upid_str = WorkerTask::new_thread(
"load-media",
Some(job_id),
auth_id,
to_stdout,
move |worker| {
let _lock_guard = lock_guard; // keep lock guard
task_log!(worker, "loading media '{}' into drive '{}'", label_text, drive);
let (mut changer, _) = required_media_changer(&config, &drive)?;
changer.load_media(&label_text)?;
Ok(())
}
)?;
Ok(upid_str.into())
} }
#[api( #[api(
@ -171,23 +197,42 @@ pub async fn export_media(drive: String, label_text: String) -> Result<u64, Erro
}, },
}, },
}, },
returns: {
schema: UPID_SCHEMA,
},
)] )]
/// Unload media via changer /// Unload media via changer
pub async fn unload( pub fn unload(
drive: String, drive: String,
target_slot: Option<u64>, target_slot: Option<u64>,
_param: Value, rpcenv: &mut dyn RpcEnvironment,
) -> Result<(), Error> { ) -> Result<Value, Error> {
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
// early check/lock before starting worker
let lock_guard = lock_tape_device(&config, &drive)?; let lock_guard = lock_tape_device(&config, &drive)?;
tokio::task::spawn_blocking(move || { let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let _lock_guard = lock_guard; // keep lock guard
let (mut changer, _) = required_media_changer(&config, &drive)?; let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
changer.unload_media(target_slot)
}).await? let upid_str = WorkerTask::new_thread(
"unload-media",
Some(drive.clone()),
auth_id,
to_stdout,
move |worker| {
let _lock_guard = lock_guard; // keep lock guard
task_log!(worker, "unloading media from drive '{}'", drive);
let (mut changer, _) = required_media_changer(&config, &drive)?;
changer.unload_media(target_slot)?;
Ok(())
}
)?;
Ok(upid_str.into())
} }
#[api( #[api(
@ -1297,7 +1342,7 @@ pub const SUBDIRS: SubdirMap = &sorted!([
( (
"load-media", "load-media",
&Router::new() &Router::new()
.put(&API_METHOD_LOAD_MEDIA) .post(&API_METHOD_LOAD_MEDIA)
), ),
( (
"load-slot", "load-slot",
@ -1332,7 +1377,7 @@ pub const SUBDIRS: SubdirMap = &sorted!([
( (
"unload", "unload",
&Router::new() &Router::new()
.put(&API_METHOD_UNLOAD) .post(&API_METHOD_UNLOAD)
), ),
]); ]);

View File

@ -206,12 +206,18 @@ async fn eject_media(mut param: Value) -> Result<(), Error> {
"label-text": { "label-text": {
schema: MEDIA_LABEL_SCHEMA, schema: MEDIA_LABEL_SCHEMA,
}, },
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}, },
}, },
)] )]
/// Load media with specified label /// Load media with specified label
async fn load_media(mut param: Value) -> Result<(), Error> { async fn load_media(mut param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
let drive = extract_drive_name(&mut param, &config)?; let drive = extract_drive_name(&mut param, &config)?;
@ -219,7 +225,9 @@ async fn load_media(mut param: Value) -> Result<(), Error> {
let mut client = connect_to_localhost()?; let mut client = connect_to_localhost()?;
let path = format!("api2/json/tape/drive/{}/load-media", drive); let path = format!("api2/json/tape/drive/{}/load-media", drive);
client.put(&path, Some(param)).await?; let result = client.post(&path, Some(param)).await?;
view_task_result(&mut client, result, &output_format).await?;
Ok(()) Ok(())
} }
@ -295,12 +303,18 @@ async fn load_media_from_slot(mut param: Value) -> Result<(), Error> {
minimum: 1, minimum: 1,
optional: true, optional: true,
}, },
"output-format": {
schema: OUTPUT_FORMAT,
optional: true,
},
}, },
}, },
)] )]
/// Unload media via changer /// Unload media via changer
async fn unload_media(mut param: Value) -> Result<(), Error> { async fn unload_media(mut param: Value) -> Result<(), Error> {
let output_format = get_output_format(&param);
let (config, _digest) = config::drive::config()?; let (config, _digest) = config::drive::config()?;
let drive = extract_drive_name(&mut param, &config)?; let drive = extract_drive_name(&mut param, &config)?;
@ -308,7 +322,9 @@ async fn unload_media(mut param: Value) -> Result<(), Error> {
let mut client = connect_to_localhost()?; let mut client = connect_to_localhost()?;
let path = format!("api2/json/tape/drive/{}/unload", drive); let path = format!("api2/json/tape/drive/{}/unload", drive);
client.put(&path, Some(param)).await?; let result = client.post(&path, Some(param)).await?;
view_task_result(&mut client, result, &output_format).await?;
Ok(()) Ok(())
} }