From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 7268B60EFE for ; Fri, 25 Sep 2020 16:14:06 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 7018A1C253 for ; Fri, 25 Sep 2020 16:13:35 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 26E611C18A for ; Fri, 25 Sep 2020 16:13:30 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id CF3B645776 for ; Fri, 25 Sep 2020 16:13:29 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Fri, 25 Sep 2020 16:13:22 +0200 Message-Id: <20200925141327.25024-10-d.csapak@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200925141327.25024-1-d.csapak@proxmox.com> References: <20200925141327.25024-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.169 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.379 Envelope sender has no MX or A DNS records RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record Subject: [pbs-devel] [PATCH proxmox-backup 06/10] server/worker_task: add TaskListInfoIterator X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 25 Sep 2020 14:14:06 -0000 this is an iterator that reads/parses/updates the task list as necessary and returns the tasks in descending order (newest first) it does this by using our logrotate iterator and using a vecdeque we can use this to iterate over all tasks, even if they are in the archive and even if the archive is logrotated but only read as much as we need Signed-off-by: Dominik Csapak --- src/server/worker_task.rs | 82 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs index 98047814..622453a1 100644 --- a/src/server/worker_task.rs +++ b/src/server/worker_task.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::collections::{HashMap, VecDeque}; use std::fs::File; use std::io::{Read, Write, BufRead, BufReader}; use std::panic::UnwindSafe; @@ -16,6 +16,7 @@ use tokio::sync::oneshot; use proxmox::sys::linux::procfs; use proxmox::try_block; use proxmox::tools::fs::{create_path, open_file_locked, open_file_locked_shared, replace_file, CreateOptions}; +use proxmox::tools::logrotate::{LogRotate, LogRotateFiles}; use super::UPID; @@ -496,6 +497,85 @@ where read_task_file(file) } +enum TaskFile { + Active, + Index, + Archive, +} + +pub struct TaskListInfoIterator { + list: VecDeque, + file: TaskFile, + archive: LogRotateFiles, + _lock: File, +} + +impl TaskListInfoIterator { + pub fn new() -> Result { + let (read_lock, active_list) = { + let lock = lock_task_list_files(false)?; + let active_list = read_task_file_from_path(PROXMOX_BACKUP_ACTIVE_TASK_FN)?; + + let needs_update = active_list + .iter() + .any(|info| info.state.is_none() && !worker_is_active_local(&info.upid)); + + if needs_update { + drop(lock); + update_active_workers(None)?; + let lock = lock_task_list_files(false)?; + let active_list = read_task_file_from_path(PROXMOX_BACKUP_ACTIVE_TASK_FN)?; + (lock, active_list) + } else { + (lock, active_list) + } + }; + let logrotate = LogRotate::new(PROXMOX_BACKUP_ARCHIVE_TASK_FN, true).ok_or_else(|| format_err!("could not get archive file names"))?; + + Ok(Self { + list: active_list.into(), + file: TaskFile::Active, + archive: logrotate.files(), + _lock: read_lock, + }) + } +} + +impl Iterator for TaskListInfoIterator { + type Item = Result; + + fn next(&mut self) -> Option { + loop { + if let Some(element) = self.list.pop_back() { + return Some(Ok(element)); + } else { + match self.file { + TaskFile::Active => { + let index = match read_task_file_from_path(PROXMOX_BACKUP_INDEX_TASK_FN) { + Ok(index) => index, + Err(err) => return Some(Err(err)), + }; + self.list.append(&mut index.into()); + self.file = TaskFile::Index; + }, + TaskFile::Index | TaskFile::Archive => { + if let Some(file) = self.archive.next() { + let archive = match read_task_file(file) { + Ok(archive) => archive, + Err(err) => return Some(Err(err)), + }; + self.list.append(&mut archive.into()); + } else { + return None; + } + self.file = TaskFile::Archive; + } + } + } + } + } +} + /// Launch long running worker tasks. /// /// A worker task can either be a whole thread, or a simply tokio -- 2.20.1