Skip to content

Commit

Permalink
initialize logging in start() and in ctl() but not in main
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj authored and FlorentinDUBOIS committed Jul 13, 2022
1 parent c18a540 commit da3cdc0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
19 changes: 13 additions & 6 deletions bin/src/ctl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod command;

use std::io;

use crate::cli;
use crate::util;
use crate::{get_config_file_path, load_configuration};
use sozu_command::channel::Channel;
use sozu_command::command::{CommandRequest, CommandResponse};
use sozu_command::config::Config;
use sozu_command::proxy::ListenerType;
use std::io;

use self::command::{
activate_listener, add_application, add_backend, add_certificate, add_http_frontend,
Expand All @@ -17,20 +19,25 @@ use self::command::{
};
use crate::cli::*;

pub fn ctl(config: Config, cmd: SubCmd, timeout: Option<u64>) -> Result<(), anyhow::Error> {
pub fn ctl(matches: cli::Sozu) -> Result<(), anyhow::Error> {
let config_file_path = get_config_file_path(&matches)?;
let config = load_configuration(config_file_path)?;

util::setup_logging(&config);

// If the command is `config check` then exit because if we are here, the configuration is valid
if let SubCmd::Config {
cmd: ConfigCmd::Check {},
} = cmd
} = matches.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 = timeout.unwrap_or(config.ctl_command_timeout);
let timeout: u64 = matches.timeout.unwrap_or(config.ctl_command_timeout);

match cmd {
match matches.cmd {
SubCmd::Shutdown { hard, worker } => {
if hard {
hard_stop(channel, worker, timeout)
Expand Down
17 changes: 9 additions & 8 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,10 @@ 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(config)?;
start(&matches)?;
info!("main process stopped");
Ok(())
}
Expand Down Expand Up @@ -91,11 +88,15 @@ fn main() -> anyhow::Result<()> {
max_command_buffer_size,
)
}
_ => ctl::ctl(config, matches.cmd, matches.timeout),
_ => ctl::ctl(matches),
}
}

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

util::setup_logging(&config);
info!("Starting up");
util::setup_metrics(&config);
util::write_pid_file(&config).with_context(|| "PID file is not writeable")?;
Expand All @@ -120,7 +121,7 @@ fn init_workers(config: &Config) -> Result<Vec<Worker>, anyhow::Error> {
start_workers(path, &config).with_context(|| "Failed at spawning workers")
}

fn get_config_file_path(matches: &cli::Sozu) -> Result<&str, anyhow::Error> {
pub fn get_config_file_path(matches: &cli::Sozu) -> Result<&str, anyhow::Error> {
match matches.config.as_ref() {
Some(config_file) => Ok(config_file.as_str()),
None => option_env!("SOZU_CONFIG").ok_or(anyhow::Error::msg(
Expand All @@ -130,7 +131,7 @@ fn get_config_file_path(matches: &cli::Sozu) -> Result<&str, anyhow::Error> {
}
}

fn load_configuration(config_file: &str) -> Result<Config, anyhow::Error> {
pub fn load_configuration(config_file: &str) -> Result<Config, anyhow::Error> {
Config::load_from_path(config_file).with_context(|| "Invalid configuration file.")
}

Expand Down

0 comments on commit da3cdc0

Please sign in to comment.