diff --git a/Cargo.toml b/Cargo.toml index a5f52152131604a357183684886dbd6f48d91c86..aba0b3ce712f7619eb78d1082834de41b26ee82e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,12 @@ license = "MIT" repository = "https://lab.themackabu.dev/self/pmc" description = "PMC is a simple and easy to use PM2 alternative" +[profile.release] +lto = true +codegen-units = 16 +opt-level = "z" +panic = "abort" + [build-dependencies] tar = "0.4.40" chrono = "0.4.31" diff --git a/src/cli/internal.rs b/src/cli/internal.rs index 3f5a6e30d6f805eac1383ad88d4f0ad9ebc0442f..986ee43a80fde71c7127bf3d94e8911529f1b86e 100644 --- a/src/cli/internal.rs +++ b/src/cli/internal.rs @@ -161,6 +161,12 @@ impl<'i> Internal<'i> { log!("process removed (id={})", self.id); } + pub fn flush(&mut self) { + self.runner.flush(self.id); + println!("{} Log Flushed {}({}) ✓", *helpers::SUCCESS, self.kind, self.id); + log!("process log flushed (id={})", self.id); + } + pub fn info(&self, format: &String) { #[derive(Clone, Debug, Tabled)] struct Info { diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 7b91babbb866c07f87373c61fbacf9209302ab83..6d2553c6d78397f38a2fa9b81d8f338c891582ad 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -159,3 +159,16 @@ pub fn env(item: &Item, server_name: &String) { }, } } + +pub fn flush(item: &Item, server_name: &String) { + let runner: Runner = Runner::new(); + let (kind, _) = format(server_name); + + match item { + Item::Id(id) => Internal { id: *id, runner, server_name, kind }.flush(), + Item::Name(name) => match runner.find(&name, server_name) { + Some(id) => Internal { id, runner, server_name, kind }.flush(), + None => crashln!("{} Process ({name}) not found", *helpers::FAIL), + }, + } +} \ No newline at end of file diff --git a/src/daemon/api/routes.rs b/src/daemon/api/routes.rs index 585506fa2176eb84ea1dd8aaa54052599ba50631..384b26098740ad76213619ffed1dbf1d5e331279 100644 --- a/src/daemon/api/routes.rs +++ b/src/daemon/api/routes.rs @@ -759,6 +759,10 @@ pub async fn action_handler(id: usize, body: Json, _t: Token) -> Res timer.observe_duration(); Ok(Json(attempt(true, method))) } + "flush" => { + runner.flush(id); + Ok(Json(attempt(true, method))) + }, _ => { timer.observe_duration(); Err(not_found("Process was not found")) diff --git a/src/main.rs b/src/main.rs index 06d1b3a634b6dd782d582e97272da85f1547e48e..d34660bb5939f8f5720c488b36aedb17380b1171 100644 --- a/src/main.rs +++ b/src/main.rs @@ -111,7 +111,7 @@ enum Commands { }, /// Stop then remove a process - #[command(visible_alias = "rm")] + #[command(visible_alias = "rm", visible_alias = "delete")] Remove { #[clap(value_parser = cli::validate::)] item: Item, @@ -181,6 +181,17 @@ enum Commands { server: Option, }, + /// Flush a process log + #[command(visible_alias = "fl")] + Flush { + #[clap(value_parser = cli::validate::)] + item: Item, + /// Server + #[arg(short, long)] + server: Option, + }, + + /// Daemon management #[command(visible_alias = "agent", visible_alias = "bgd")] Daemon { @@ -219,6 +230,7 @@ fn main() { Commands::Details { item, format, server } => cli::info(item, format, &defaults(server)), Commands::List { format, server } => Internal::list(format, &defaults(server)), Commands::Logs { item, lines, server } => cli::logs(item, lines, &defaults(server)), + Commands::Flush { item, server } => cli::flush(item, &defaults(server)), Commands::Daemon { command } => match command { Daemon::Stop => daemon::stop(), diff --git a/src/process/http.rs b/src/process/http.rs index 008fddff2c5e9152230a20320d6ee690cd67729b..cbd85423af78f281a9078dda1c2512aaa884f0f6 100644 --- a/src/process/http.rs +++ b/src/process/http.rs @@ -99,3 +99,10 @@ pub fn remove(Remote { address, token, .. }: &Remote, id: usize) -> Result Result { + let (client, headers) = sync::client(token); + let content = ActionBody { method: string!("flush") }; + + Ok(client.post(fmtstr!("{address}/process/{id}/action")).json(&content).headers(headers).send()?) +} \ No newline at end of file diff --git a/src/process/mod.rs b/src/process/mod.rs index 9fd98ceb84ceb9a8500e82ce4dfe1b4187f0e7c1..2cf3f3b8c5ba73414db91abc39bf417a72642b51 100644 --- a/src/process/mod.rs +++ b/src/process/mod.rs @@ -173,6 +173,18 @@ impl Status { } } +impl LogInfo { + pub fn flush(&self) { + if let Err(err) = std::fs::remove_file(&self.out) { + crashln!("Failed to remove log {0} file: {err}", self.out); + } + + if let Err(err) = std::fs::remove_file(&self.error) { + crashln!("Failed to remove log {0} file: {err}", self.error); + } + } +} + macro_rules! lock { ($runner:expr) => {{ match $runner.lock() { @@ -494,6 +506,21 @@ impl Runner { return processes; } + + pub fn flush(&mut self, id: usize) -> &mut Self { + if let Some(remote) = &self.remote { + if let Err(err) = http::flush(remote, id) { + crashln!("{} Failed to flush process {id}\nError: {:#?}", *helpers::FAIL, err); + }; + } else { + match self.info(id) { + Some(item) => item.logs().flush(), + None => crashln!("{} Process ({id}) not found", *helpers::FAIL), + }; + } + + self + } } impl Process { diff --git a/src/webui/src/components/react/view.tsx b/src/webui/src/components/react/view.tsx index e31ca2a7b2c13dfe41199ff1b2e6fd1dea4c9f9d..1cfb78f964059d49248bded80f6a87354f27b8e9 100644 --- a/src/webui/src/components/react/view.tsx +++ b/src/webui/src/components/react/view.tsx @@ -339,6 +339,15 @@ const View = (props: { id: string; base: string }) => { {({ focus }) => } + + {({ _ }) => ( + action(props.id, 'flush')} + className="text-zinc-200 rounded-md block p-2 w-full text-left cursor-pointer hover:bg-zinc-800/80 hover:text-zinc-50"> + Flush + + )} +