Skip to content

Commit

Permalink
initialize logging in main.rs so that ctl() benefits from it
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj authored and FlorentinDUBOIS committed Jul 13, 2022
1 parent af29ec6 commit c18a540
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 20 deletions.
16 changes: 4 additions & 12 deletions bin/src/ctl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,20 @@ use self::command::{
};
use crate::cli::*;

pub fn ctl(matches: Sozu) -> Result<(), anyhow::Error> {
let config_file = matches
.config
.or(option_env!("SOZU_CONFIG").map(|s| s.to_string()))
.expect("missing --config <configuration file> option");

let config =
Config::load_from_path(config_file.as_str()).expect("could not parse configuration file");

pub fn ctl(config: Config, cmd: SubCmd, timeout: Option<u64>) -> Result<(), anyhow::Error> {
// If the command is `config check` then exit because if we are here, the configuration is valid
if let SubCmd::Config {
cmd: ConfigCmd::Check {},
} = matches.cmd
} = cmd
{
println!("Configuration file is valid");
std::process::exit(0);
}

let channel = create_channel(&config).expect("could not connect to the command unix socket");
let timeout: u64 = matches.timeout.unwrap_or(config.ctl_command_timeout);
let timeout: u64 = timeout.unwrap_or(config.ctl_command_timeout);

match matches.cmd {
match cmd {
SubCmd::Shutdown { hard, worker } => {
if hard {
hard_stop(channel, worker, timeout)
Expand Down
15 changes: 7 additions & 8 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ fn main() -> anyhow::Result<()> {
register_panic_hook();

let matches = cli::Sozu::from_args();
let config = load_configuration(get_config_file_path(&matches)?)?;

util::setup_logging(&config);

match matches.cmd {
cli::SubCmd::Start => {
start(&matches)?;
start(config)?;
info!("main process stopped");
Ok(())
}
Expand Down Expand Up @@ -88,14 +91,11 @@ fn main() -> anyhow::Result<()> {
max_command_buffer_size,
)
}
_ => ctl::ctl(matches),
_ => ctl::ctl(config, matches.cmd, matches.timeout),
}
}

fn start(matches: &cli::Sozu) -> Result<(), anyhow::Error> {
let config = load_configuration(get_config_file_path(&matches)?)?;

util::setup_logging(&config);
fn start(config: Config) -> Result<(), anyhow::Error> {
info!("Starting up");
util::setup_metrics(&config);
util::write_pid_file(&config).with_context(|| "PID file is not writeable")?;
Expand All @@ -110,8 +110,7 @@ fn start(matches: &cli::Sozu) -> Result<(), anyhow::Error> {

let command_socket_path = config.command_socket_path();

command::start(config, command_socket_path, workers)
.with_context(|| "could not start Sozu")?;
command::start(config, command_socket_path, workers).with_context(|| "could not start Sozu")?;

Ok(())
}
Expand Down

0 comments on commit c18a540

Please sign in to comment.