Skip to content

Commit

Permalink
Supports restarting daemon processes and viewing status
Browse files Browse the repository at this point in the history
  • Loading branch information
0x676e67 committed Dec 15, 2023
1 parent a06f466 commit e9bc36a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
17 changes: 14 additions & 3 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub(crate) const DEFAULT_STDERR_PATH: &str = "/var/run/xunlei.err";
pub(crate) const DEFAULT_WORK_DIR: &str = "/";

pub fn check_root() {
use nix::unistd::Uid;

if !Uid::effective().is_root() {
if !nix::unistd::Uid::effective().is_root() {
println!("You must run this executable with root permissions");
std::process::exit(-1)
}
Expand Down Expand Up @@ -96,6 +94,19 @@ pub(super) fn stop() -> anyhow::Result<()> {
Ok(())
}

pub(super) fn restart() -> anyhow::Result<()> {
stop()?;
start()
}

pub(super) fn status() -> anyhow::Result<()> {
match get_pid() {
Some(pid) => println!("Xunlei is running with pid: {}", pid),
None => println!("Xunlei is not running"),
}
Ok(())
}

pub(super) fn log() -> anyhow::Result<()> {
fn read_and_print_file(file_path: &Path, placeholder: &str) -> anyhow::Result<()> {
if !file_path.exists() {
Expand Down
13 changes: 12 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ pub enum Commands {
Run(ServeConfig),
/// Start xunlei daemon
Start(ServeConfig),
/// Restart xunlei daemon
Restart(ServeConfig),
/// Stop xunlei daemon
Stop,
/// Xunlei log
/// Status of the Http server daemon process
Status,
/// Show the Http server daemon log
Log,
}

Expand Down Expand Up @@ -182,9 +186,16 @@ fn main() -> anyhow::Result<()> {
daemon::start()?;
serve::Serve::new(config, InstallConfig::read_from_file()?).run()?;
}
Commands::Restart(config) => {
daemon::restart()?;
serve::Serve::new(config, InstallConfig::read_from_file()?).run()?;
}
Commands::Stop => {
daemon::stop()?;
}
Commands::Status => {
daemon::status()?;
}
Commands::Log => {
daemon::log()?;
}
Expand Down

0 comments on commit e9bc36a

Please sign in to comment.