]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/api2/node/tasks.rs: implement list_tasks
authorDietmar Maurer <dietmar@proxmox.com>
Sun, 7 Apr 2019 10:18:58 +0000 (12:18 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Sun, 7 Apr 2019 10:18:58 +0000 (12:18 +0200)
src/api2/node.rs
src/api2/node/tasks.rs [new file with mode: 0644]

index 89ef8931478f34f98fda3d7f8fbee09012737c51..1ec90a5b0db864821441fcc974a467e3cfc9d2b0 100644 (file)
@@ -2,6 +2,7 @@ use crate::api_schema::*;
 use crate::api_schema::router::*;
 use serde_json::{json};
 
+mod tasks;
 mod time;
 mod network;
 mod dns;
@@ -24,6 +25,7 @@ pub fn router() -> Router {
         .subdir("network", network::router())
         .subdir("services", services::router())
         .subdir("syslog", syslog::router())
+        .subdir("tasks", tasks::router())
         .subdir("time", time::router());
 
     route
diff --git a/src/api2/node/tasks.rs b/src/api2/node/tasks.rs
new file mode 100644 (file)
index 0000000..25cabb9
--- /dev/null
@@ -0,0 +1,88 @@
+use failure::*;
+
+//use crate::tools;
+use crate::api_schema::*;
+use crate::api_schema::router::*;
+use serde_json::{json, Value};
+
+use crate::server;
+
+fn list_tasks(
+    param: Value,
+    _info: &ApiMethod,
+    rpcenv: &mut RpcEnvironment,
+) -> Result<Value, Error> {
+
+    let start = param["start"].as_u64().unwrap_or(0);
+    let limit = param["limit"].as_u64().unwrap_or(50);
+    let errors = param["errors"].as_bool().unwrap_or(false);
+
+    let list = server::read_task_list()?;
+
+    let mut result = vec![];
+
+    let mut count = 0;
+
+    for info in list.iter() {
+        let mut entry = json!({
+            "upid": info.upid_str,
+            "node": "localhost",
+            "pid": info.upid.pid,
+            "pstart": info.upid.pstart,
+            "starttime": info.upid.starttime,
+            "type": info.upid.worker_type,
+            "id": info.upid.worker_id,
+            "user": info.upid.username,
+        });
+
+        if let Some(ref state) = info.state {
+            if errors && state.1 == "OK" {
+                continue;
+            }
+
+            entry["endtime"] = Value::from(state.0);
+            entry["status"] = Value::from(state.1.clone());
+        }
+
+        if (count as u64) <= start {
+            count += 1;
+            continue;
+        } else {
+            count += 1;
+        }
+
+        if (result.len() as u64) < limit { result.push(entry); };
+    }
+
+    rpcenv.set_result_attrib("total", Value::from(count));
+
+    Ok(json!(result))
+}
+
+pub fn router() -> Router {
+
+    let route = Router::new()
+        .get(ApiMethod::new(
+            list_tasks,
+            ObjectSchema::new("List tasks.")
+                .optional(
+                    "start",
+                    IntegerSchema::new("List tasks beginning from this offset.")
+                        .minimum(0)
+                        .default(0)
+                )
+                .optional(
+                    "limit",
+                    IntegerSchema::new("Only list this amount of tasks.")
+                        .minimum(0)
+                        .default(50)
+                )
+                .optional(
+                    "errors",
+                    BooleanSchema::new("Only list erroneous tasks.")
+                )
+            )
+        );
+
+    route
+}