Skip to content

Commit

Permalink
Merge branch 'healthcheck' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierstoval committed Nov 6, 2021
2 parents 4fcb2eb + b1aefbd commit d89c1b6
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 195 deletions.
79 changes: 45 additions & 34 deletions src/commands/serve.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::env;
use std::fs::read_to_string;
use std::fs::write;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
Expand All @@ -13,16 +14,18 @@ use log::info;
use sysinfo::get_current_pid;
use sysinfo::ProcessExt;
use sysinfo::SystemExt;
use crate::config::config::php_server_pid;
use crate::config::paths::rymfony_server_info_file;
use crate::config::paths::rymfony_pid_file;

use crate::http::proxy_server;
use crate::php::php_server;
use crate::php::php_server::PhpServer;
use crate::php::structs::PhpServerSapi;
use crate::php::structs::ServerInfo;
use crate::utils::current_process_name;
use crate::utils::network::find_available_port;
use crate::utils::network::parse_default_port;
use crate::utils::project_directory::get_rymfony_project_directory;
use crate::utils::project_directory::{clean_rymfony_runtime_files, get_rymfony_project_directory};

const DEFAULT_PORT: &str = "8000";

Expand Down Expand Up @@ -92,45 +95,52 @@ pub(crate) fn serve(args: &ArgMatches) {
}

