From 263e3380883ec2ae801f7be8fa0be7388b158377 Mon Sep 17 00:00:00 2001 From: Jason Volk <jason@zemos.net> Date: Mon, 22 Jul 2024 21:21:44 +0000 Subject: [PATCH] add --console program option to automatically spawn Signed-off-by: Jason Volk <jason@zemos.net> --- src/core/config/mod.rs | 6 ++++++ src/main/clap.rs | 6 +++++- src/service/admin/mod.rs | 22 +++++++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/core/config/mod.rs b/src/core/config/mod.rs index a689701aa..6cef402af 100644 --- a/src/core/config/mod.rs +++ b/src/core/config/mod.rs @@ -330,6 +330,8 @@ pub struct Config { pub block_non_admin_invites: bool, #[serde(default = "true_fn")] pub admin_escape_commands: bool, + #[serde(default)] + pub admin_console_automatic: bool, #[serde(default)] pub sentry: bool, @@ -579,6 +581,10 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { &self.block_non_admin_invites.to_string(), ); line("Enable admin escape commands", &self.admin_escape_commands.to_string()); + line( + "Activate admin console after startup", + &self.admin_console_automatic.to_string(), + ); line("Allow outgoing federated typing", &self.allow_outgoing_typing.to_string()); line("Allow incoming federated typing", &self.allow_incoming_typing.to_string()); line( diff --git a/src/main/clap.rs b/src/main/clap.rs index e82fea16e..16d88978e 100644 --- a/src/main/clap.rs +++ b/src/main/clap.rs @@ -12,6 +12,10 @@ pub(crate) struct Args { #[arg(short, long)] /// Optional argument to the path of a conduwuit config TOML file pub(crate) config: Option<PathBuf>, + + /// Activate admin command console automatically after startup. + #[arg(long, num_args(0))] + pub(crate) console: bool, } /// Parse commandline arguments into structured data @@ -22,7 +26,7 @@ pub(super) fn parse() -> Args { Args::parse() } pub(crate) fn update(config: &mut Config, args: &Args) -> Result<()> { // Indicate the admin console should be spawned automatically if the // configuration file hasn't already. - config.admin_console_automatic |= args.console.unwrap_or(false); + config.admin_console_automatic |= args.console; Ok(()) } diff --git a/src/service/admin/mod.rs b/src/service/admin/mod.rs index 1d51bf381..b3879deb7 100644 --- a/src/service/admin/mod.rs +++ b/src/service/admin/mod.rs @@ -82,6 +82,8 @@ fn build(args: crate::Args<'_>) -> Result<Arc<Self>> { async fn worker(self: Arc<Self>) -> Result<()> { let receiver = self.receiver.lock().await; let mut signals = self.services.server.signal.subscribe(); + self.console_auto_start().await; + loop { tokio::select! { command = receiver.recv_async() => match command { @@ -95,9 +97,7 @@ async fn worker(self: Arc<Self>) -> Result<()> { } } - //TODO: not unwind safe - #[cfg(feature = "console")] - self.console.close().await; + self.console_auto_stop().await; //TODO: not unwind safe Ok(()) } @@ -340,4 +340,20 @@ pub fn is_admin_room(&self, room_id: &RoomId) -> bool { false } } + + /// Possibly spawn the terminal console at startup if configured. + async fn console_auto_start(&self) { + #[cfg(feature = "console")] + if self.services.server.config.admin_console_automatic { + // Allow more of the startup sequence to execute before spawning + tokio::task::yield_now().await; + self.console.start().await; + } + } + + /// Shutdown the console when the admin worker terminates. + async fn console_auto_stop(&self) { + #[cfg(feature = "console")] + self.console.close().await; + } } -- GitLab