Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/acp/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ impl SessionPool {
}
}

/// Return the thread IDs of all currently tracked sessions.
/// Used by the shutdown path to broadcast notifications before the pool is cleared.
pub async fn active_thread_ids(&self) -> Vec<String> {
self.connections.read().await.keys().cloned().collect()
}

pub async fn shutdown(&self) {
let mut conns = self.connections.write().await;
let count = conns.len();
Expand Down
30 changes: 29 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,37 @@ async fn main() -> anyhow::Result<()> {
// Run bot until SIGINT/SIGTERM
let shard_manager = client.shard_manager.clone();
let shutdown_pool = pool.clone();
let broadcast_pool = pool.clone();
let shutdown_http = client.http.clone();
tokio::spawn(async move {
tokio::signal::ctrl_c().await.ok();
let mut sigterm = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
.expect("install SIGTERM handler");
tokio::select! {
_ = tokio::signal::ctrl_c() => {}
_ = sigterm.recv() => {}
}
info!("shutdown signal received");

// Broadcast shutdown notification to active threads before closing the pool.
// Neutral wording — we don't promise automatic resume; Phase 2 of RFC #78 1d
// (session persistence) is a separate follow-up.
let thread_ids = broadcast_pool.active_thread_ids().await;
info!(count = thread_ids.len(), "broadcasting shutdown notification");
for thread_id in thread_ids {
if let Ok(id) = thread_id.parse::<u64>() {
let channel = serenity::model::id::ChannelId::new(id);
if let Err(e) = channel
.say(
&shutdown_http,
"🔄 Broker restarting. You can continue the conversation when the broker is back.",
)
.await
{
tracing::warn!(thread_id, error = %e, "failed to post shutdown notification");
}
}
}

shard_manager.shutdown_all().await;
});

Expand Down