Skip to content

Commit

Permalink
more readable error handling on write_pid_file()
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj authored and Geal committed Jul 30, 2021
1 parent 3f98117 commit 7628b46
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ include = [
name = "sozu"

[dependencies]
anyhow = "1.0.42"
mio = { version = "^0.7", features = [ "os-poll", "uds" ] }
serde = "~1.0.2"
serde_json = "~1.0.1"
Expand Down
5 changes: 3 additions & 2 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ fn start(matches: &ArgMatches) -> Result<(), StartupError> {
util::setup_logging(&config);
info!("Starting up");
util::setup_metrics(&config);
util::write_pid_file(&config)?;
util::write_pid_file(&config)
.or_else(|e| Err(StartupError::PIDFileNotWritable(e.to_string())))?;

update_process_limits(&config)?;

Expand All @@ -102,7 +103,7 @@ fn start(matches: &ArgMatches) -> Result<(), StartupError> {

let command_socket_path = config.command_socket_path();

// this should be transformed into a new StartupError that contains std::io::Error
// this could be transformed into a new StartupError that contains std::io::Error
if let Err(e) = command::start(config, command_socket_path, workers) {
error!("could not start worker: {:?}", e);
}
Expand Down
27 changes: 11 additions & 16 deletions bin/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::fs::File;
use std::io::Write;
use libc;

use anyhow;
use crate::logging;
use crate::StartupError;
use sozu_command::config::Config;
use sozu::metrics;

Expand Down Expand Up @@ -46,27 +46,22 @@ pub fn setup_metrics(config: &Config) {
}
}

pub fn write_pid_file(config: &Config) -> Result<(), StartupError> {
pub fn write_pid_file(config: &Config) -> Result<(), anyhow::Error> {
let pid_file_path = match config.pid_file_path {
Some(ref pid_file_path) => Some(pid_file_path.as_ref()),
None => option_env!("SOZU_PID_FILE_PATH")
};

if let Some(ref pid_file_path) = pid_file_path {
File::create(pid_file_path)
.and_then(|mut file| {
let pid = unsafe { libc::getpid() };
file
.write_all(format!("{}", pid).as_bytes())
.map(|()| file)
})
.and_then(|file| file.sync_all())
.map(|()| ())
.or_else(|err| return Err(
StartupError::PIDFileNotWritable(
format!("Couldn't write the PID file to {}. Error: {:?}", pid_file_path, err))
)
)

let mut file = File::create(pid_file_path)?;

let pid = unsafe { libc::getpid() };

file.write_all(format!("{}", pid).as_bytes())?;
file.sync_all()?;

Ok(())
} else {
Ok(())
}
Expand Down

0 comments on commit 7628b46

Please sign in to comment.