api2/tape/drive: use run_drive_blocking_task where possible

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-02-18 15:40:25 +01:00 committed by Dietmar Maurer
parent 54c77b3d62
commit 47a7241410

View File

@ -180,16 +180,15 @@ pub fn load_media(
/// ///
/// Issue a media load request to the associated changer device. /// Issue a media load request to the associated changer device.
pub async fn load_slot(drive: String, source_slot: u64) -> Result<(), Error> { pub async fn load_slot(drive: String, source_slot: u64) -> Result<(), Error> {
run_drive_blocking_task(
let (config, _digest) = config::drive::config()?; drive.clone(),
let lock_guard = lock_tape_device(&config, &drive)?; format!("load from slot {}", source_slot),
move |config| {
tokio::task::spawn_blocking(move || {
let _lock_guard = lock_guard; // keep lock guard
let (mut changer, _) = required_media_changer(&config, &drive)?; let (mut changer, _) = required_media_changer(&config, &drive)?;
changer.load_media_from_slot(source_slot) changer.load_media_from_slot(source_slot)
}).await? },
)
.await
} }
#[api( #[api(
@ -211,19 +210,22 @@ pub async fn load_slot(drive: String, source_slot: u64) -> Result<(), Error> {
)] )]
/// Export media with specified label /// Export media with specified label
pub async fn export_media(drive: String, label_text: String) -> Result<u64, Error> { pub async fn export_media(drive: String, label_text: String) -> Result<u64, Error> {
run_drive_blocking_task(
let (config, _digest) = config::drive::config()?; drive.clone(),
let lock_guard = lock_tape_device(&config, &drive)?; format!("export media {}", label_text),
move |config| {
tokio::task::spawn_blocking(move || {
let _lock_guard = lock_guard; // keep lock guard
let (mut changer, changer_name) = required_media_changer(&config, &drive)?; let (mut changer, changer_name) = required_media_changer(&config, &drive)?;
match changer.export_media(&label_text)? { match changer.export_media(&label_text)? {
Some(slot) => Ok(slot), Some(slot) => Ok(slot),
None => bail!("media '{}' is not online (via changer '{}')", label_text, changer_name), None => bail!(
"media '{}' is not online (via changer '{}')",
label_text,
changer_name
),
} }
}).await? }
)
.await
} }
#[api( #[api(
@ -584,15 +586,10 @@ pub async fn restore_key(
drive: String, drive: String,
password: String, password: String,
) -> Result<(), Error> { ) -> Result<(), Error> {
run_drive_blocking_task(
let (config, _digest) = config::drive::config()?; drive.clone(),
"restore key".to_string(),
// early check/lock before starting worker move |config| {
let lock_guard = lock_tape_device(&config, &drive)?;
tokio::task::spawn_blocking(move || {
let _lock_guard = lock_guard; // keep lock guard
let mut drive = open_drive(&config, &drive)?; let mut drive = open_drive(&config, &drive)?;
let (_media_id, key_config) = drive.read_label()?; let (_media_id, key_config) = drive.read_label()?;
@ -606,7 +603,9 @@ pub async fn restore_key(
} }
Ok(()) Ok(())
}).await? }
)
.await
} }
#[api( #[api(
@ -630,15 +629,10 @@ pub async fn read_label(
drive: String, drive: String,
inventorize: Option<bool>, inventorize: Option<bool>,
) -> Result<MediaIdFlat, Error> { ) -> Result<MediaIdFlat, Error> {
run_drive_blocking_task(
let (config, _digest) = config::drive::config()?; drive.clone(),
"reading label".to_string(),
// early check/lock before starting worker move |config| {
let lock_guard = lock_tape_device(&config, &drive)?;
tokio::task::spawn_blocking(move || {
let _lock_guard = lock_guard; // keep lock guard
let mut drive = open_drive(&config, &drive)?; let mut drive = open_drive(&config, &drive)?;
let (media_id, _key_config) = drive.read_label()?; let (media_id, _key_config) = drive.read_label()?;
@ -688,7 +682,9 @@ pub async fn read_label(
}; };
Ok(media_id) Ok(media_id)
}).await? }
)
.await
} }
#[api( #[api(
@ -755,15 +751,10 @@ pub fn clean_drive(
pub async fn inventory( pub async fn inventory(
drive: String, drive: String,
) -> Result<Vec<LabelUuidMap>, Error> { ) -> Result<Vec<LabelUuidMap>, Error> {
run_drive_blocking_task(
let (config, _digest) = config::drive::config()?; drive.clone(),
"inventorize".to_string(),
// early check/lock before starting worker move |config| {
let lock_guard = lock_tape_device(&config, &drive)?;
tokio::task::spawn_blocking(move || {
let _lock_guard = lock_guard; // keep lock guard
let (mut changer, changer_name) = required_media_changer(&config, &drive)?; let (mut changer, changer_name) = required_media_changer(&config, &drive)?;
let label_text_list = changer.online_media_label_texts()?; let label_text_list = changer.online_media_label_texts()?;
@ -797,7 +788,9 @@ pub async fn inventory(
} }
Ok(list) Ok(list)
}).await? }
)
.await
} }
#[api( #[api(