fn serve_foreground(args: &ArgMatches) {
let path = get_rymfony_project_directory().unwrap();
let rymfony_pid_file = path.join("rymfony.pid");
debug!(
"Looking for PID file in \"{}\".",
rymfony_pid_file.to_str().unwrap()
);
let rymfony_pid_file = rymfony_pid_file();
debug!("Looking for Rymfony PID file in \"{}\".",rymfony_pid_file.to_str().unwrap());

if rymfony_pid_file.exists() {
// Check if process is rymfony and exit if true.

let infos: ServerInfo =
serde_json::from_str(read_to_string(&rymfony_pid_file).unwrap().as_str())
.expect("Unable to unserialize data from PID file.");
let server_info_file = rymfony_server_info_file();

if !server_info_file.exists() {
warn!("Rymfony's PID file exists, but no server info was found.");
warn!("Cleaning Rymfony's project directory.");
clean_rymfony_runtime_files();
} else {
let infos: ServerInfo =
serde_json::from_str(read_to_string(server_info_file).unwrap().as_str())
.expect("Unable to unserialize data from PID file.");

let mut system = sysinfo::System::new_all();
system.refresh_all();
for (pid, proc_) in system.get_processes() {
#[cfg(not(target_family = "windows"))]
let process_pid = *pid;
let php_server_pid = php_server_pid();

#[cfg(target_family = "windows")]
let process_pid = *pid as i32;
let mut system = sysinfo::System::new_all();
system.refresh_all();
for (pid, proc_) in system.get_processes() {
#[cfg(not(target_family = "windows"))]
let process_pid = *pid;

let mut pname = proc_.exe().to_str().unwrap();
let pname_lower = pname.to_lowercase();
pname = pname_lower.as_str();
#[cfg(target_family = "windows")]
let process_pid = *pid as i32;

let exe_rymfony_name = if cfg!(not(target_family = "windows")) {
"rymfony"
} else {
"rymfony.exe"
};
let mut pname = proc_.exe().to_str().unwrap();
let pname_lower = pname.to_lowercase();
pname = pname_lower.as_str();

if &process_pid == &infos.pid() && pname.ends_with(exe_rymfony_name) {
info!(
let exe_rymfony_name = if cfg!(not(target_family = "windows")) {
"rymfony"
} else {
"rymfony.exe"
};

if process_pid.to_string() == php_server_pid && pname.ends_with(exe_rymfony_name) {
info!(
"The server is already running and listening to {}://127.0.0.1:{}",
infos.scheme(),
infos.port()
);
return;
return;
}
}
}
}
Expand Down Expand Up @@ -169,14 +179,14 @@ fn serve_foreground(args: &ArgMatches) {
};

let php_entrypoint_path = doc_root_path.join(script_filename.as_str());
let php_server = if !php_entrypoint_path.is_file() {
let (php_sapi, php_server_port) = if !php_entrypoint_path.is_file() {
warn!("No PHP entrypoint file");
PhpServer::new(0, PhpServerSapi::Unknown)
(PhpServerSapi::Unknown, 0)
} else {
php_server::start()
};

let sapi = match php_server.sapi() {
let sapi = match php_sapi {
PhpServerSapi::FPM => "FPM",
PhpServerSapi::CLI => "CLI",
PhpServerSapi::CGI => "CGI",
Expand Down Expand Up @@ -204,14 +214,15 @@ fn serve_foreground(args: &ArgMatches) {
#[cfg(target_family = "windows")]
let pid = get_current_pid().unwrap() as i32;

write(&rymfony_pid_file, pid.to_string()).expect("Could not write Rymfony PID to file.");

let args_str: Vec<String> = Vec::new();
let scheme = if args.is_present("no-tls") {
"http".to_string()
} else {
"https".to_string()
};
let pid_info = ServerInfo::new(
pid,
port,
scheme,
"Web Server".to_string(),
Expand All @@ -230,7 +241,7 @@ fn serve_foreground(args: &ArgMatches) {
proxy_server::start(
!args.is_present("no-tls"),
port,
php_server.port(),
php_server_port,
document_root,
script_filename,
args.is_present("expose-server-header"),
Expand Down
5 changes: 5 additions & 0 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::fs::read_to_string;
use std::fs::remove_file;
use std::fs::File;
use std::io::Write;
use crate::config::paths::php_server_pid_file;

#[derive(Debug)]
struct ConfigError(String);
Expand Down Expand Up @@ -71,3 +72,7 @@ pub(crate) fn clear_binaries_list() -> std::result::Result<(), Box<dyn std::erro
}
Ok(())
}

pub(crate) fn php_server_pid() -> String {
String::from(read_to_string(php_server_pid_file()).expect("Could not read PHP server's PID file."))
}
19 changes: 19 additions & 0 deletions src/config/paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::path::PathBuf;
use crate::utils::project_directory::get_rymfony_project_directory;

pub(crate) fn rymfony_pid_file() -> PathBuf {
PathBuf::from(format!("{}/rymfony.pid", get_rymfony_project_directory().unwrap().to_str().unwrap()))
}

pub(crate) fn rymfony_server_info_file() -> PathBuf {
PathBuf::from(format!("{}/rymfony_server_info", get_rymfony_project_directory().unwrap().to_str().unwrap()))
}

pub(crate) fn php_server_pid_file() -> PathBuf {
PathBuf::from(format!("{}/php_server.pid", get_rymfony_project_directory().unwrap().to_str().unwrap()))
}

#[cfg(not(target_os = "windows"))]
pub(crate) fn php_fpm_conf_ini_file() -> PathBuf {
PathBuf::from(format!("{}/fpm-conf.ini", get_rymfony_project_directory().unwrap().to_str().unwrap()))
}
6 changes: 4 additions & 2 deletions src/http/proxy_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,19 @@ pub(crate) fn start(

if output.status.code().unwrap() != 0 {
let stderr = String::from_utf8(output.stderr).unwrap();

if stderr.contains("listen tcp :80: bind: permission denied") {
error!("Caddy is unable to listen to port 80, which is used for HTTP to HTTPS redirection.");
error!("This can happen when you run Caddy (and therefore Rymfony) as non-root user.");
error!("To make it work, you need to give Caddy the necessary network capabilities.");

#[cfg(target_os = "linux")] {
error!("On most linux distribuions, you can do it by running this command (possibly with \"sudo\"):");
error!("On most linux distributions, you can do it by running this command (possibly with \"sudo\"):");
error!(" setcap cap_net_bind_service=+ep {}", caddy_path.to_str().unwrap());
}
}
panic!("Caddy failed to start.");

panic!("Caddy failed to start with error:\n{}", stderr);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ extern crate regex;

mod config {
pub(crate) mod config;
pub(crate) mod paths;
}

mod commands {
Expand All @@ -30,7 +31,6 @@ mod php {
pub(crate) mod php_server;
pub(crate) mod server_cgi;
pub(crate) mod server_fpm;
pub(crate) mod server_native;
pub(crate) mod structs;
}

Expand Down
4 changes: 2 additions & 2 deletions src/php/binaries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ fn binaries_from_dir(path: PathBuf) -> HashMap<PhpVersion, PhpBinary> {
} else {
let mut bin = PhpBinary::from_version(version.clone());
bin.add_sapi(&sapi, &path);
&binaries.insert(version.clone(), bin);
let _ = &binaries.insert(version.clone(), bin);
}
}

Expand Down Expand Up @@ -293,7 +293,7 @@ fn merge_binaries(into: &mut HashMap<PhpVersion, PhpBinary>, from: HashMap<PhpVe
// FIXME
// this needs to be fixed, but for now we assume that the first ever found version is
// the one that is first in PATH and therefore the "system" binary
&binary.set_system(if into.len() == 0 { true } else { false });
let _ = &binary.set_system(if into.len() == 0 { true } else { false });

if into.contains_key(&version) {
into.get_mut(&version).unwrap().merge_with(binary);
Expand Down
Loading

0 comments on commit d89c1b6

Please sign in to comment.