api2/tape/drive: wrap some api calls in run_drive_blocking_task

those calls could also block, so we have to run them in a blocking
tokio task, as to not block the current thread

nice side effect is that we now also update the state for that
drive in those instances

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

View File

@ -1014,17 +1014,19 @@ fn barcode_label_media_worker(
}, },
)] )]
/// Read Cartridge Memory (Medium auxiliary memory attributes) /// Read Cartridge Memory (Medium auxiliary memory attributes)
pub fn cartridge_memory(drive: String) -> Result<Vec<MamAttribute>, Error> { pub async fn cartridge_memory(drive: String) -> Result<Vec<MamAttribute>, Error> {
run_drive_blocking_task(
let (config, _digest) = config::drive::config()?; drive.clone(),
"reading cartridge memory".to_string(),
let _lock_guard = lock_tape_device(&config, &drive)?; move |config| {
let drive_config: LinuxTapeDrive = config.lookup("linux", &drive)?; let drive_config: LinuxTapeDrive = config.lookup("linux", &drive)?;
let mut handle = drive_config.open()?; let mut handle = drive_config.open()?;
handle.cartridge_memory() handle.cartridge_memory()
} }
)
.await
}
#[api( #[api(
input: { input: {
@ -1039,17 +1041,19 @@ pub fn cartridge_memory(drive: String) -> Result<Vec<MamAttribute>, Error> {
}, },
)] )]
/// Read Volume Statistics (SCSI log page 17h) /// Read Volume Statistics (SCSI log page 17h)
pub fn volume_statistics(drive: String) -> Result<Lp17VolumeStatistics, Error> { pub async fn volume_statistics(drive: String) -> Result<Lp17VolumeStatistics, Error> {
run_drive_blocking_task(
let (config, _digest) = config::drive::config()?; drive.clone(),
"reading volume statistics".to_string(),
let _lock_guard = lock_tape_device(&config, &drive)?; move |config| {
let drive_config: LinuxTapeDrive = config.lookup("linux", &drive)?; let drive_config: LinuxTapeDrive = config.lookup("linux", &drive)?;
let mut handle = drive_config.open()?; let mut handle = drive_config.open()?;
handle.volume_statistics() handle.volume_statistics()
} }
)
.await
}
#[api( #[api(
input: { input: {
@ -1064,12 +1068,11 @@ pub fn volume_statistics(drive: String) -> Result<Lp17VolumeStatistics, Error> {
}, },
)] )]
/// Get drive/media status /// Get drive/media status
pub fn status(drive: String) -> Result<LinuxDriveAndMediaStatus, Error> { pub async fn status(drive: String) -> Result<LinuxDriveAndMediaStatus, Error> {
run_drive_blocking_task(
let (config, _digest) = config::drive::config()?; drive.clone(),
"reading drive status".to_string(),
let _lock_guard = lock_tape_device(&config, &drive)?; move |config| {
let drive_config: LinuxTapeDrive = config.lookup("linux", &drive)?; let drive_config: LinuxTapeDrive = config.lookup("linux", &drive)?;
// Note: use open_linux_tape_device, because this also works if no medium loaded // Note: use open_linux_tape_device, because this also works if no medium loaded
@ -1079,6 +1082,9 @@ pub fn status(drive: String) -> Result<LinuxDriveAndMediaStatus, Error> {
handle.get_drive_and_media_status() handle.get_drive_and_media_status()
} }
)
.await
}
#[api( #[api(
input: { input: {