Skip to content

Commit

Permalink
Change admin flag to no-admin, respect shutdown signal
Browse files Browse the repository at this point in the history
  • Loading branch information
XAMPPRocky committed Oct 13, 2022
1 parent 2c3e844 commit cab52ea
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ const ETC_CONFIG_PATH: &str = "/etc/quilkin/quilkin.yaml";
#[non_exhaustive]
pub struct Cli {
/// Whether to spawn the admin server or not.
#[clap(default_value = "true", env, long)]
pub admin: bool,
#[clap(env, long)]
pub no_admin: bool,
/// The path to the configuration file for the Quilkin instance.
#[clap(short, long, env = "QUILKIN_CONFIG", default_value = "quilkin.yaml")]
pub config: PathBuf,
Expand Down Expand Up @@ -103,7 +103,7 @@ impl Cli {
);

let config = Arc::new(Self::read_config(self.config)?);
let _admin_task = if let Some(mode) = self.command.admin_mode().filter(|_| self.admin) {
let _admin_task = if let Some(mode) = self.command.admin_mode().filter(|_| !self.no_admin) {
if let Some(address) = self.admin_address {
config
.admin
Expand All @@ -115,7 +115,7 @@ impl Cli {
None
};

let (shutdown_tx, shutdown_rx) = watch::channel::<()>(());
let (shutdown_tx, mut shutdown_rx) = watch::channel::<()>(());

#[cfg(target_os = "linux")]
let mut sig_term_fut = signal::unix::signal(signal::unix::SignalKind::terminate())?;
Expand All @@ -137,10 +137,18 @@ impl Cli {
shutdown_tx.send(()).ok();
});

match &self.command {
Commands::Run(runner) => runner.run(config, shutdown_rx.clone()).await,
Commands::Manage(manager) => manager.manage(config).await,
Commands::GenerateConfigSchema(generator) => generator.generate_config_schema(),
let fut = match self.command {
Commands::Run(runner) => tokio::spawn({
let shutdown_rx = shutdown_rx.clone();
async move { runner.run(config, shutdown_rx).await }
}),
Commands::Manage(manager) => tokio::spawn(async move { manager.manage(config).await }),
Commands::GenerateConfigSchema(generator) => tokio::spawn(std::future::ready(generator.generate_config_schema())),
};

tokio::select! {
result = fut => result?,
_ = shutdown_rx.changed() => Ok(())
}
}

Expand Down

0 comments on commit cab52ea

Please sign in to comment.