diff --git a/src/api2/node.rs b/src/api2/node.rs index 89ef8931..1ec90a5b 100644 --- a/src/api2/node.rs +++ b/src/api2/node.rs @@ -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 index 00000000..25cabb9f --- /dev/null +++ b/src/api2/node/tasks.rs @@ -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 { + + 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 +}