瀏覽代碼

bin/taud: add 'list' rpc function

ghassmo 4 年之前
父節點
當前提交
c5b7d97bf1
共有 1 個文件被更改,包括 22 次插入1 次删除
  1. 22 1
      bin/taud/src/main.rs

+ 22 - 1
bin/taud/src/main.rs

@@ -24,6 +24,7 @@ mod task_info;
 mod util;
 
 use crate::{
+    month_tasks::MonthTasks,
     task_info::TaskInfo,
     util::{get_current_time, CliTaud, Settings, TauConfig, Timestamp, CONFIG_FILE_CONTENTS},
 };
@@ -42,6 +43,7 @@ impl RequestHandler for JsonRpcInterface {
 
         match req.method.as_str() {
             Some("add") => return self.add(req.id, req.params).await,
+            Some("list") => return self.list(req.id, req.params).await,
             Some(_) | None => return JsonResult::Err(jsonerr(MethodNotFound, None, req.id)),
         }
     }
@@ -112,11 +114,30 @@ impl JsonRpcInterface {
             }
         }
 
-        match task.save() {
+        let result = || -> Result<()> {
+            task.save()?;
+            task.activate()?;
+            Ok(())
+        };
+
+        match result() {
             Ok(()) => JsonResult::Resp(jsonresp(json!(true), id)),
             Err(e) => JsonResult::Err(jsonerr(ServerError(-32603), Some(e.to_string()), id)),
         }
     }
+
+    // RPCAPI:
+    // List tasks
+    // --> {"jsonrpc": "2.0", "method": "list", "params": [], "id": 1}
+    // <-- {"jsonrpc": "2.0", "result": [task, ...], "id": 1}
+    async fn list(&self, id: Value, _params: Value) -> JsonResult {
+        let tasks: Result<Vec<TaskInfo>> = MonthTasks::load_current_open_tasks(&self.settings);
+
+        match tasks {
+            Ok(tks) => JsonResult::Resp(jsonresp(json!(tks), id)),
+            Err(e) => JsonResult::Err(jsonerr(ServerError(-32603), Some(e.to_string()), id)),
+        }
+    }
 }
 
 async fn start(config: TauConfig, executor: Arc<Executor<'_>>) -> Result<()> {