Page MenuHomePhorge

No OneTemporary

Size
90 KB
Referenced Files
None
Subscribers
None
diff --git a/src/cli/internal.rs b/src/cli/internal.rs
index c89052a..d41cada 100644
--- a/src/cli/internal.rs
+++ b/src/cli/internal.rs
@@ -1,648 +1,652 @@
use colored::Colorize;
use macros_rs::{crashln, string, ternary, then};
use psutil::process::{MemoryInfo, Process};
use regex::Regex;
use serde::Serialize;
use serde_json::json;
use pmc::{
config, file,
helpers::{self, ColoredString},
log,
process::{http, ItemSingle, Runner},
};
use tabled::{
settings::{
object::{Columns, Rows},
style::{BorderColor, Style},
themes::Colorization,
Color, Modify, Rotate, Width,
},
Table, Tabled,
};
pub struct Internal<'i> {
pub id: usize,
pub runner: Runner,
pub kind: String,
pub server_name: &'i str,
}
impl<'i> Internal<'i> {
pub fn create(mut self, script: &String, name: &Option<String>, watch: &Option<String>, silent: bool) -> Runner {
let config = config::read();
let name = match name {
Some(name) => string!(name),
None => string!(script.split_whitespace().next().unwrap_or_default()),
};
if matches!(self.server_name, "internal" | "local") {
let pattern = Regex::new(r"(?m)^[a-zA-Z0-9]+(/[a-zA-Z0-9]+)*(\.js|\.ts)?$").unwrap();
if pattern.is_match(script) {
let script = format!("{} {script}", config.runner.node);
self.runner.start(&name, &script, file::cwd(), watch).save();
} else {
self.runner.start(&name, script, file::cwd(), watch).save();
}
} else {
let Some(servers) = config::servers().servers else {
crashln!("{} Failed to read servers", *helpers::FAIL)
};
if let Some(server) = servers.get(self.server_name) {
match Runner::connect(self.server_name.into(), server.get(), false) {
Some(mut remote) => remote.start(&name, script, file::cwd(), watch),
None => crashln!("{} Failed to connect (name={}, address={})", *helpers::FAIL, self.server_name, server.address),
};
} else {
crashln!("{} Server '{}' does not exist", *helpers::FAIL, self.server_name,)
};
}
then!(!silent, println!("{} Creating {}process with ({name})", *helpers::SUCCESS, self.kind));
then!(!silent, println!("{} {}Created ({name}) ✓", *helpers::SUCCESS, self.kind));
return self.runner;
}
- pub fn restart(mut self, name: &Option<String>, watch: &Option<String>, silent: bool) -> Runner {
+ pub fn restart(mut self, name: &Option<String>, watch: &Option<String>, reset_env: bool, silent: bool) -> Runner {
then!(!silent, println!("{} Applying {}action restartProcess on ({})", *helpers::SUCCESS, self.kind, self.id));
if matches!(self.server_name, "internal" | "local") {
let mut item = self.runner.get(self.id);
match watch {
Some(path) => item.watch(path),
None => item.disable_watch(),
}
+ then!(reset_env, item.clear_env());
+
name.as_ref().map(|n| item.rename(n.trim().replace("\n", "")));
item.restart();
self.runner = item.get_runner().clone();
} else {
let Some(servers) = config::servers().servers else {
crashln!("{} Failed to read servers", *helpers::FAIL)
};
if let Some(server) = servers.get(self.server_name) {
match Runner::connect(self.server_name.into(), server.get(), false) {
Some(remote) => {
let mut item = remote.get(self.id);
+ then!(reset_env, item.clear_env());
+
name.as_ref().map(|n| item.rename(n.trim().replace("\n", "")));
item.restart();
}
None => crashln!("{} Failed to connect (name={}, address={})", *helpers::FAIL, self.server_name, server.address),
}
} else {
crashln!("{} Server '{}' does not exist", *helpers::FAIL, self.server_name)
};
}
if !silent {
println!("{} Restarted {}({}) ✓", *helpers::SUCCESS, self.kind, self.id);
log!("process started (id={})", self.id);
}
return self.runner;
}
pub fn stop(mut self, silent: bool) -> Runner {
then!(!silent, println!("{} Applying {}action stopProcess on ({})", *helpers::SUCCESS, self.kind, self.id));
if !matches!(self.server_name, "internal" | "local") {
let Some(servers) = config::servers().servers else {
crashln!("{} Failed to read servers", *helpers::FAIL)
};
if let Some(server) = servers.get(self.server_name) {
self.runner = match Runner::connect(self.server_name.into(), server.get(), false) {
Some(remote) => remote,
None => crashln!("{} Failed to connect (name={}, address={})", *helpers::FAIL, self.server_name, server.address),
};
} else {
crashln!("{} Server '{}' does not exist", *helpers::FAIL, self.server_name)
};
}
let mut item = self.runner.get(self.id);
item.stop();
self.runner = item.get_runner().clone();
if !silent {
println!("{} Stopped {}({}) ✓", *helpers::SUCCESS, self.kind, self.id);
log!("process stopped {}(id={})", self.kind, self.id);
}
return self.runner;
}
pub fn remove(mut self) {
println!("{} Applying {}action removeProcess on ({})", *helpers::SUCCESS, self.kind, self.id);
if !matches!(self.server_name, "internal" | "local") {
let Some(servers) = config::servers().servers else {
crashln!("{} Failed to read servers", *helpers::FAIL)
};
if let Some(server) = servers.get(self.server_name) {
self.runner = match Runner::connect(self.server_name.into(), server.get(), false) {
Some(remote) => remote,
None => crashln!("{} Failed to remove (name={}, address={})", *helpers::FAIL, self.server_name, server.address),
};
} else {
crashln!("{} Server '{}' does not exist", *helpers::FAIL, self.server_name)
};
}
self.runner.remove(self.id);
println!("{} Removed {}({}) ✓", *helpers::SUCCESS, self.kind, self.id);
log!("process removed (id={})", self.id);
}
pub fn flush(&mut self) {
println!("{} Applying {}action flushLogs on ({})", *helpers::SUCCESS, self.kind, self.id);
if !matches!(self.server_name, "internal" | "local") {
let Some(servers) = config::servers().servers else {
crashln!("{} Failed to read servers", *helpers::FAIL)
};
if let Some(server) = servers.get(self.server_name) {
self.runner = match Runner::connect(self.server_name.into(), server.get(), false) {
Some(remote) => remote,
None => crashln!("{} Failed to remove (name={}, address={})", *helpers::FAIL, self.server_name, server.address),
};
} else {
crashln!("{} Server '{}' does not exist", *helpers::FAIL, self.server_name)
};
}
self.runner.flush(self.id);
println!("{} Flushed Logs {}({}) ✓", *helpers::SUCCESS, self.kind, self.id);
log!("process logs cleaned (id={})", self.id);
}
pub fn info(&self, format: &String) {
#[derive(Clone, Debug, Tabled)]
struct Info {
#[tabled(rename = "error log path ")]
log_error: String,
#[tabled(rename = "out log path")]
log_out: String,
#[tabled(rename = "cpu percent")]
cpu_percent: String,
#[tabled(rename = "memory usage")]
memory_usage: String,
#[tabled(rename = "path hash")]
hash: String,
#[tabled(rename = "watching")]
watch: String,
children: String,
#[tabled(rename = "exec cwd")]
path: String,
#[tabled(rename = "script command ")]
command: String,
#[tabled(rename = "script id")]
id: String,
restarts: u64,
uptime: String,
pid: String,
name: String,
status: ColoredString,
}
impl Serialize for Info {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let trimmed_json = json!({
"id": &self.id.trim(),
"pid": &self.pid.trim(),
"name": &self.name.trim(),
"path": &self.path.trim(),
"restarts": &self.restarts,
"hash": &self.hash.trim(),
"watch": &self.watch.trim(),
"children": &self.children,
"uptime": &self.uptime.trim(),
"status": &self.status.0.trim(),
"log_out": &self.log_out.trim(),
"cpu": &self.cpu_percent.trim(),
"command": &self.command.trim(),
"mem": &self.memory_usage.trim(),
"log_error": &self.log_error.trim(),
});
trimmed_json.serialize(serializer)
}
}
let render_info = |data: Vec<Info>| {
let table = Table::new(data.clone())
.with(Rotate::Left)
.with(Style::rounded().remove_horizontals())
.with(Colorization::exact([Color::FG_CYAN], Columns::first()))
.with(BorderColor::filled(Color::FG_BRIGHT_BLACK))
.to_string();
if let Ok(json) = serde_json::to_string(&data[0]) {
match format.as_str() {
"raw" => println!("{:?}", data[0]),
"json" => println!("{json}"),
_ => {
println!("{}\n{table}\n", format!("Describing {}process with id ({})", self.kind, self.id).on_bright_white().black());
println!(" {}", format!("Use `pmc logs {} [--lines <num>]` to display logs", self.id).white());
println!(" {}", format!("Use `pmc env {}` to display environment variables", self.id).white());
}
};
};
};
if matches!(self.server_name, "internal" | "local") {
if let Some(home) = home::home_dir() {
let config = config::read().runner;
let mut runner = Runner::new();
let item = runner.process(self.id);
let mut memory_usage: Option<MemoryInfo> = None;
let mut cpu_percent: Option<f32> = None;
let path = file::make_relative(&item.path, &home).to_string_lossy().into_owned();
let children = if item.children.is_empty() { "none".to_string() } else { format!("{:?}", item.children) };
if let Ok(mut process) = Process::new(item.pid as u32) {
memory_usage = process.memory_info().ok();
cpu_percent = process.cpu_percent().ok();
}
let cpu_percent = match cpu_percent {
Some(percent) => format!("{:.2}%", percent),
None => string!("0%"),
};
let memory_usage = match memory_usage {
Some(usage) => helpers::format_memory(usage.rss()),
None => string!("0b"),
};
let status = if item.running {
"online ".green().bold()
} else {
match item.crash.crashed {
true => "crashed ",
false => "stopped ",
}
.red()
.bold()
};
let data = vec![Info {
children,
cpu_percent,
memory_usage,
id: string!(self.id),
restarts: item.restarts,
name: item.name.clone(),
log_out: item.logs().out,
path: format!("{} ", path),
log_error: item.logs().error,
status: ColoredString(status),
pid: ternary!(item.running, format!("{}", item.pid), string!("n/a")),
command: format!("{} {} '{}'", config.shell, config.args.join(" "), item.script),
hash: ternary!(item.watch.enabled, format!("{} ", item.watch.hash), string!("none ")),
watch: ternary!(item.watch.enabled, format!("{path}/{} ", item.watch.path), string!("disabled ")),
uptime: ternary!(item.running, format!("{}", helpers::format_duration(item.started)), string!("none")),
}];
render_info(data)
} else {
crashln!("{} Impossible to get your home directory", *helpers::FAIL);
}
} else {
let data: (pmc::process::Process, Runner);
let Some(servers) = config::servers().servers else {
crashln!("{} Failed to read servers", *helpers::FAIL)
};
if let Some(server) = servers.get(self.server_name) {
data = match Runner::connect(self.server_name.into(), server.get(), false) {
Some(mut remote) => (remote.process(self.id).clone(), remote),
None => crashln!("{} Failed to connect (name={}, address={})", *helpers::FAIL, self.server_name, server.address),
};
} else {
crashln!("{} Server '{}' does not exist", *helpers::FAIL, self.server_name)
};
let (item, remote) = data;
let remote = remote.remote.unwrap();
let info = http::info(&remote, self.id);
let path = item.path.to_string_lossy().into_owned();
let status = if item.running {
"online ".green().bold()
} else {
match item.crash.crashed {
true => "crashed ",
false => "stopped ",
}
.red()
.bold()
};
if let Ok(info) = info {
let stats = info.json::<ItemSingle>().unwrap().stats;
let children = if item.children.is_empty() { "none".to_string() } else { format!("{:?}", item.children) };
let cpu_percent = match stats.cpu_percent {
Some(percent) => format!("{percent:.2}%"),
None => string!("0%"),
};
let memory_usage = match stats.memory_usage {
Some(usage) => helpers::format_memory(usage.rss),
None => string!("0b"),
};
let data = vec![Info {
children,
cpu_percent,
memory_usage,
id: string!(self.id),
path: path.clone(),
status: status.into(),
restarts: item.restarts,
name: item.name.clone(),
pid: ternary!(item.running, format!("{pid}", pid = item.pid), string!("n/a")),
log_out: format!("{}/{}-out.log", remote.config.log_path, item.name),
log_error: format!("{}/{}-error.log", remote.config.log_path, item.name),
hash: ternary!(item.watch.enabled, format!("{} ", item.watch.hash), string!("none ")),
command: format!("{} {} '{}'", remote.config.shell, remote.config.args.join(" "), item.script),
watch: ternary!(item.watch.enabled, format!("{path}/{} ", item.watch.path), string!("disabled ")),
uptime: ternary!(item.running, format!("{}", helpers::format_duration(item.started)), string!("none")),
}];
render_info(data)
}
}
}
pub fn logs(mut self, lines: &usize) {
if !matches!(self.server_name, "internal" | "local") {
let Some(servers) = config::servers().servers else {
crashln!("{} Failed to read servers", *helpers::FAIL)
};
if let Some(server) = servers.get(self.server_name) {
self.runner = match Runner::connect(self.server_name.into(), server.get(), false) {
Some(remote) => remote,
None => crashln!("{} Failed to connect (name={}, address={})", *helpers::FAIL, self.server_name, server.address),
};
} else {
crashln!("{} Server '{}' does not exist", *helpers::FAIL, self.server_name)
};
let item = self.runner.info(self.id).unwrap_or_else(|| crashln!("{} Process ({}) not found", *helpers::FAIL, self.id));
println!(
"{}",
format!("Showing last {lines} lines for {}process [{}] (change the value with --lines option)", self.kind, self.id).yellow()
);
for kind in vec!["error", "out"] {
let logs = http::logs(&self.runner.remote.as_ref().unwrap(), self.id, kind);
if let Ok(log) = logs {
if log.lines.is_empty() {
println!("{} No logs found for {}/{kind}", *helpers::FAIL, item.name);
continue;
}
file::logs_internal(log.lines, *lines, log.path, self.id, kind, &item.name)
}
}
} else {
let item = self.runner.info(self.id).unwrap_or_else(|| crashln!("{} Process ({}) not found", *helpers::FAIL, self.id));
println!(
"{}",
format!("Showing last {lines} lines for {}process [{}] (change the value with --lines option)", self.kind, self.id).yellow()
);
file::logs(item, *lines, "error");
file::logs(item, *lines, "out");
}
}
pub fn env(mut self) {
println!("{}", format!("Showing env for {}process {}:\n", self.kind, self.id).bright_yellow());
if !matches!(self.server_name, "internal" | "local") {
let Some(servers) = config::servers().servers else {
crashln!("{} Failed to read servers", *helpers::FAIL)
};
if let Some(server) = servers.get(self.server_name) {
self.runner = match Runner::connect(self.server_name.into(), server.get(), false) {
Some(remote) => remote,
None => crashln!("{} Failed to connect (name={}, address={})", *helpers::FAIL, self.server_name, server.address),
};
} else {
crashln!("{} Server '{}' does not exist", *helpers::FAIL, self.server_name)
};
}
let item = self.runner.process(self.id);
item.env.iter().for_each(|(key, value)| println!("{}: {}", key, value.green()));
}
pub fn save(server_name: &String) {
if !matches!(&**server_name, "internal" | "local") {
crashln!("{} Cannot force save on remote servers", *helpers::FAIL)
}
println!("{} Saved current processes to dumpfile", *helpers::SUCCESS);
Runner::new().save();
}
pub fn restore(server_name: &String) {
let mut runner = Runner::new();
let (kind, list_name) = super::format(server_name);
if !matches!(&**server_name, "internal" | "local") {
crashln!("{} Cannot restore on remote servers", *helpers::FAIL)
}
Runner::new().list().for_each(|(id, p)| {
if p.running == true {
runner = Internal {
id: *id,
server_name,
kind: kind.clone(),
runner: runner.clone(),
}
- .restart(&None, &None, true);
+ .restart(&None, &None, false, true);
}
});
println!("{} Restored process statuses from dumpfile", *helpers::SUCCESS);
Internal::list(&string!("default"), &list_name);
}
pub fn list(format: &String, server_name: &String) {
let render_list = |runner: &mut Runner, internal: bool| {
let mut processes: Vec<ProcessItem> = Vec::new();
#[derive(Tabled, Debug)]
struct ProcessItem {
id: ColoredString,
name: String,
pid: String,
uptime: String,
#[tabled(rename = "↺")]
restarts: String,
status: ColoredString,
cpu: String,
mem: String,
#[tabled(rename = "watching")]
watch: String,
}
impl serde::Serialize for ProcessItem {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let trimmed_json = json!({
"cpu": &self.cpu.trim(),
"mem": &self.mem.trim(),
"id": &self.id.0.trim(),
"pid": &self.pid.trim(),
"name": &self.name.trim(),
"watch": &self.watch.trim(),
"uptime": &self.uptime.trim(),
"status": &self.status.0.trim(),
"restarts": &self.restarts.trim(),
});
trimmed_json.serialize(serializer)
}
}
if runner.is_empty() {
println!("{} Process table empty", *helpers::SUCCESS);
} else {
for (id, item) in runner.items() {
let mut cpu_percent: String = string!("0%");
let mut memory_usage: String = string!("0b");
if internal {
let mut usage_internals: (Option<f32>, Option<MemoryInfo>) = (None, None);
if let Ok(mut process) = Process::new(item.pid as u32) {
usage_internals = (process.cpu_percent().ok(), process.memory_info().ok());
}
cpu_percent = match usage_internals.0 {
Some(percent) => format!("{:.0}%", percent),
None => string!("0%"),
};
memory_usage = match usage_internals.1 {
Some(usage) => helpers::format_memory(usage.rss()),
None => string!("0b"),
};
} else {
let info = http::info(&runner.remote.as_ref().unwrap(), id);
if let Ok(info) = info {
let stats = info.json::<ItemSingle>().unwrap().stats;
cpu_percent = match stats.cpu_percent {
Some(percent) => format!("{:.2}%", percent),
None => string!("0%"),
};
memory_usage = match stats.memory_usage {
Some(usage) => helpers::format_memory(usage.rss),
None => string!("0b"),
};
}
}
let status = if item.running {
"online ".green().bold()
} else {
match item.crash.crashed {
true => "crashed ",
false => "stopped ",
}
.red()
.bold()
};
processes.push(ProcessItem {
status: status.into(),
cpu: format!("{cpu_percent} "),
mem: format!("{memory_usage} "),
id: id.to_string().cyan().bold().into(),
restarts: format!("{} ", item.restarts),
name: format!("{} ", item.name.clone()),
pid: ternary!(item.running, format!("{} ", item.pid), string!("n/a ")),
watch: ternary!(item.watch.enabled, format!("{} ", item.watch.path), string!("disabled ")),
uptime: ternary!(item.running, format!("{} ", helpers::format_duration(item.started)), string!("none ")),
});
}
let table = Table::new(&processes)
.with(Style::rounded().remove_verticals())
.with(BorderColor::filled(Color::FG_BRIGHT_BLACK))
.with(Colorization::exact([Color::FG_BRIGHT_CYAN], Rows::first()))
.with(Modify::new(Columns::single(1)).with(Width::truncate(35).suffix("... ")))
.to_string();
if let Ok(json) = serde_json::to_string(&processes) {
match format.as_str() {
"raw" => println!("{:?}", processes),
"json" => println!("{json}"),
"default" => println!("{table}"),
_ => {}
};
};
}
};
if let Some(servers) = config::servers().servers {
let mut failed: Vec<(String, String)> = vec![];
if let Some(server) = servers.get(server_name) {
match Runner::connect(server_name.clone(), server.get(), true) {
Some(mut remote) => render_list(&mut remote, false),
None => println!("{} Failed to fetch (name={server_name}, address={})", *helpers::FAIL, server.address),
}
} else {
if matches!(&**server_name, "internal" | "all" | "global" | "local") {
if *server_name == "all" || *server_name == "global" {
println!("{} Internal daemon", *helpers::SUCCESS);
}
render_list(&mut Runner::new(), true);
} else {
crashln!("{} Server '{server_name}' does not exist", *helpers::FAIL);
}
}
if *server_name == "all" || *server_name == "global" {
for (name, server) in servers {
match Runner::connect(name.clone(), server.get(), true) {
Some(mut remote) => render_list(&mut remote, false),
None => failed.push((name, server.address)),
}
}
}
if !failed.is_empty() {
println!("{} Failed servers:", *helpers::FAIL);
failed
.iter()
.for_each(|server| println!(" {} {} {}", "-".yellow(), format!("{}", server.0), format!("[{}]", server.1).white()));
}
} else {
render_list(&mut Runner::new(), true);
}
}
}
diff --git a/src/cli/mod.rs b/src/cli/mod.rs
index a2e792d..facc74a 100644
--- a/src/cli/mod.rs
+++ b/src/cli/mod.rs
@@ -1,180 +1,178 @@
mod args;
pub use args::*;
pub(crate) mod import;
pub(crate) mod internal;
pub(crate) mod server;
use internal::Internal;
-use macros_rs::{crashln, string, ternary, then};
+use macros_rs::{crashln, string, ternary};
use pmc::{helpers, process::Runner};
use std::env;
pub(crate) fn format(server_name: &String) -> (String, String) {
let kind = ternary!(matches!(&**server_name, "internal" | "local"), "", "remote ").to_string();
return (kind, server_name.to_string());
}
pub fn get_version(short: bool) -> String {
return match short {
true => format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
false => match env!("GIT_HASH") {
"" => format!("{} ({}) [{}]", env!("CARGO_PKG_VERSION"), env!("BUILD_DATE"), env!("PROFILE")),
hash => format!("{} ({} {hash}) [{}]", env!("CARGO_PKG_VERSION"), env!("BUILD_DATE"), env!("PROFILE")),
},
};
}
pub fn start(name: &Option<String>, args: &Args, watch: &Option<String>, reset_env: &bool, server_name: &String) {
let mut runner = Runner::new();
let (kind, list_name) = format(server_name);
let arg = match args.get_string() {
Some(arg) => arg,
None => "",
};
if arg == "all" {
println!("{} Applying {kind}action startAllProcess", *helpers::SUCCESS);
let largest = runner.size();
match largest {
Some(largest) => (0..*largest + 1).for_each(|id| {
runner = Internal {
id,
server_name,
kind: kind.clone(),
runner: runner.clone(),
}
- .restart(&None, &None, true);
+ .restart(&None, &None, false, true);
}),
None => println!("{} Cannot start all, no processes found", *helpers::FAIL),
}
} else {
match args {
Args::Id(id) => {
- then!(*reset_env, runner.clear_env(*id));
- Internal { id: *id, runner, server_name, kind }.restart(name, watch, false);
+ Internal { id: *id, runner, server_name, kind }.restart(name, watch, *reset_env, false);
}
Args::Script(script) => match runner.find(&script, server_name) {
Some(id) => {
- then!(*reset_env, runner.clear_env(id));
- Internal { id, runner, server_name, kind }.restart(name, watch, false);
+ Internal { id, runner, server_name, kind }.restart(name, watch, *reset_env, false);
}
None => {
Internal { id: 0, runner, server_name, kind }.create(script, name, watch, false);
}
},
}
}
Internal::list(&string!("default"), &list_name);
}
pub fn stop(item: &Item, server_name: &String) {
let mut runner: Runner = Runner::new();
let (kind, list_name) = format(server_name);
let arg = match item.get_string() {
Some(arg) => arg,
None => "",
};
if arg == "all" {
println!("{} Applying {kind}action stopAllProcess", *helpers::SUCCESS);
let largest = runner.size();
match largest {
Some(largest) => (0..*largest + 1).for_each(|id| {
runner = Internal {
id,
server_name,
kind: kind.clone(),
runner: runner.clone(),
}
.stop(true);
}),
None => println!("{} Cannot stop all, no processes found", *helpers::FAIL),
}
} else {
match item {
Item::Id(id) => {
Internal { id: *id, runner, server_name, kind }.stop(false);
}
Item::Name(name) => match runner.find(&name, server_name) {
Some(id) => {
Internal { id, runner, server_name, kind }.stop(false);
}
None => crashln!("{} Process ({name}) not found", *helpers::FAIL),
},
}
}
Internal::list(&string!("default"), &list_name);
}
pub fn remove(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 }.remove(),
Item::Name(name) => match runner.find(&name, server_name) {
Some(id) => Internal { id, runner, server_name, kind }.remove(),
None => crashln!("{} Process ({name}) not found", *helpers::FAIL),
},
}
}
pub fn info(item: &Item, format: &String, server_name: &String) {
let runner: Runner = Runner::new();
let (kind, _) = self::format(server_name);
match item {
Item::Id(id) => Internal { id: *id, runner, server_name, kind }.info(format),
Item::Name(name) => match runner.find(&name, server_name) {
Some(id) => Internal { id, runner, server_name, kind }.info(format),
None => crashln!("{} Process ({name}) not found", *helpers::FAIL),
},
}
}
pub fn logs(item: &Item, lines: &usize, 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 }.logs(lines),
Item::Name(name) => match runner.find(&name, server_name) {
Some(id) => Internal { id, runner, server_name, kind }.logs(lines),
None => crashln!("{} Process ({name}) not found", *helpers::FAIL),
},
}
}
// combine into a single function that handles multiple
pub fn env(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 }.env(),
Item::Name(name) => match runner.find(&name, server_name) {
Some(id) => Internal { id, runner, server_name, kind }.env(),
None => crashln!("{} Process ({name}) not found", *helpers::FAIL),
},
}
}
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),
},
}
}
diff --git a/src/daemon/api/routes.rs b/src/daemon/api/routes.rs
index e5435de..b1b7db4 100644
--- a/src/daemon/api/routes.rs
+++ b/src/daemon/api/routes.rs
@@ -1,839 +1,844 @@
#![allow(non_snake_case)]
use chrono::{DateTime, Utc};
use global_placeholders::global;
use macros_rs::{fmtstr, string, ternary, then};
use prometheus::{Encoder, TextEncoder};
use psutil::process::{MemoryInfo, Process};
use reqwest::header::HeaderValue;
use serde::Deserialize;
use tera::{Context, Tera};
use utoipa::ToSchema;
use rocket::{
get,
http::{ContentType, Status},
post,
serde::{json::Json, Serialize},
State,
};
use super::{
helpers::{generic_error, not_found, GenericError, NotFound},
structs::ErrorMessage,
EnableWebUI, TeraState,
};
use pmc::{
config, file, helpers,
process::{dump, http::client, ItemSingle, ProcessItem, Runner},
};
use crate::daemon::{
api::{HTTP_COUNTER, HTTP_REQ_HISTOGRAM},
pid,
};
use std::{
collections::BTreeMap,
env,
fs::{self, File},
io::{self, BufRead, BufReader},
path::PathBuf,
};
pub(crate) struct Token;
type EnvList = Json<BTreeMap<String, String>>;
#[allow(dead_code)]
#[derive(ToSchema)]
#[schema(as = MemoryInfo)]
pub(crate) struct DocMemoryInfo {
rss: u64,
vms: u64,
#[cfg(target_os = "linux")]
shared: u64,
#[cfg(target_os = "linux")]
text: u64,
#[cfg(target_os = "linux")]
data: u64,
#[cfg(target_os = "macos")]
page_faults: u64,
#[cfg(target_os = "macos")]
pageins: u64,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub(crate) struct ActionBody {
#[schema(example = "restart")]
method: String,
}
#[derive(Serialize, ToSchema)]
pub(crate) struct ConfigBody {
#[schema(example = "bash")]
shell: String,
#[schema(min_items = 1, example = json!(["-c"]))]
args: Vec<String>,
#[schema(example = "/home/user/.pmc/logs")]
log_path: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub(crate) struct CreateBody {
#[schema(example = "app")]
name: Option<String>,
#[schema(example = "node index.js")]
script: String,
#[schema(value_type = String, example = "/projects/app")]
path: PathBuf,
#[schema(example = "src")]
watch: Option<String>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub(crate) struct ActionResponse {
#[schema(example = true)]
done: bool,
#[schema(example = "name")]
action: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub(crate) struct LogResponse {
logs: Vec<String>,
}
#[derive(Serialize, ToSchema)]
pub struct MetricsRoot {
pub version: Version,
pub daemon: Daemon,
}
#[derive(Serialize, ToSchema)]
pub struct Version {
#[schema(example = "v1.0.0")]
pub pkg: String,
pub hash: Option<&'static str>,
#[schema(example = "2000-01-01")]
pub build_date: &'static str,
#[schema(example = "release")]
pub target: &'static str,
}
#[derive(Serialize, ToSchema)]
pub struct Daemon {
pub pid: Option<i32>,
#[schema(example = true)]
pub running: bool,
pub uptime: String,
pub process_count: usize,
#[schema(example = "default")]
pub daemon_type: String,
pub stats: Stats,
}
#[derive(Serialize, ToSchema)]
pub struct Stats {
pub memory_usage: String,
pub cpu_percent: String,
}
fn attempt(done: bool, method: &str) -> ActionResponse {
ActionResponse {
done,
action: ternary!(done, Box::leak(Box::from(method)), "DOES_NOT_EXIST").to_string(),
}
}
fn render(name: &str, tmpl: &Tera, ctx: &Context) -> Result<String, NotFound> { tmpl.render(name, &ctx).or(Err(not_found("Page was not found"))) }
#[get("/")]
pub async fn dashboard(state: &State<TeraState>, _webui: EnableWebUI) -> Result<(ContentType, String), NotFound> {
let mut ctx = Context::new();
ctx.insert("base_path", &state.path);
let payload = render("dashboard", &state.tera, &ctx)?;
Ok((ContentType::HTML, payload))
}
#[get("/login")]
pub async fn login(state: &State<TeraState>, _webui: EnableWebUI) -> Result<(ContentType, String), NotFound> {
let mut ctx = Context::new();
ctx.insert("base_path", &state.path);
let payload = render("login", &state.tera, &ctx)?;
Ok((ContentType::HTML, payload))
}
#[get("/view/<id>")]
pub async fn view_process(id: usize, state: &State<TeraState>, _webui: EnableWebUI) -> Result<(ContentType, String), NotFound> {
let mut ctx = Context::new();
ctx.insert("base_path", &state.path);
ctx.insert("process_id", &id);
let payload = render("view", &state.tera, &ctx)?;
Ok((ContentType::HTML, payload))
}
#[get("/daemon/prometheus")]
#[utoipa::path(get, tag = "Daemon", path = "/daemon/prometheus", security((), ("api_key" = [])),
responses(
(
description = "Get prometheus metrics", body = String, status = 200,
example = json!("# HELP daemon_cpu_percentage The cpu usage graph of the daemon.\n# TYPE daemon_cpu_percentage histogram\ndaemon_cpu_percentage_bucket{le=\"0.005\"} 0"),
),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn prometheus_handler(_t: Token) -> String {
let encoder = TextEncoder::new();
let mut buffer = Vec::<u8>::new();
let metric_families = prometheus::gather();
encoder.encode(&metric_families, &mut buffer).unwrap();
String::from_utf8(buffer.clone()).unwrap()
}
#[get("/daemon/servers")]
#[utoipa::path(get, tag = "Daemon", path = "/daemon/servers", security((), ("api_key" = [])),
responses(
(status = 200, description = "Get daemon servers successfully", body = [String]),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn servers_handler(_t: Token) -> Result<Json<Vec<String>>, GenericError> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["servers"]).start_timer();
if let Some(servers) = config::servers().servers {
HTTP_COUNTER.inc();
timer.observe_duration();
Ok(Json(servers.into_keys().collect()))
} else {
Err(generic_error(Status::BadRequest, string!("No servers have been added")))
}
}
#[get("/remote/<name>/list")]
#[utoipa::path(get, tag = "Remote", path = "/remote/{name}/list", security((), ("api_key" = [])),
params(("name" = String, Path, description = "Name of remote daemon", example = "example"),),
responses(
(status = 200, description = "Get list from remote daemon successfully", body = [ProcessItem]),
(status = NOT_FOUND, description = "Remote daemon does not exist", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn remote_list(name: String, _t: Token) -> Result<Json<Vec<ProcessItem>>, GenericError> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["list"]).start_timer();
if let Some(servers) = config::servers().servers {
let (address, (client, headers)) = match servers.get(&name) {
Some(server) => (&server.address, client(&server.token).await),
None => return Err(generic_error(Status::NotFound, string!("Server was not found"))),
};
HTTP_COUNTER.inc();
timer.observe_duration();
match client.get(fmtstr!("{address}/list")).headers(headers).send().await {
Ok(data) => {
if data.status() != 200 {
let err = data.json::<ErrorMessage>().await.unwrap();
Err(generic_error(err.code, err.message))
} else {
Ok(Json(data.json::<Vec<ProcessItem>>().await.unwrap()))
}
}
Err(err) => Err(generic_error(Status::InternalServerError, err.to_string())),
}
} else {
Err(generic_error(Status::BadRequest, string!("No servers have been added")))
}
}
#[get("/remote/<name>/info/<id>")]
#[utoipa::path(get, tag = "Remote", path = "/remote/{name}/info/{id}", security((), ("api_key" = [])),
params(
("name" = String, Path, description = "Name of remote daemon", example = "example"),
("id" = usize, Path, description = "Process id to get information for", example = 0)
),
responses(
(status = 200, description = "Get process info from remote daemon successfully", body = [ProcessItem]),
(status = NOT_FOUND, description = "Remote daemon does not exist", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn remote_info(name: String, id: usize, _t: Token) -> Result<Json<ItemSingle>, GenericError> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["info"]).start_timer();
if let Some(servers) = config::servers().servers {
let (address, (client, headers)) = match servers.get(&name) {
Some(server) => (&server.address, client(&server.token).await),
None => return Err(generic_error(Status::NotFound, string!("Server was not found"))),
};
HTTP_COUNTER.inc();
timer.observe_duration();
match client.get(fmtstr!("{address}/process/{id}/info")).headers(headers).send().await {
Ok(data) => {
if data.status() != 200 {
let err = data.json::<ErrorMessage>().await.unwrap();
Err(generic_error(err.code, err.message))
} else {
Ok(Json(data.json::<ItemSingle>().await.unwrap()))
}
}
Err(err) => Err(generic_error(Status::InternalServerError, err.to_string())),
}
} else {
Err(generic_error(Status::BadRequest, string!("No servers have been added")))
}
}
#[get("/remote/<name>/logs/<id>/<kind>")]
#[utoipa::path(get, tag = "Remote", path = "/remote/{name}/logs/{id}/{kind}", security((), ("api_key" = [])),
params(
("name" = String, Path, description = "Name of remote daemon", example = "example"),
("id" = usize, Path, description = "Process id to get information for", example = 0),
("kind" = String, Path, description = "Log output type", example = "out")
),
responses(
(status = 200, description = "Remote process logs of {type} fetched", body = LogResponse),
(status = NOT_FOUND, description = "Remote daemon does not exist", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn remote_logs(name: String, id: usize, kind: String, _t: Token) -> Result<Json<LogResponse>, GenericError> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["info"]).start_timer();
if let Some(servers) = config::servers().servers {
let (address, (client, headers)) = match servers.get(&name) {
Some(server) => (&server.address, client(&server.token).await),
None => return Err(generic_error(Status::NotFound, string!("Server was not found"))),
};
HTTP_COUNTER.inc();
timer.observe_duration();
match client.get(fmtstr!("{address}/process/{id}/logs/{kind}")).headers(headers).send().await {
Ok(data) => {
if data.status() != 200 {
let err = data.json::<ErrorMessage>().await.unwrap();
Err(generic_error(err.code, err.message))
} else {
Ok(Json(data.json::<LogResponse>().await.unwrap()))
}
}
Err(err) => Err(generic_error(Status::InternalServerError, err.to_string())),
}
} else {
Err(generic_error(Status::BadRequest, string!("No servers have been added")))
}
}
#[post("/remote/<name>/rename/<id>", format = "text", data = "<body>")]
#[utoipa::path(post, tag = "Remote", path = "/remote/{name}/rename/{id}",
security((), ("api_key" = [])),
request_body(content = String, example = json!("example_name")),
params(
("id" = usize, Path, description = "Process id to rename", example = 0),
("name" = String, Path, description = "Name of remote daemon", example = "example"),
),
responses(
(
description = "Remote rename process successful", body = ActionResponse,
example = json!({"action": "rename", "done": true }), status = 200,
),
(status = NOT_FOUND, description = "Process was not found", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn remote_rename(name: String, id: usize, body: String, _t: Token) -> Result<Json<ActionResponse>, GenericError> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["rename"]).start_timer();
if let Some(servers) = config::servers().servers {
let (address, (client, mut headers)) = match servers.get(&name) {
Some(server) => (&server.address, client(&server.token).await),
None => return Err(generic_error(Status::NotFound, string!("Server was not found"))),
};
HTTP_COUNTER.inc();
timer.observe_duration();
headers.insert("content-type", HeaderValue::from_static("text/plain"));
match client.post(fmtstr!("{address}/process/{id}/rename")).body(body).headers(headers).send().await {
Ok(data) => {
if data.status() != 200 {
let err = data.json::<ErrorMessage>().await.unwrap();
Err(generic_error(err.code, err.message))
} else {
Ok(Json(data.json::<ActionResponse>().await.unwrap()))
}
}
Err(err) => Err(generic_error(Status::InternalServerError, err.to_string())),
}
} else {
Err(generic_error(Status::BadRequest, string!("No servers have been added")))
}
}
#[post("/remote/<name>/action/<id>", format = "json", data = "<body>")]
#[utoipa::path(post, tag = "Remote", path = "/remote/{name}/action/{id}", request_body = ActionBody,
security((), ("api_key" = [])),
params(
("id" = usize, Path, description = "Process id to run action on", example = 0),
("name" = String, Path, description = "Name of remote daemon", example = "example")
),
responses(
(status = 200, description = "Run action on remote process successful", body = ActionResponse),
(status = NOT_FOUND, description = "Process/action was not found", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn remote_action(name: String, id: usize, body: Json<ActionBody>, _t: Token) -> Result<Json<ActionResponse>, GenericError> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["action"]).start_timer();
if let Some(servers) = config::servers().servers {
let (address, (client, headers)) = match servers.get(&name) {
Some(server) => (&server.address, client(&server.token).await),
None => return Err(generic_error(Status::NotFound, string!("Server was not found"))),
};
HTTP_COUNTER.inc();
timer.observe_duration();
match client.post(fmtstr!("{address}/process/{id}/action")).json(&body.0).headers(headers).send().await {
Ok(data) => {
if data.status() != 200 {
let err = data.json::<ErrorMessage>().await.unwrap();
Err(generic_error(err.code, err.message))
} else {
Ok(Json(data.json::<ActionResponse>().await.unwrap()))
}
}
Err(err) => Err(generic_error(Status::InternalServerError, err.to_string())),
}
} else {
Err(generic_error(Status::BadRequest, string!("No servers have been added")))
}
}
#[get("/daemon/dump")]
#[utoipa::path(get, tag = "Daemon", path = "/daemon/dump", security((), ("api_key" = [])),
responses(
(status = 200, description = "Dump processes successfully", body = [u8]),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn dump_handler(_t: Token) -> Vec<u8> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["dump"]).start_timer();
HTTP_COUNTER.inc();
timer.observe_duration();
dump::raw()
}
#[get("/daemon/config")]
#[utoipa::path(get, tag = "Daemon", path = "/daemon/config", security((), ("api_key" = [])),
responses(
(status = 200, description = "Get daemon config successfully", body = ConfigBody),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn config_handler(_t: Token) -> Json<ConfigBody> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["dump"]).start_timer();
let config = config::read().runner;
HTTP_COUNTER.inc();
timer.observe_duration();
Json(ConfigBody {
shell: config.shell,
args: config.args,
log_path: config.log_path,
})
}
#[get("/list")]
#[utoipa::path(get, path = "/list", tag = "Process", security((), ("api_key" = [])),
responses(
(status = 200, description = "List processes successfully", body = [ProcessItem]),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn list_handler(_t: Token) -> Json<Vec<ProcessItem>> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["list"]).start_timer();
let data = Runner::new().fetch();
HTTP_COUNTER.inc();
timer.observe_duration();
Json(data)
}
#[get("/process/<id>/logs/<kind>")]
#[utoipa::path(get, tag = "Process", path = "/process/{id}/logs/{kind}",
security((), ("api_key" = [])),
params(
("id" = usize, Path, description = "Process id to get logs for", example = 0),
("kind" = String, Path, description = "Log output type", example = "out")
),
responses(
(status = 200, description = "Process logs of {type} fetched", body = LogResponse),
(status = NOT_FOUND, description = "Process was not found", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn logs_handler(id: usize, kind: String, _t: Token) -> Result<Json<LogResponse>, NotFound> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["log"]).start_timer();
HTTP_COUNTER.inc();
match Runner::new().info(id) {
Some(item) => {
let log_file = match kind.as_str() {
"out" | "stdout" => item.logs().out,
"error" | "stderr" => item.logs().error,
_ => item.logs().out,
};
match File::open(log_file) {
Ok(data) => {
let reader = BufReader::new(data);
let logs: Vec<String> = reader.lines().collect::<io::Result<_>>().unwrap();
timer.observe_duration();
Ok(Json(LogResponse { logs }))
}
Err(_) => Ok(Json(LogResponse { logs: vec![] })),
}
}
None => {
timer.observe_duration();
Err(not_found("Process was not found"))
}
}
}
#[get("/process/<id>/logs/<kind>/raw")]
#[utoipa::path(get, tag = "Process", path = "/process/{id}/logs/{kind}/raw",
security((), ("api_key" = [])),
params(
("id" = usize, Path, description = "Process id to get logs for", example = 0),
("kind" = String, Path, description = "Log output type", example = "out")
),
responses(
(
description = "Process logs of {type} fetched raw", body = String, status = 200,
example = json!("# PATH path/of/file.log\nserver started on port 3000")
),
(status = NOT_FOUND, description = "Process was not found", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn logs_raw_handler(id: usize, kind: String, _t: Token) -> Result<String, NotFound> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["log"]).start_timer();
HTTP_COUNTER.inc();
match Runner::new().info(id) {
Some(item) => {
let log_file = match kind.as_str() {
"out" | "stdout" => item.logs().out,
"error" | "stderr" => item.logs().error,
_ => item.logs().out,
};
let data = match fs::read_to_string(&log_file) {
Ok(data) => format!("# PATH {log_file}\n{data}"),
Err(err) => err.to_string(),
};
timer.observe_duration();
Ok(data)
}
None => {
timer.observe_duration();
Err(not_found("Process was not found"))
}
}
}
#[get("/process/<id>/info")]
#[utoipa::path(get, tag = "Process", path = "/process/{id}/info", security((), ("api_key" = [])),
params(("id" = usize, Path, description = "Process id to get information for", example = 0)),
responses(
(status = 200, description = "Current process info retrieved", body = ItemSingle),
(status = NOT_FOUND, description = "Process was not found", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn info_handler(id: usize, _t: Token) -> Result<Json<ItemSingle>, NotFound> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["info"]).start_timer();
let runner = Runner::new();
if runner.exists(id) {
let item = runner.get(id);
HTTP_COUNTER.inc();
timer.observe_duration();
Ok(Json(item.fetch()))
} else {
Err(not_found("Process was not found"))
}
}
#[post("/process/create", format = "json", data = "<body>")]
#[utoipa::path(post, tag = "Process", path = "/process/create", request_body(content = CreateBody),
security((), ("api_key" = [])),
responses(
(
description = "Create process successful", body = ActionResponse,
example = json!({"action": "create", "done": true }), status = 200,
),
(status = INTERNAL_SERVER_ERROR, description = "Failed to create process", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn create_handler(body: Json<CreateBody>, _t: Token) -> Result<Json<ActionResponse>, ()> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["create"]).start_timer();
let mut runner = Runner::new();
HTTP_COUNTER.inc();
let name = match &body.name {
Some(name) => string!(name),
None => string!(body.script.split_whitespace().next().unwrap_or_default()),
};
runner.start(&name, &body.script, body.path.clone(), &body.watch).save();
timer.observe_duration();
Ok(Json(attempt(true, "create")))
}
#[post("/process/<id>/rename", format = "text", data = "<body>")]
#[utoipa::path(post, tag = "Process", path = "/process/{id}/rename",
security((), ("api_key" = [])),
request_body(content = String, example = json!("example_name")),
params(("id" = usize, Path, description = "Process id to rename", example = 0)),
responses(
(
description = "Rename process successful", body = ActionResponse,
example = json!({"action": "rename", "done": true }), status = 200,
),
(status = NOT_FOUND, description = "Process was not found", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn rename_handler(id: usize, body: String, _t: Token) -> Result<Json<ActionResponse>, NotFound> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["rename"]).start_timer();
let runner = Runner::new();
match runner.clone().info(id) {
Some(process) => {
HTTP_COUNTER.inc();
let mut item = runner.get(id);
item.rename(body.trim().replace("\n", ""));
then!(process.running, item.restart());
timer.observe_duration();
Ok(Json(attempt(true, "rename")))
}
None => {
timer.observe_duration();
Err(not_found("Process was not found"))
}
}
}
#[get("/process/<id>/env")]
#[utoipa::path(get, tag = "Process", path = "/process/{id}/env",
params(("id" = usize, Path, description = "Process id to fetch env from", example = 0)),
responses(
(
description = "Current process env", body = HashMap<String, String>,
example = json!({"ENV_TEST_VALUE": "example_value"}), status = 200
),
(status = NOT_FOUND, description = "Process was not found", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn env_handler(id: usize, _t: Token) -> Result<EnvList, NotFound> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["env"]).start_timer();
HTTP_COUNTER.inc();
match Runner::new().info(id) {
Some(item) => {
timer.observe_duration();
Ok(Json(item.clone().env))
}
None => {
timer.observe_duration();
Err(not_found("Process was not found"))
}
}
}
#[post("/process/<id>/action", format = "json", data = "<body>")]
#[utoipa::path(post, tag = "Process", path = "/process/{id}/action", request_body = ActionBody,
security((), ("api_key" = [])),
params(("id" = usize, Path, description = "Process id to run action on", example = 0)),
responses(
(status = 200, description = "Run action on process successful", body = ActionResponse),
(status = NOT_FOUND, description = "Process/action was not found", body = ErrorMessage),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn action_handler(id: usize, body: Json<ActionBody>, _t: Token) -> Result<Json<ActionResponse>, NotFound> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["action"]).start_timer();
let mut runner = Runner::new();
let method = body.method.as_str();
if runner.exists(id) {
HTTP_COUNTER.inc();
match method {
"start" | "restart" => {
runner.get(id).restart();
timer.observe_duration();
Ok(Json(attempt(true, method)))
}
"stop" | "kill" => {
runner.get(id).stop();
timer.observe_duration();
Ok(Json(attempt(true, method)))
}
+ "reset_env" | "clear_env" => {
+ runner.get(id).clear_env();
+ timer.observe_duration();
+ Ok(Json(attempt(true, method)))
+ }
"remove" | "delete" => {
runner.remove(id);
timer.observe_duration();
Ok(Json(attempt(true, method)))
}
"flush" | "clean" => {
runner.flush(id);
timer.observe_duration();
Ok(Json(attempt(true, method)))
}
_ => {
timer.observe_duration();
Err(not_found("Invalid action attempt"))
}
}
} else {
Err(not_found("Process was not found"))
}
}
#[get("/daemon/metrics")]
#[utoipa::path(get, tag = "Daemon", path = "/daemon/metrics", security((), ("api_key" = [])),
responses(
(status = 200, description = "Get daemon metrics", body = MetricsRoot),
(
status = UNAUTHORIZED, description = "Authentication failed or not provided", body = ErrorMessage,
example = json!({"code": 401, "message": "Unauthorized"})
)
)
)]
pub async fn metrics_handler(_t: Token) -> Json<MetricsRoot> {
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["metrics"]).start_timer();
let mut pid: Option<i32> = None;
let mut cpu_percent: Option<f32> = None;
let mut uptime: Option<DateTime<Utc>> = None;
let mut memory_usage: Option<MemoryInfo> = None;
let mut runner: Runner = file::read_object(global!("pmc.dump"));
HTTP_COUNTER.inc();
if pid::exists() {
if let Ok(process_id) = pid::read() {
if let Ok(mut process) = Process::new(process_id as u32) {
pid = Some(process_id);
uptime = Some(pid::uptime().unwrap());
memory_usage = process.memory_info().ok();
cpu_percent = process.cpu_percent().ok();
}
}
}
let memory_usage = match memory_usage {
Some(usage) => helpers::format_memory(usage.rss()),
None => string!("0b"),
};
let cpu_percent = match cpu_percent {
Some(percent) => format!("{:.2}%", percent),
None => string!("0%"),
};
let uptime = match uptime {
Some(uptime) => helpers::format_duration(uptime),
None => string!("none"),
};
timer.observe_duration();
Json(MetricsRoot {
version: Version {
target: env!("PROFILE"),
build_date: env!("BUILD_DATE"),
pkg: format!("v{}", env!("CARGO_PKG_VERSION")),
hash: ternary!(env!("GIT_HASH_FULL") == "", None, Some(env!("GIT_HASH_FULL"))),
},
daemon: Daemon {
pid,
uptime,
running: pid::exists(),
process_count: runner.count(),
daemon_type: global!("pmc.daemon.kind"),
stats: Stats { memory_usage, cpu_percent },
},
})
}
diff --git a/src/process/http.rs b/src/process/http.rs
index ffd04c1..25bf84c 100644
--- a/src/process/http.rs
+++ b/src/process/http.rs
@@ -1,109 +1,116 @@
use crate::process::Remote;
use macros_rs::{fmtstr, string};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::Client;
use serde::Serialize;
use std::path::PathBuf;
#[derive(Serialize)]
struct ActionBody {
pub method: String,
}
pub struct LogResponse {
pub path: &'static str,
pub lines: Vec<String>,
}
#[derive(Serialize)]
struct CreateBody<'c> {
pub name: &'c String,
pub script: &'c String,
pub path: PathBuf,
pub watch: &'c Option<String>,
}
pub mod sync {
use reqwest::blocking::Client;
use reqwest::header::{HeaderMap, HeaderValue};
pub use reqwest::blocking::Response;
pub fn client(token: &Option<String>) -> (Client, HeaderMap) {
let client = Client::new();
let mut headers = HeaderMap::new();
if let Some(token) = token {
headers.insert("token", HeaderValue::from_str(&token).unwrap());
}
return (client, headers);
}
}
pub async fn client(token: &Option<String>) -> (Client, HeaderMap) {
let client = Client::new();
let mut headers = HeaderMap::new();
if let Some(token) = token {
headers.insert("token", HeaderValue::from_str(&token).unwrap());
}
return (client, headers);
}
pub fn info(Remote { address, token, .. }: &Remote, id: usize) -> Result<sync::Response, anyhow::Error> {
let (client, headers) = sync::client(token);
Ok(client.get(fmtstr!("{address}/process/{id}/info")).headers(headers).send()?)
}
pub fn logs(Remote { address, token, .. }: &Remote, id: usize, kind: &str) -> Result<LogResponse, anyhow::Error> {
let (client, headers) = sync::client(token);
let response = client.get(fmtstr!("{address}/process/{id}/logs/{kind}/raw")).headers(headers).send()?;
let log = response.text()?;
Ok(LogResponse {
lines: log.lines().skip(1).map(|line| line.to_string()).collect::<Vec<String>>(),
path: Box::leak(Box::from(log.lines().next().unwrap_or("").split_whitespace().last().unwrap_or(""))),
})
}
pub fn create(Remote { address, token, .. }: &Remote, name: &String, script: &String, path: PathBuf, watch: &Option<String>) -> Result<sync::Response, anyhow::Error> {
let (client, headers) = sync::client(token);
let content = CreateBody { name, script, path, watch };
Ok(client.post(fmtstr!("{address}/process/create")).json(&content).headers(headers).send()?)
}
pub fn restart(Remote { address, token, .. }: &Remote, id: usize) -> Result<sync::Response, anyhow::Error> {
let (client, headers) = sync::client(token);
let content = ActionBody { method: string!("restart") };
Ok(client.post(fmtstr!("{address}/process/{id}/action")).json(&content).headers(headers).send()?)
}
pub fn rename(Remote { address, token, .. }: &Remote, id: usize, name: String) -> Result<sync::Response, anyhow::Error> {
let (client, headers) = sync::client(token);
Ok(client.post(fmtstr!("{address}/process/{id}/rename")).body(name).headers(headers).send()?)
}
// merge into one function
pub fn stop(Remote { address, token, .. }: &Remote, id: usize) -> Result<sync::Response, anyhow::Error> {
let (client, headers) = sync::client(token);
let content = ActionBody { method: string!("stop") };
Ok(client.post(fmtstr!("{address}/process/{id}/action")).json(&content).headers(headers).send()?)
}
pub fn remove(Remote { address, token, .. }: &Remote, id: usize) -> Result<sync::Response, anyhow::Error> {
let (client, headers) = sync::client(token);
let content = ActionBody { method: string!("remove") };
Ok(client.post(fmtstr!("{address}/process/{id}/action")).json(&content).headers(headers).send()?)
}
pub fn flush(Remote { address, token, .. }: &Remote, id: usize) -> Result<sync::Response, anyhow::Error> {
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()?)
}
+
+pub fn clear_env(Remote { address, token, .. }: &Remote, id: usize) -> Result<sync::Response, anyhow::Error> {
+ let (client, headers) = sync::client(token);
+ let content = ActionBody { method: string!("clear_env") };
+
+ Ok(client.post(fmtstr!("{address}/process/{id}/action")).json(&content).headers(headers).send()?)
+}
diff --git a/src/process/mod.rs b/src/process/mod.rs
index 64e1fa4..d3e2c36 100644
--- a/src/process/mod.rs
+++ b/src/process/mod.rs
@@ -1,649 +1,656 @@
mod unix;
use crate::{
config,
config::structs::Server,
file, helpers,
service::{run, stop, ProcessMetadata},
};
use std::{
env,
fs::File,
path::PathBuf,
sync::{Arc, Mutex},
};
use nix::{
sys::signal::{kill, Signal},
unistd::Pid,
};
use chrono::serde::ts_milliseconds;
use chrono::{DateTime, Utc};
use global_placeholders::global;
use macros_rs::{crashln, string, ternary, then};
use psutil::process;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use utoipa::ToSchema;
#[derive(Serialize, Deserialize, ToSchema)]
pub struct ItemSingle {
pub info: Info,
pub stats: Stats,
pub watch: Watch,
pub log: Log,
pub raw: Raw,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct Info {
pub id: usize,
pub pid: i64,
pub name: String,
pub status: String,
#[schema(value_type = String, example = "/path")]
pub path: PathBuf,
pub uptime: String,
pub command: String,
pub children: Vec<i64>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct Stats {
pub restarts: u64,
pub start_time: i64,
pub cpu_percent: Option<f32>,
pub memory_usage: Option<MemoryInfo>,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct MemoryInfo {
pub rss: u64,
pub vms: u64,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct Log {
pub out: String,
pub error: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct Raw {
pub running: bool,
pub crashed: bool,
pub crashes: u64,
}
#[derive(Clone)]
pub struct LogInfo {
pub out: String,
pub error: String,
}
#[derive(Serialize, Deserialize, ToSchema)]
pub struct ProcessItem {
pid: i64,
id: usize,
cpu: String,
mem: String,
name: String,
restarts: u64,
status: String,
uptime: String,
#[schema(example = "/path")]
watch_path: String,
#[schema(value_type = String, example = "2000-01-01T01:00:00.000Z")]
start_time: DateTime<Utc>,
}
#[derive(Clone)]
pub struct ProcessWrapper {
pub id: usize,
pub runner: Arc<Mutex<Runner>>,
}
pub type Env = BTreeMap<String, String>;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Process {
pub id: usize,
pub pid: i64,
pub env: Env,
pub name: String,
pub path: PathBuf,
pub script: String,
pub restarts: u64,
pub running: bool,
pub crash: Crash,
pub watch: Watch,
pub children: Vec<i64>,
#[serde(with = "ts_milliseconds")]
pub started: DateTime<Utc>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Crash {
pub crashed: bool,
pub value: u64,
}
#[derive(Clone, Debug, Deserialize, Serialize, ToSchema)]
pub struct Watch {
pub enabled: bool,
#[schema(example = "/path")]
pub path: String,
pub hash: String,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Runner {
pub id: id::Id,
#[serde(skip)]
pub remote: Option<Remote>,
pub list: BTreeMap<usize, Process>,
}
#[derive(Clone, Debug)]
pub struct Remote {
address: String,
token: Option<String>,
pub config: RemoteConfig,
}
#[derive(Clone, Debug, Deserialize)]
pub struct RemoteConfig {
pub shell: String,
pub args: Vec<String>,
pub log_path: String,
}
pub enum Status {
Offline,
Running,
}
impl Status {
pub fn to_bool(&self) -> bool {
match self {
Status::Offline => false,
Status::Running => true,
}
}
}
macro_rules! lock {
($runner:expr) => {{
match $runner.lock() {
Ok(runner) => runner,
Err(err) => crashln!("Unable to lock mutex: {err}"),
}
}};
}
fn kill_children(children: Vec<i64>) {
for pid in children {
if let Err(err) = kill(Pid::from_raw(pid as i32), Signal::SIGTERM) {
log::error!("Failed to stop pid {pid}: {err:?}");
};
}
}
impl Runner {
pub fn new() -> Self { dump::read() }
pub fn connect(name: String, Server { address, token }: Server, verbose: bool) -> Option<Self> {
let remote_config = match config::from(&address, token.as_deref()) {
Ok(config) => config,
Err(err) => {
log::error!("{err}");
return None;
}
};
if let Ok(dump) = dump::from(&address, token.as_deref()) {
then!(verbose, println!("{} Fetched remote (name={name}, address={address})", *helpers::SUCCESS));
Some(Runner {
remote: Some(Remote {
token,
address: string!(address),
config: remote_config,
}),
..dump
})
} else {
None
}
}
pub fn start(&mut self, name: &String, command: &String, path: PathBuf, watch: &Option<String>) -> &mut Self {
if let Some(remote) = &self.remote {
if let Err(err) = http::create(remote, name, command, path, watch) {
crashln!("{} Failed to start create {name}\nError: {:#?}", *helpers::FAIL, err);
};
} else {
let id = self.id.next();
let config = config::read().runner;
let crash = Crash { crashed: false, value: 0 };
let watch = match watch {
Some(watch) => Watch {
enabled: true,
path: string!(watch),
hash: hash::create(file::cwd().join(watch)),
},
None => Watch {
enabled: false,
path: string!(""),
hash: string!(""),
},
};
let pid = run(ProcessMetadata {
args: config.args,
name: name.clone(),
shell: config.shell,
command: command.clone(),
log_path: config.log_path,
env: unix::env(),
});
self.list.insert(
id,
Process {
id,
pid,
path,
watch,
crash,
restarts: 0,
running: true,
children: vec![],
name: name.clone(),
started: Utc::now(),
script: command.clone(),
env: env::vars().collect(),
},
);
}
return self;
}
pub fn restart(&mut self, id: usize, dead: bool) -> &mut Self {
if let Some(remote) = &self.remote {
if let Err(err) = http::restart(remote, id) {
crashln!("{} Failed to start process {id}\nError: {:#?}", *helpers::FAIL, err);
};
} else {
let process = self.process(id);
let config = config::read().runner;
let Process { path, script, name, .. } = process.clone();
kill_children(process.children.clone());
stop(process.pid);
if let Err(err) = std::env::set_current_dir(&path) {
process.running = false;
process.children = vec![];
process.crash.crashed = true;
println!("{} Failed to set working directory {:?}\nError: {:#?}", *helpers::FAIL, path, err);
} else {
let mut temp_env = process.env.iter().map(|(key, value)| format!("{}={}", key, value)).collect::<Vec<String>>();
temp_env.extend(unix::env());
process.pid = run(ProcessMetadata {
args: config.args,
name: name.clone(),
shell: config.shell,
log_path: config.log_path,
command: script.to_string(),
env: temp_env,
});
process.running = true;
process.children = vec![];
process.started = Utc::now();
process.crash.crashed = false;
process.env.extend(env::vars().collect::<Env>());
then!(dead, process.restarts += 1);
then!(dead, process.crash.value += 1);
then!(!dead, process.crash.value = 0);
}
}
return self;
}
pub fn remove(&mut self, id: usize) {
if let Some(remote) = &self.remote {
if let Err(err) = http::remove(remote, id) {
crashln!("{} Failed to stop remove {id}\nError: {:#?}", *helpers::FAIL, err);
};
} else {
self.stop(id);
self.list.remove(&id);
self.save();
}
}
pub fn set_id(&mut self, id: id::Id) {
self.id = id;
self.id.next();
self.save();
}
pub fn set_status(&mut self, id: usize, status: Status) {
self.process(id).running = status.to_bool();
self.save();
}
pub fn items(&self) -> BTreeMap<usize, Process> { self.list.clone() }
pub fn items_mut(&mut self) -> &mut BTreeMap<usize, Process> { &mut self.list }
pub fn save(&self) { then!(self.remote.is_none(), dump::write(&self)) }
pub fn count(&mut self) -> usize { self.list().count() }
pub fn is_empty(&self) -> bool { self.list.is_empty() }
pub fn exists(&self, id: usize) -> bool { self.list.contains_key(&id) }
pub fn info(&self, id: usize) -> Option<&Process> { self.list.get(&id) }
pub fn size(&self) -> Option<&usize> { self.list.iter().map(|(k, _)| k).max() }
pub fn list<'l>(&'l mut self) -> impl Iterator<Item = (&'l usize, &'l mut Process)> { self.list.iter_mut().map(|(k, v)| (k, v)) }
pub fn process(&mut self, id: usize) -> &mut Process { self.list.get_mut(&id).unwrap_or_else(|| crashln!("{} Process ({id}) not found", *helpers::FAIL)) }
pub fn pid(&self, id: usize) -> i64 { self.list.get(&id).unwrap_or_else(|| crashln!("{} Process ({id}) not found", *helpers::FAIL)).pid }
pub fn get(self, id: usize) -> ProcessWrapper {
ProcessWrapper {
id,
runner: Arc::new(Mutex::new(self)),
}
}
pub fn set_crashed(&mut self, id: usize) -> &mut Self {
self.process(id).crash.crashed = true;
return self;
}
pub fn set_env(&mut self, id: usize, env: Env) -> &mut Self {
self.process(id).env.extend(env);
return self;
}
pub fn clear_env(&mut self, id: usize) -> &mut Self {
- self.process(id).env = BTreeMap::new();
+ if let Some(remote) = &self.remote {
+ if let Err(err) = http::clear_env(remote, id) {
+ crashln!("{} Failed to clear environment on {id}\nError: {:#?}", *helpers::FAIL, err);
+ };
+ } else {
+ self.process(id).env = BTreeMap::new();
+ }
+
return self;
}
pub fn set_children(&mut self, id: usize, children: Vec<i64>) -> &mut Self {
self.process(id).children = children;
return self;
}
pub fn new_crash(&mut self, id: usize) -> &mut Self {
self.process(id).crash.value += 1;
return self;
}
pub fn stop(&mut self, id: usize) -> &mut Self {
if let Some(remote) = &self.remote {
if let Err(err) = http::stop(remote, id) {
crashln!("{} Failed to stop process {id}\nError: {:#?}", *helpers::FAIL, err);
};
} else {
let process = self.process(id);
kill_children(process.children.clone());
stop(process.pid);
process.running = false;
process.crash.crashed = false;
process.crash.value = 0;
process.children = vec![];
}
return self;
}
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 {
self.process(id).logs().flush();
}
return self;
}
pub fn rename(&mut self, id: usize, name: String) -> &mut Self {
if let Some(remote) = &self.remote {
if let Err(err) = http::rename(remote, id, name) {
crashln!("{} Failed to rename process {id}\nError: {:#?}", *helpers::FAIL, err);
};
} else {
self.process(id).name = name;
}
return self;
}
pub fn watch(&mut self, id: usize, path: &str, enabled: bool) -> &mut Self {
let process = self.process(id);
process.watch = Watch {
enabled,
path: string!(path),
hash: ternary!(enabled, hash::create(process.path.join(path)), string!("")),
};
return self;
}
pub fn find(&self, name: &str, server_name: &String) -> Option<usize> {
let mut runner = self.clone();
if !matches!(&**server_name, "internal" | "local") {
let Some(servers) = config::servers().servers else {
crashln!("{} Failed to read servers", *helpers::FAIL)
};
if let Some(server) = servers.get(server_name) {
runner = match Runner::connect(server_name.clone(), server.get(), false) {
Some(remote) => remote,
None => crashln!("{} Failed to connect (name={server_name}, address={})", *helpers::FAIL, server.address),
};
} else {
crashln!("{} Server '{server_name}' does not exist", *helpers::FAIL)
};
}
runner.list.iter().find(|(_, p)| p.name == name).map(|(id, _)| *id)
}
pub fn fetch(&self) -> Vec<ProcessItem> {
let mut processes: Vec<ProcessItem> = Vec::new();
for (id, item) in self.items() {
let mut memory_usage: Option<MemoryInfo> = None;
let mut cpu_percent: Option<f32> = None;
if let Ok(mut process) = process::Process::new(item.pid as u32) {
let mem_info_psutil = process.memory_info().ok();
cpu_percent = process.cpu_percent().ok();
memory_usage = Some(MemoryInfo {
rss: mem_info_psutil.as_ref().unwrap().rss(),
vms: mem_info_psutil.as_ref().unwrap().vms(),
});
}
let cpu_percent = match cpu_percent {
Some(percent) => format!("{:.2}%", percent),
None => string!("0.00%"),
};
let memory_usage = match memory_usage {
Some(usage) => helpers::format_memory(usage.rss),
None => string!("0b"),
};
let status = if item.running {
string!("online")
} else {
match item.crash.crashed {
true => string!("crashed"),
false => string!("stopped"),
}
};
processes.push(ProcessItem {
id,
status,
pid: item.pid,
cpu: cpu_percent,
mem: memory_usage,
restarts: item.restarts,
name: item.name.clone(),
start_time: item.started,
watch_path: item.watch.path.clone(),
uptime: helpers::format_duration(item.started),
});
}
return processes;
}
}
impl LogInfo {
pub fn flush(&self) {
if let Err(err) = File::create(&self.out) {
log::debug!("{err}");
crashln!("{} Failed to purge logs (path={})", *helpers::FAIL, self.error);
}
if let Err(err) = File::create(&self.error) {
log::debug!("{err}");
crashln!("{} Failed to purge logs (path={})", *helpers::FAIL, self.error);
}
}
}
impl Process {
/// Get a log paths of the process item
pub fn logs(&self) -> LogInfo {
let name = self.name.replace(" ", "_");
LogInfo {
out: global!("pmc.logs.out", name.as_str()),
error: global!("pmc.logs.error", name.as_str()),
}
}
}
impl ProcessWrapper {
/// Stop the process item
pub fn stop(&mut self) { lock!(self.runner).stop(self.id).save(); }
/// Restart the process item
pub fn restart(&mut self) { lock!(self.runner).restart(self.id, false).save(); }
/// Rename the process item
pub fn rename(&mut self, name: String) { lock!(self.runner).rename(self.id, name).save(); }
/// Enable watching a path on the process item
pub fn watch(&mut self, path: &str) { lock!(self.runner).watch(self.id, path, true).save(); }
/// Disable watching on the process item
pub fn disable_watch(&mut self) { lock!(self.runner).watch(self.id, "", false).save(); }
/// Set the process item as crashed
pub fn crashed(&mut self) { lock!(self.runner).restart(self.id, true).save(); }
/// Get the borrowed runner reference (lives till program end)
pub fn get_runner(&mut self) -> &Runner { Box::leak(Box::new(lock!(self.runner))) }
/// Append new environment values to the process item
pub fn set_env(&mut self, env: Env) { lock!(self.runner).set_env(self.id, env).save(); }
/// Clear environment values of the process item
pub fn clear_env(&mut self) { lock!(self.runner).clear_env(self.id).save(); }
/// Get a json dump of the process item
pub fn fetch(&self) -> ItemSingle {
let mut runner = lock!(self.runner);
let item = runner.process(self.id);
let config = config::read().runner;
let mut memory_usage: Option<MemoryInfo> = None;
let mut cpu_percent: Option<f32> = None;
if let Ok(mut process) = process::Process::new(item.pid as u32) {
let mem_info_psutil = process.memory_info().ok();
cpu_percent = process.cpu_percent().ok();
memory_usage = Some(MemoryInfo {
rss: mem_info_psutil.as_ref().unwrap().rss(),
vms: mem_info_psutil.as_ref().unwrap().vms(),
});
}
let status = if item.running {
string!("online")
} else {
match item.crash.crashed {
true => string!("crashed"),
false => string!("stopped"),
}
};
ItemSingle {
info: Info {
status,
id: item.id,
pid: item.pid,
name: item.name.clone(),
path: item.path.clone(),
children: item.children.clone(),
uptime: helpers::format_duration(item.started),
command: format!("{} {} '{}'", config.shell, config.args.join(" "), item.script.clone()),
},
stats: Stats {
cpu_percent,
memory_usage,
restarts: item.restarts,
start_time: item.started.timestamp_millis(),
},
watch: Watch {
enabled: item.watch.enabled,
hash: item.watch.hash.clone(),
path: item.watch.path.clone(),
},
log: Log {
out: item.logs().out,
error: item.logs().error,
},
raw: Raw {
running: item.running,
crashed: item.crash.crashed,
crashes: item.crash.value,
},
}
}
}
pub mod dump;
pub mod hash;
pub mod http;
pub mod id;

File Metadata

Mime Type
text/x-diff
Expires
Sun, Feb 1, 6:24 PM (1 d, 19 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
494880
Default Alt Text
(90 KB)

Event Timeline