5
0
mirror of git://git.proxmox.com/git/proxmox-backup.git synced 2025-01-10 01:18:06 +03:00

api2/node/tasks: use TaskListInfoIterator instead of read_task_list

this makes the filtering/limiting much nicer and readable

since we now have potentially an 'infinite' amount of tasks we iterate over,
and cannot now beforehand how many there are, we return the total count
as always 1 higher then requested iff we are not at the end (this is
the case when the amount of entries is smaller than the requested limit)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-09-28 15:32:09 +02:00 committed by Dietmar Maurer
parent e7244387c7
commit 768e10d0b3

View File

@ -10,7 +10,7 @@ use proxmox::{identity, list_subdirs_api_method, sortable};
use crate::tools;
use crate::api2::types::*;
use crate::server::{self, UPID, TaskState};
use crate::server::{self, UPID, TaskState, TaskListInfoIterator};
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
use crate::config::cached_user_info::CachedUserInfo;
@ -316,56 +316,54 @@ pub fn list_tasks(
let store = param["store"].as_str();
let list = TaskListInfoIterator::new(running)?;
let list = server::read_task_list()?;
let mut result = vec![];
let mut count = 0;
for info in list {
if !list_all && info.upid.userid != userid { continue; }
let result: Vec<TaskListItem> = list
.take_while(|info| !info.is_err())
.filter_map(|info| {
let info = match info {
Ok(info) => info,
Err(_) => return None,
};
if !list_all && info.upid.userid != userid { return None; }
if let Some(userid) = &userfilter {
if !info.upid.userid.as_str().contains(userid) { continue; }
if !info.upid.userid.as_str().contains(userid) { return None; }
}
if let Some(store) = store {
// Note: useful to select all tasks spawned by proxmox-backup-client
let worker_id = match &info.upid.worker_id {
Some(w) => w,
None => continue, // skip
None => return None, // skip
};
if info.upid.worker_type == "backup" || info.upid.worker_type == "restore" ||
info.upid.worker_type == "prune"
{
let prefix = format!("{}_", store);
if !worker_id.starts_with(&prefix) { continue; }
if !worker_id.starts_with(&prefix) { return None; }
} else if info.upid.worker_type == "garbage_collection" {
if worker_id != store { continue; }
if worker_id != store { return None; }
} else {
continue; // skip
return None; // skip
}
}
if let Some(ref state) = info.state {
if running { continue; }
match state {
crate::server::TaskState::OK { .. } if errors => continue,
match info.state {
Some(crate::server::TaskState::OK { .. }) if errors => return None,
_ => {},
}
}
if (count as u64) < start {
count += 1;
continue;
} else {
count += 1;
}
Some(info.into())
}).skip(start as usize)
.take(limit as usize)
.collect();
if (result.len() as u64) < limit { result.push(info.into()); };
let mut count = result.len() + start as usize;
if result.len() > 0 && result.len() >= limit as usize { // we have a 'virtual' entry as long as we have any new
count += 1;
}
rpcenv["total"] = Value::from(count);