From 1e64feeaada0e0bcda40296d86540b1901c33699 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Wed, 18 Oct 2023 19:36:32 +0200 Subject: [PATCH] rest-server: factor out task-log directory and creation We had two call sites deriving the directory "shard" where the task log file is actually saved to, this can lead to ugly bugs and is better done in a central single-source-of-truth way. While at it factor out the creation of the log file (and it's shard directory) to avoid crowding the WorkerTask new fn to much. Signed-off-by: Thomas Lamprecht --- proxmox-rest-server/src/worker_task.rs | 36 +++++++++++++++----------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/proxmox-rest-server/src/worker_task.rs b/proxmox-rest-server/src/worker_task.rs index 54b6bc2c..7f5319bb 100644 --- a/proxmox-rest-server/src/worker_task.rs +++ b/proxmox-rest-server/src/worker_task.rs @@ -88,13 +88,31 @@ impl WorkerTaskSetup { Ok(TaskListLockGuard(file)) } - fn log_path(&self, upid: &UPID) -> std::path::PathBuf { + fn log_directory(&self, upid: &UPID) -> std::path::PathBuf { let mut path = self.taskdir.clone(); - path.push(format!("{:02X}", upid.pstart % 256)); + path.push(format!("{:02X}", upid.pstart & 255)); + path + } + + fn log_path(&self, upid: &UPID) -> std::path::PathBuf { + let mut path = self.log_directory(upid); path.push(upid.to_string()); path } + fn create_and_get_log_path(&self, upid: &UPID) -> Result { + let mut path = self.log_directory(upid); + let dir_opts = self + .file_opts + .clone() + .perm(nix::sys::stat::Mode::from_bits_truncate(0o755)); + + create_path(&path, None, Some(dir_opts))?; + + path.push(upid.to_string()); + Ok(path) + } + // atomically read/update the task list, update status of finished tasks // new_upid is added to the list when specified. fn update_active_workers(&self, new_upid: Option<&UPID>) -> Result<(), Error> { @@ -372,7 +390,6 @@ pub fn upid_read_status(upid: &UPID) -> Result { }; let path = setup.log_path(upid); - let mut file = File::open(path)?; /// speedup - only read tail @@ -814,18 +831,7 @@ impl WorkerTask { let upid = UPID::new(worker_type, worker_id, auth_id)?; let task_id = upid.task_id; - let mut path = setup.taskdir.clone(); - - path.push(format!("{:02X}", upid.pstart & 255)); - - let dir_opts = setup - .file_opts - .clone() - .perm(nix::sys::stat::Mode::from_bits_truncate(0o755)); - - create_path(&path, None, Some(dir_opts))?; - - path.push(upid.to_string()); + let path = setup.create_and_get_log_path(&upid)?; let logger_options = FileLogOptions { to_stdout,