api2/tape/drive: use 'run_drive_worker' where possible

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-02-18 15:40:23 +01:00 committed by Dietmar Maurer
parent a44c934b5d
commit a1c5575308
1 changed files with 52 additions and 171 deletions

View File

@ -124,32 +124,20 @@ pub fn load_media(
label_text: String, label_text: String,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let (config, _digest) = config::drive::config()?;
// early check/lock before starting worker
let lock_guard = lock_tape_device(&config, &drive)?;
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
let job_id = format!("{}:{}", drive, label_text); let job_id = format!("{}:{}", drive, label_text);
let upid_str = WorkerTask::new_thread( let upid_str = run_drive_worker(
rpcenv,
drive.clone(),
"load-media", "load-media",
Some(job_id), Some(job_id),
auth_id, move |worker, config| {
to_stdout,
move |worker| {
let _lock_guard = lock_guard; // keep lock guard
task_log!(worker, "loading media '{}' into drive '{}'", label_text, drive); task_log!(worker, "loading media '{}' into drive '{}'", label_text, drive);
let (mut changer, _) = required_media_changer(&config, &drive)?; let (mut changer, _) = required_media_changer(&config, &drive)?;
changer.load_media(&label_text)?; changer.load_media(&label_text)?;
Ok(()) Ok(())
} },
)?; )?;
Ok(upid_str.into()) Ok(upid_str.into())
@ -241,29 +229,18 @@ pub fn unload(
target_slot: Option<u64>, target_slot: Option<u64>,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let upid_str = run_drive_worker(
let (config, _digest) = config::drive::config()?; rpcenv,
// early check/lock before starting worker drive.clone(),
let lock_guard = lock_tape_device(&config, &drive)?;
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
let upid_str = WorkerTask::new_thread(
"unload-media", "unload-media",
Some(drive.clone()), Some(drive.clone()),
auth_id, move |worker, config| {
to_stdout,
move |worker| {
let _lock_guard = lock_guard; // keep lock guard
task_log!(worker, "unloading media from drive '{}'", drive); task_log!(worker, "unloading media from drive '{}'", drive);
let (mut changer, _) = required_media_changer(&config, &drive)?; let (mut changer, _) = required_media_changer(&config, &drive)?;
changer.unload_media(target_slot)?; changer.unload_media(target_slot)?;
Ok(()) Ok(())
} },
)?; )?;
Ok(upid_str.into()) Ok(upid_str.into())
@ -298,23 +275,12 @@ pub fn erase_media(
label_text: Option<String>, label_text: Option<String>,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let (config, _digest) = config::drive::config()?; let upid_str = run_drive_worker(
rpcenv,
// early check/lock before starting worker drive.clone(),
let lock_guard = lock_tape_device(&config, &drive)?;
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
let upid_str = WorkerTask::new_thread(
"erase-media", "erase-media",
Some(drive.clone()), Some(drive.clone()),
auth_id, move |worker, config| {
to_stdout,
move |worker| {
let _lock_guard = lock_guard; // keep lock guard
if let Some(ref label) = label_text { if let Some(ref label) = label_text {
task_log!(worker, "try to load media '{}'", label); task_log!(worker, "try to load media '{}'", label);
if let Some((mut changer, _)) = media_changer(&config, &drive)? { if let Some((mut changer, _)) = media_changer(&config, &drive)? {
@ -368,7 +334,7 @@ pub fn erase_media(
} }
Ok(()) Ok(())
} },
)?; )?;
Ok(upid_str.into()) Ok(upid_str.into())
@ -391,27 +357,16 @@ pub fn rewind(
drive: String, drive: String,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let upid_str = run_drive_worker(
let (config, _digest) = config::drive::config()?; rpcenv,
drive.clone(),
// early check/lock before starting worker
let lock_guard = lock_tape_device(&config, &drive)?;
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
let upid_str = WorkerTask::new_thread(
"rewind-media", "rewind-media",
Some(drive.clone()), Some(drive.clone()),
auth_id, move |_worker, config| {
to_stdout,
move |_worker| {
let _lock_guard = lock_guard; // keep lock guard
let mut drive = open_drive(&config, &drive)?; let mut drive = open_drive(&config, &drive)?;
drive.rewind()?; drive.rewind()?;
Ok(()) Ok(())
} },
)?; )?;
Ok(upid_str.into()) Ok(upid_str.into())
@ -434,24 +389,12 @@ pub fn eject_media(
drive: String, drive: String,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let upid_str = run_drive_worker(
let (config, _digest) = config::drive::config()?; rpcenv,
drive.clone(),
// early check/lock before starting worker
let lock_guard = lock_tape_device(&config, &drive)?;
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", "eject-media",
Some(drive.clone()), Some(drive.clone()),
auth_id, move |_worker, config| {
to_stdout,
move |_worker| {
let _lock_guard = lock_guard; // keep lock guard
if let Some((mut changer, _)) = media_changer(&config, &drive)? { if let Some((mut changer, _)) = media_changer(&config, &drive)? {
changer.unload_media(None)?; changer.unload_media(None)?;
} else { } else {
@ -459,7 +402,8 @@ pub fn eject_media(
drive.eject_media()?; drive.eject_media()?;
} }
Ok(()) Ok(())
})?; },
)?;
Ok(upid_str.into()) Ok(upid_str.into())
} }
@ -495,9 +439,6 @@ pub fn label_media(
label_text: String, label_text: String,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
if let Some(ref pool) = pool { if let Some(ref pool) = pool {
let (pool_config, _digest) = config::media_pool::config()?; let (pool_config, _digest) = config::media_pool::config()?;
@ -505,22 +446,12 @@ pub fn label_media(
bail!("no such pool ('{}')", pool); bail!("no such pool ('{}')", pool);
} }
} }
let upid_str = run_drive_worker(
let (config, _digest) = config::drive::config()?; rpcenv,
drive.clone(),
// early check/lock before starting worker
let lock_guard = lock_tape_device(&config, &drive)?;
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
let upid_str = WorkerTask::new_thread(
"label-media", "label-media",
Some(drive.clone()), Some(drive.clone()),
auth_id, move |worker, config| {
to_stdout,
move |worker| {
let _lock_guard = lock_guard; // keep lock guard
let mut drive = open_drive(&config, &drive)?; let mut drive = open_drive(&config, &drive)?;
drive.rewind()?; drive.rewind()?;
@ -545,7 +476,7 @@ pub fn label_media(
}; };
write_media_label(worker, &mut drive, label, pool) write_media_label(worker, &mut drive, label, pool)
} },
)?; )?;
Ok(upid_str.into()) Ok(upid_str.into())
@ -757,24 +688,12 @@ pub fn clean_drive(
drive: String, drive: String,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let upid_str = run_drive_worker(
let (config, _digest) = config::drive::config()?; rpcenv,
drive.clone(),
// early check/lock before starting worker
let lock_guard = lock_tape_device(&config, &drive)?;
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
let upid_str = WorkerTask::new_thread(
"clean-drive", "clean-drive",
Some(drive.clone()), Some(drive.clone()),
auth_id, move |worker, config| {
to_stdout,
move |worker| {
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)?;
worker.log("Starting drive clean"); worker.log("Starting drive clean");
@ -784,7 +703,8 @@ pub fn clean_drive(
worker.log("Drive cleaned sucessfully"); worker.log("Drive cleaned sucessfully");
Ok(()) Ok(())
})?; },
)?;
Ok(upid_str.into()) Ok(upid_str.into())
} }
@ -891,24 +811,12 @@ pub fn update_inventory(
read_all_labels: Option<bool>, read_all_labels: Option<bool>,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let upid_str = run_drive_worker(
let (config, _digest) = config::drive::config()?; rpcenv,
drive.clone(),
// early check/lock before starting worker
let lock_guard = lock_tape_device(&config, &drive)?;
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
let upid_str = WorkerTask::new_thread(
"inventory-update", "inventory-update",
Some(drive.clone()), Some(drive.clone()),
auth_id, move |worker, config| {
to_stdout,
move |worker| {
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()?;
@ -960,7 +868,7 @@ pub fn update_inventory(
changer.unload_media(None)?; changer.unload_media(None)?;
} }
Ok(()) Ok(())
} },
)?; )?;
Ok(upid_str.into()) Ok(upid_str.into())
@ -989,7 +897,6 @@ pub fn barcode_label_media(
pool: Option<String>, pool: Option<String>,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
if let Some(ref pool) = pool { if let Some(ref pool) = pool {
let (pool_config, _digest) = config::media_pool::config()?; let (pool_config, _digest) = config::media_pool::config()?;
@ -998,24 +905,12 @@ pub fn barcode_label_media(
} }
} }
let (drive_config, _digest) = config::drive::config()?; let upid_str = run_drive_worker(
rpcenv,
// early check/lock before starting worker drive.clone(),
let lock_guard = lock_tape_device(&drive_config, &drive)?;
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
let upid_str = WorkerTask::new_thread(
"barcode-label-media", "barcode-label-media",
Some(drive.clone()), Some(drive.clone()),
auth_id, move |worker, config| barcode_label_media_worker(worker, drive, &config, pool),
to_stdout,
move |worker| {
let _lock_guard = lock_guard; // keep lock guard
barcode_label_media_worker(worker, drive, &drive_config, pool)
}
)?; )?;
Ok(upid_str.into()) Ok(upid_str.into())
@ -1027,7 +922,6 @@ fn barcode_label_media_worker(
drive_config: &SectionConfigData, drive_config: &SectionConfigData,
pool: Option<String>, pool: Option<String>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let (mut changer, changer_name) = required_media_changer(drive_config, &drive)?; let (mut changer, changer_name) = required_media_changer(drive_config, &drive)?;
let label_text_list = changer.online_media_label_texts()?; let label_text_list = changer.online_media_label_texts()?;
@ -1202,27 +1096,15 @@ pub fn catalog_media(
verbose: Option<bool>, verbose: Option<bool>,
rpcenv: &mut dyn RpcEnvironment, rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let verbose = verbose.unwrap_or(false); let verbose = verbose.unwrap_or(false);
let force = force.unwrap_or(false); let force = force.unwrap_or(false);
let (config, _digest) = config::drive::config()?; let upid_str = run_drive_worker(
rpcenv,
// early check/lock before starting worker drive.clone(),
let lock_guard = lock_tape_device(&config, &drive)?;
let auth_id: Authid = rpcenv.get_auth_id().unwrap().parse()?;
let to_stdout = rpcenv.env_type() == RpcEnvironmentType::CLI;
let upid_str = WorkerTask::new_thread(
"catalog-media", "catalog-media",
Some(drive.clone()), Some(drive.clone()),
auth_id, move |worker, config| {
to_stdout,
move |worker| {
let _lock_guard = lock_guard; // keep lock guard
let mut drive = open_drive(&config, &drive)?; let mut drive = open_drive(&config, &drive)?;
drive.rewind()?; drive.rewind()?;
@ -1279,8 +1161,7 @@ pub fn catalog_media(
restore_media(&worker, &mut drive, &media_id, None, verbose)?; restore_media(&worker, &mut drive, &media_id, None, verbose)?;
Ok(()) Ok(())
},
}
)?; )?;
Ok(upid_str.into()) Ok(upid_str.into())