improve admin command error propagation

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-28 04:09:46 +00:00
parent f047675a63
commit bb5f2556c3
9 changed files with 192 additions and 78 deletions

View file

@ -1,9 +1,7 @@
use conduit::{debug, debug_info, error, implement, info};
use conduit::{debug, debug_info, error, implement, info, Err, Result};
use ruma::events::room::message::RoomMessageEventContent;
use tokio::time::{sleep, Duration};
use super::console;
/// Possibly spawn the terminal console at startup if configured.
#[implement(super::Service)]
pub(super) async fn console_auto_start(&self) {
@ -24,45 +22,82 @@ pub(super) async fn console_auto_stop(&self) {
/// Execute admin commands after startup
#[implement(super::Service)]
pub(super) async fn startup_execute(&self) {
sleep(Duration::from_millis(500)).await; //TODO: remove this after run-states are broadcast
for (i, command) in self.services.server.config.admin_execute.iter().enumerate() {
self.startup_execute_command(i, command.clone()).await;
pub(super) async fn startup_execute(&self) -> Result<()> {
// List of comamnds to execute
let commands = &self.services.server.config.admin_execute;
// Determine if we're running in smoketest-mode which will change some behaviors
let smoketest = self.services.server.config.test.contains("smoke");
// When true, errors are ignored and startup continues.
let errors = !smoketest && self.services.server.config.admin_execute_errors_ignore;
//TODO: remove this after run-states are broadcast
sleep(Duration::from_millis(500)).await;
for (i, command) in commands.iter().enumerate() {
if let Err(e) = self.startup_execute_command(i, command.clone()).await {
if !errors {
return Err(e);
}
}
tokio::task::yield_now().await;
}
// The smoketest functionality is placed here for now and simply initiates
// shutdown after all commands have executed.
if self.services.server.config.test.contains("smoke") {
if smoketest {
debug_info!("Smoketest mode. All commands complete. Shutting down now...");
self.services
.server
.shutdown()
.unwrap_or_else(error::default_log);
.inspect_err(error::inspect_log)
.expect("Error shutting down from smoketest");
}
Ok(())
}
/// Execute one admin command after startup
#[implement(super::Service)]
async fn startup_execute_command(&self, i: usize, command: String) {
async fn startup_execute_command(&self, i: usize, command: String) -> Result<()> {
debug!("Startup command #{i}: executing {command:?}");
match self.command_in_place(command, None).await {
Err(e) => error!("Startup command #{i} failed: {e:?}"),
Ok(None) => info!("Startup command #{i} completed (no output)."),
Ok(Some(output)) => Self::startup_command_output(i, &output),
Err(output) => Self::startup_command_error(i, &output),
Ok(None) => {
info!("Startup command #{i} completed (no output).");
Ok(())
},
}
}
#[cfg(feature = "console")]
#[implement(super::Service)]
fn startup_command_output(i: usize, content: &RoomMessageEventContent) {
info!("Startup command #{i} completed:");
console::print(content.body());
fn startup_command_output(i: usize, content: &RoomMessageEventContent) -> Result<()> {
debug_info!("Startup command #{i} completed:");
super::console::print(content.body());
Ok(())
}
#[cfg(feature = "console")]
#[implement(super::Service)]
fn startup_command_error(i: usize, content: &RoomMessageEventContent) -> Result<()> {
super::console::print_err(content.body());
Err!(debug_error!("Startup command #{i} failed."))
}
#[cfg(not(feature = "console"))]
#[implement(super::Service)]
fn startup_command_output(i: usize, content: &RoomMessageEventContent) {
fn startup_command_output(i: usize, content: &RoomMessageEventContent) -> Result<()> {
info!("Startup command #{i} completed:\n{:#?}", content.body());
Ok(())
}
#[cfg(not(feature = "console"))]
#[implement(super::Service)]
fn startup_command_error(i: usize, content: &RoomMessageEventContent) -> Result<()> {
Err!(error!("Startup command #{i} failed:\n{:#?}", content.body()))
}