split admin service startup related into unit

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-08-26 21:23:06 +00:00
parent e4bcbb8088
commit ffc41cb01f
2 changed files with 64 additions and 52 deletions

View file

@ -1,6 +1,7 @@
pub mod console; pub mod console;
mod create; mod create;
mod grant; mod grant;
mod startup;
use std::{ use std::{
future::Future, future::Future,
@ -9,7 +10,7 @@ use std::{
}; };
use async_trait::async_trait; use async_trait::async_trait;
use conduit::{debug, error, error::default_log, info, pdu::PduBuilder, Err, Error, PduEvent, Result, Server}; use conduit::{debug, error, error::default_log, pdu::PduBuilder, Err, Error, PduEvent, Result, Server};
pub use create::create_admin_room; pub use create::create_admin_room;
use loole::{Receiver, Sender}; use loole::{Receiver, Sender};
use ruma::{ use ruma::{
@ -20,10 +21,7 @@ use ruma::{
OwnedEventId, OwnedRoomId, RoomId, UserId, OwnedEventId, OwnedRoomId, RoomId, UserId,
}; };
use serde_json::value::to_raw_value; use serde_json::value::to_raw_value;
use tokio::{ use tokio::sync::{Mutex, RwLock};
sync::{Mutex, RwLock},
time::{sleep, Duration},
};
use crate::{globals, rooms, rooms::state::RoomMutexGuard, Dep}; use crate::{globals, rooms, rooms::state::RoomMutexGuard, Dep};
@ -361,53 +359,6 @@ impl Service {
} }
} }
/// Execute admin commands after startup
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;
tokio::task::yield_now().await;
}
}
/// Execute one admin command after startup
async fn startup_execute_command(&self, i: usize, command: String) {
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),
}
}
#[cfg(feature = "console")]
fn startup_command_output(i: usize, content: &RoomMessageEventContent) {
info!("Startup command #{i} completed:");
console::print(content.body());
}
#[cfg(not(feature = "console"))]
fn startup_command_output(i: usize, content: &RoomMessageEventContent) {
info!("Startup command #{i} completed:\n{:#?}", content.body());
}
/// 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;
}
/// Sets the self-reference to crate::Services which will provide context to /// Sets the self-reference to crate::Services which will provide context to
/// the admin commands. /// the admin commands.
pub(super) fn set_services(&self, services: &Option<Arc<crate::Services>>) { pub(super) fn set_services(&self, services: &Option<Arc<crate::Services>>) {

View file

@ -0,0 +1,61 @@
use conduit::{
debug, error, implement, info,
};
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) {
#[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.
#[implement(super::Service)]
pub(super) async fn console_auto_stop(&self) {
#[cfg(feature = "console")]
self.console.close().await;
}
/// 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;
tokio::task::yield_now().await;
}
}
/// Execute one admin command after startup
#[implement(super::Service)]
async fn startup_execute_command(&self, i: usize, command: String) {
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),
}
}
#[cfg(feature = "console")]
#[implement(super::Service)]
fn startup_command_output(i: usize, content: &RoomMessageEventContent) {
info!("Startup command #{i} completed:");
console::print(content.body());
}
#[cfg(not(feature = "console"))]
#[implement(super::Service)]
fn startup_command_output(i: usize, content: &RoomMessageEventContent) {
info!("Startup command #{i} completed:\n{:#?}", content.body());
}