From 0908660fc904ab9172169d6a1556532ce9e1a907 Mon Sep 17 00:00:00 2001 From: plazmoid Date: Sat, 29 Aug 2020 00:59:41 +0500 Subject: [PATCH] jobs meta --- bin/u_panel/src/main.rs | 1 - bin/u_server/src/handlers.rs | 37 +++++++++++++++ lib/u_lib/src/client/network.rs | 2 +- lib/u_lib/src/contracts/datatypes.rs | 2 - lib/u_lib/src/contracts/jobs.rs | 71 ++++++++++++++++++++++++---- 5 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 bin/u_server/src/handlers.rs diff --git a/bin/u_panel/src/main.rs b/bin/u_panel/src/main.rs index e427fb7..6669231 100644 --- a/bin/u_panel/src/main.rs +++ b/bin/u_panel/src/main.rs @@ -7,7 +7,6 @@ use u_lib::{ #[tokio::main] async fn main() -> Result<(), &'static str> { - //daemonize(); let mut raw_args = args(); let method = match raw_args.nth(1) { Some(m) => m, diff --git a/bin/u_server/src/handlers.rs b/bin/u_server/src/handlers.rs new file mode 100644 index 0000000..3f7128c --- /dev/null +++ b/bin/u_server/src/handlers.rs @@ -0,0 +1,37 @@ +use u_lib::contracts::*; +use warp::{ + Rejection, + Reply, +}; + +pub async fn add_client( + msg: Message<'_, ClientInfo>, + db: Storage) -> Result +{ + let new_cli = msg.item; + let mut clients = db.lock().await; + if clients.contains_key(&new_cli.id) { + Ok(warp::reply::json( + &RawMsg("Already exist".to_string()).into_message() + )) + } else { + clients.insert( + new_cli.id.clone(), + UClient::new(new_cli.into_owned()) + ); + Ok(warp::reply::json( + &RawMsg("Added".to_string()).into_message() + )) + } +} + +pub async fn listing(db: Storage) -> Result { + let clients = db.lock().await; + let mut result: Vec = Vec::with_capacity(clients.len()); + for cli in clients.values() { + result.push(cli.client_info.clone()); + } + Ok(warp::reply::json( + &Message::new_owned(result) + )) +} \ No newline at end of file diff --git a/lib/u_lib/src/client/network.rs b/lib/u_lib/src/client/network.rs index ddd3681..b2c34cd 100644 --- a/lib/u_lib/src/client/network.rs +++ b/lib/u_lib/src/client/network.rs @@ -67,7 +67,7 @@ impl ClientHandler { } pub async fn list(&self) -> Vec { - let response: Response = dbg!(self.build_get("/ls")) + let response: Response = self.build_get("/ls") .send() .await .unwrap(); diff --git a/lib/u_lib/src/contracts/datatypes.rs b/lib/u_lib/src/contracts/datatypes.rs index e865794..44ca8d3 100644 --- a/lib/u_lib/src/contracts/datatypes.rs +++ b/lib/u_lib/src/contracts/datatypes.rs @@ -11,8 +11,6 @@ use { //pub type JobPool = Vec>; -pub type JobPool = Vec; -pub type JobResult = Result, Vec>; pub type CliStorage = HashMap; diff --git a/lib/u_lib/src/contracts/jobs.rs b/lib/u_lib/src/contracts/jobs.rs index 1c8320b..8bef23e 100644 --- a/lib/u_lib/src/contracts/jobs.rs +++ b/lib/u_lib/src/contracts/jobs.rs @@ -1,13 +1,13 @@ use std::{ - process::Command + process::Command, + time::SystemTime }; +use uuid::Uuid; //use tokio::process::Command; use super::*; -pub trait Job { - fn run(&mut self); - fn get_result(&self) -> &JobResult; -} +pub type JobPool = Vec; +pub type JobResult = Result, Vec>; pub struct ShellJob { pub result: JobResult, @@ -28,9 +28,7 @@ impl ShellJob { result: Err(b"Did not run".to_vec()) } } -} -impl Job for ShellJob { fn run(&mut self) { let result = Command::new(&self.cmd) .args(&self.args) @@ -52,7 +50,62 @@ impl Job for ShellJob { } } +pub enum ManageAction { + Ping, + UpdateAvailable, + JobsResultsRequest, + Terminate +} + +pub enum JobSchedule { + Once, + Permanent, + //TODO: Scheduled +} + +pub enum JobState { + Dummy, + Queued, // server created a job, but client didn't get it yet + Pending, // client got a job, but not running yet + Running, // client is currently running a job + Rerunning, // if job is cycled + Finished, +} -/* pub struct PyJob { +pub enum JobType { + Manage(ManageAction), + Shell, + Python, + Binary +} + +pub struct JobMeta { + id: Uuid, + name: String, + created: SystemTime, + updated: SystemTime, + state: JobState, + exec_type: JobType, + append_result: bool, //true: append, false: rewrite + payload: Option>, +} + +impl JobMeta { + pub fn from_shell(shell_cmd: Vec) -> Self { + let uid = Uuid::new_v4(); + let str_payload_name = shell_cmd.split(b" ").collect(); + let job_name = format!("{} {}", uid.to_string()[..6], str_payload_name[0]); + Self { + id: uid, + name: job_name, + created: SystemTime::now(), + updated: SystemTime::now(), + state: JobState::Pending, + exec_type: JobType::Shell, + append_result: true, + payload: Some(shell_cmd) + } + } +} -} */ \ No newline at end of file +impl ToMsg for JobMeta {} \ No newline at end of file