huskies: merge 918
This commit is contained in:
@@ -431,6 +431,10 @@ pub type ActiveProject = std::sync::Arc<tokio::sync::RwLock<String>>;
|
||||
/// `gateway_event_tx` — when `Some`, the bot will subscribe to the gateway
|
||||
/// status broadcaster and forward [`super::GatewayStatusEvent`]s to its
|
||||
/// configured Matrix rooms with a `[project-name]` prefix.
|
||||
///
|
||||
/// Returns `(abort_handle, shutdown_tx)`. The caller **must** hold
|
||||
/// `shutdown_tx` for the bot's lifetime and send `Some(ShutdownReason)` on it
|
||||
/// before process exit so the bot can announce "going offline" to its rooms.
|
||||
pub fn spawn_gateway_bot(
|
||||
config_dir: &Path,
|
||||
active_project: ActiveProject,
|
||||
@@ -438,7 +442,10 @@ pub fn spawn_gateway_bot(
|
||||
gateway_project_urls: BTreeMap<String, String>,
|
||||
port: u16,
|
||||
gateway_event_tx: Option<tokio::sync::broadcast::Sender<super::GatewayStatusEvent>>,
|
||||
) -> Option<tokio::task::AbortHandle> {
|
||||
) -> (
|
||||
Option<tokio::task::AbortHandle>,
|
||||
tokio::sync::watch::Sender<Option<crate::rebuild::ShutdownReason>>,
|
||||
) {
|
||||
use crate::agents::AgentPool;
|
||||
use crate::services::Services;
|
||||
use tokio::sync::{broadcast, mpsc};
|
||||
@@ -458,7 +465,8 @@ pub fn spawn_gateway_bot(
|
||||
|
||||
let (shutdown_tx, shutdown_rx) =
|
||||
tokio::sync::watch::channel::<Option<crate::rebuild::ShutdownReason>>(None);
|
||||
std::mem::forget(shutdown_tx);
|
||||
// shutdown_tx is intentionally NOT forgotten — the caller holds it and
|
||||
// sends Some(reason) on gateway shutdown so the bot announces "going offline".
|
||||
|
||||
let agents = std::sync::Arc::new(AgentPool::new(port, watcher_tx.clone()));
|
||||
|
||||
@@ -498,7 +506,7 @@ pub fn spawn_gateway_bot(
|
||||
config_dir.join(".huskies").join("timers.json"),
|
||||
));
|
||||
let gateway_event_rx = gateway_event_tx.map(|tx| tx.subscribe());
|
||||
crate::chat::transport::matrix::spawn_bot(
|
||||
let handle = crate::chat::transport::matrix::spawn_bot(
|
||||
config_dir,
|
||||
watcher_tx,
|
||||
services,
|
||||
@@ -508,5 +516,48 @@ pub fn spawn_gateway_bot(
|
||||
gateway_project_urls,
|
||||
timer_store,
|
||||
gateway_event_rx,
|
||||
)
|
||||
);
|
||||
(handle, shutdown_tx)
|
||||
}
|
||||
|
||||
// ── Tests ────────────────────────────────────────────────────────────────────
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// Regression test for story 918: `spawn_gateway_bot` must return a live
|
||||
/// `shutdown_tx` (not one dropped via `std::mem::forget`) so that callers
|
||||
/// can signal `Some(reason)` and the bot's shutdown watcher receives it.
|
||||
#[tokio::test]
|
||||
async fn spawn_gateway_bot_shutdown_tx_not_forgotten() {
|
||||
let tmp = tempfile::tempdir().unwrap();
|
||||
let active = std::sync::Arc::new(tokio::sync::RwLock::new("proj".to_string()));
|
||||
let (event_tx, _) = tokio::sync::broadcast::channel(4);
|
||||
|
||||
let (handle, shutdown_tx) = spawn_gateway_bot(
|
||||
tmp.path(),
|
||||
active,
|
||||
vec!["proj".to_string()],
|
||||
std::collections::BTreeMap::new(),
|
||||
3001,
|
||||
Some(event_tx),
|
||||
);
|
||||
|
||||
// No bot.toml in tmp → no abort handle spawned.
|
||||
assert!(handle.is_none());
|
||||
|
||||
// The shutdown_tx must be live: subscribe a receiver and verify that
|
||||
// sending Some(reason) is observed — this would fail if the sender
|
||||
// had been forgotten (channel closed, send returns Err).
|
||||
let rx = shutdown_tx.subscribe();
|
||||
shutdown_tx
|
||||
.send(Some(crate::rebuild::ShutdownReason::Manual))
|
||||
.expect("shutdown_tx must not be closed (was previously std::mem::forget'd)");
|
||||
assert_eq!(
|
||||
*rx.borrow(),
|
||||
Some(crate::rebuild::ShutdownReason::Manual),
|
||||
"shutdown receiver must see the Manual reason"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ pub use io::{
|
||||
spawn_gateway_notification_poller,
|
||||
};
|
||||
|
||||
use crate::rebuild::ShutdownReason;
|
||||
use io::Client;
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
use std::path::PathBuf;
|
||||
@@ -111,6 +112,12 @@ pub struct GatewayState {
|
||||
pub port: u16,
|
||||
/// Abort handle for the running Matrix bot task (if any).
|
||||
pub bot_handle: Arc<TokioMutex<Option<tokio::task::AbortHandle>>>,
|
||||
/// Watch sender used to signal the Matrix bot to shut down gracefully.
|
||||
///
|
||||
/// Send `Some(ShutdownReason)` on this channel before process exit so the
|
||||
/// bot task receives the signal and posts an "going offline" announcement.
|
||||
pub bot_shutdown_tx:
|
||||
Arc<TokioMutex<Option<tokio::sync::watch::Sender<Option<ShutdownReason>>>>>,
|
||||
/// Broadcast sender for [`GatewayStatusEvent`]s pushed by project nodes.
|
||||
///
|
||||
/// Call `event_tx.subscribe()` to obtain a receiver for outbound fan-out.
|
||||
@@ -142,6 +149,7 @@ impl GatewayState {
|
||||
config_dir,
|
||||
port,
|
||||
bot_handle: Arc::new(TokioMutex::new(None)),
|
||||
bot_shutdown_tx: Arc::new(TokioMutex::new(None)),
|
||||
event_tx,
|
||||
})
|
||||
}
|
||||
@@ -462,7 +470,7 @@ pub async fn save_bot_config_and_restart(state: &GatewayState, content: &str) ->
|
||||
.map(|(name, entry)| (name.clone(), entry.url.clone()))
|
||||
.collect();
|
||||
|
||||
let new_handle = io::spawn_gateway_bot(
|
||||
let (new_handle, new_shutdown_tx) = io::spawn_gateway_bot(
|
||||
&state.config_dir,
|
||||
Arc::clone(&state.active_project),
|
||||
gateway_projects,
|
||||
@@ -471,6 +479,7 @@ pub async fn save_bot_config_and_restart(state: &GatewayState, content: &str) ->
|
||||
Some(state.event_tx.clone()),
|
||||
);
|
||||
*handle = new_handle;
|
||||
*state.bot_shutdown_tx.lock().await = Some(new_shutdown_tx);
|
||||
}
|
||||
|
||||
crate::slog!("[gateway] Bot configuration saved; bot restarted");
|
||||
|
||||
Reference in New Issue
Block a user