From 54c77b3d62414777862ef4b059d4b11dc6efdb68 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Thu, 18 Feb 2021 15:40:24 +0100 Subject: [PATCH] api2/tape/drive: add wrapper for tokio::task::spawn_blocking similar to the worker wrapper, lock, write status, run code, unset status Signed-off-by: Dominik Csapak --- src/api2/tape/drive.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/api2/tape/drive.rs b/src/api2/tape/drive.rs index 7a6461b0..b4cd4aa6 100644 --- a/src/api2/tape/drive.rs +++ b/src/api2/tape/drive.rs @@ -101,6 +101,26 @@ where }) } +async fn run_drive_blocking_task(drive: String, state: String, f: F) -> Result +where + F: Send + 'static + FnOnce(SectionConfigData) -> Result, + R: Send + 'static, +{ + // early check/lock before starting worker + let (config, _digest) = config::drive::config()?; + let lock_guard = lock_tape_device(&config, &drive)?; + tokio::task::spawn_blocking(move || { + let _lock_guard = lock_guard; + set_tape_device_state(&drive, &state) + .map_err(|err| format_err!("could not set tape device state: {}", err))?; + let result = f(config); + set_tape_device_state(&drive, "") + .map_err(|err| format_err!("could not unset tape device state: {}", err))?; + result + }) + .await? +} + #[api( input: { properties: {