Page MenuHomePhorge

No OneTemporary

Size
5 KB
Referenced Files
None
Subscribers
None
diff --git a/src/cli.rs b/src/cli.rs
index d982cf7..ab07935 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,41 +1,41 @@
use crate::process::Runner;
use crate::structs::Args;
use global_placeholders::global;
use macros_rs::string;
use std::env;
pub fn get_version(short: bool) -> String {
return match short {
true => format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION")),
false => format!("{} ({} {}) [{}]", env!("CARGO_PKG_VERSION"), env!("GIT_HASH"), env!("BUILD_DATE"), env!("PROFILE")),
};
}
pub fn start(name: &Option<String>, args: &Option<Args>) {
let mut runner = Runner::new(global!("pmc.logs"));
let name = match name {
Some(name) => string!(name),
None => string!(""),
};
match args {
- Some(Args::Id(id)) => println!("{}", id),
+ Some(Args::Id(id)) => runner.restart(*id),
Some(Args::Script(script)) => runner.start(name, script),
None => {}
}
}
pub fn stop(id: &usize) {
let mut runner = Runner::new(global!("pmc.logs"));
runner.stop(*id);
println!("Stopped process");
}
pub fn list() {
let runner = Runner::new(global!("pmc.logs"));
for (id, item) in runner.list() {
println!("id: {id}\nname: {}\npid: {}\nstatus: {}", item.name, item.pid, item.running);
}
}
diff --git a/src/process/dump.rs b/src/process/dump.rs
index 407272a..9cebd76 100644
--- a/src/process/dump.rs
+++ b/src/process/dump.rs
@@ -1,54 +1,52 @@
use crate::helpers::Exists;
use crate::helpers::Id;
use crate::process::Runner;
use colored::Colorize;
use global_placeholders::global;
use macros_rs::{crashln, string};
use std::{collections::BTreeMap, fs};
pub fn read() -> Runner {
if !Exists::folder(global!("pmc.base")).unwrap() {
fs::create_dir_all(global!("pmc.base")).unwrap();
log::info!("created pmc base dir");
}
if !Exists::file(global!("pmc.dump")).unwrap() {
let runner = Runner {
log_path: string!(""),
id: Id::new(0),
process_list: BTreeMap::new(),
};
write(&runner);
log::info!("created dump file");
}
let contents = match fs::read_to_string(global!("pmc.dump")) {
Ok(contents) => contents,
Err(err) => crashln!("Cannot find dumpfile.\n{}", string!(err).white()),
};
match toml::from_str(&contents).map_err(|err| string!(err)) {
Ok(parsed) => parsed,
Err(err) => crashln!("Cannot read dumpfile.\n{}", err.white()),
}
}
pub fn write(dump: &Runner) {
if !Exists::folder(global!("pmc.base")).unwrap() {
fs::create_dir_all(global!("pmc.base")).unwrap();
log::info!("created pmc base dir");
}
- println!("{:?}", dump);
-
let contents = match toml::to_string(dump) {
Ok(contents) => contents,
Err(err) => crashln!("Cannot parse dump.\n{}", string!(err).white()),
};
if let Err(err) = fs::write(global!("pmc.dump"), contents) {
crashln!("Error writing dumpfile.\n{}", string!(err).white())
}
}
diff --git a/src/process/mod.rs b/src/process/mod.rs
index 5d8a05f..f032359 100644
--- a/src/process/mod.rs
+++ b/src/process/mod.rs
@@ -1,60 +1,84 @@
mod dump;
use crate::helpers::Id;
use crate::service::{run, stop};
use macros_rs::{crashln, string};
use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap;
#[derive(Debug, Deserialize, Serialize)]
pub struct Process {
pub pid: i64,
pub name: String,
+ pub script: String,
pub running: bool,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct Runner {
id: Id,
log_path: String,
process_list: BTreeMap<String, Process>,
}
impl Runner {
pub fn new(path: String) -> Self {
let dump = dump::read();
let runner = Runner {
log_path: path,
id: dump.id,
process_list: dump.process_list,
};
dump::write(&runner);
return runner;
}
pub fn start(&mut self, name: String, command: &String) {
let pid = run(&name, &self.log_path, &command);
- self.process_list.insert(string!(self.id.next()), Process { pid, name, running: true });
+ self.process_list.insert(
+ string!(self.id.next()),
+ Process {
+ pid,
+ name,
+ script: string!(command),
+ running: true,
+ },
+ );
dump::write(&self);
}
pub fn stop(&mut self, id: usize) {
if let Some(item) = self.process_list.get_mut(&string!(id)) {
let pid = item.pid;
// add error catching in cc
// to allow match Err() here
stop(pid);
item.running = false;
dump::write(&self);
} else {
crashln!("Process with {id} does not exist");
}
}
+ pub fn restart(&mut self, id: usize) {
+ if let Some(item) = self.info(id) {
+ let name = item.name.clone();
+ let script = item.script.clone();
+
+ self.stop(id);
+ let pid = run(&name, &self.log_path, &script);
+
+ self.process_list.insert(string!(id), Process { pid, name, script, running: true });
+ dump::write(&self);
+ } else {
+ crashln!("Failed to restart process with id {}", id);
+ }
+ }
+
pub fn info(&self, id: usize) -> Option<&Process> { self.process_list.get(&string!(id)) }
pub fn list(&self) -> &BTreeMap<String, Process> { &self.process_list }
}

File Metadata

Mime Type
text/x-diff
Expires
Thu, Mar 19, 11:07 PM (11 h, 22 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
506943
Default Alt Text
(5 KB)

Event Timeline