diff --git a/crates/client/lang/en/builder_server.txt b/crates/client/lang/en/builder_server.txt index 4118259..cc572d2 100644 --- a/crates/client/lang/en/builder_server.txt +++ b/crates/client/lang/en/builder_server.txt @@ -23,3 +23,6 @@ rate_limits = Rate limits zero_unlimited = 0 = unlimited zero_off = 0 = off invalid_number = Invalid number +process = Process +restart = Restart server +restart_hint = Everyone reconnects in a few seconds diff --git a/crates/client/lang/tr/builder_server.txt b/crates/client/lang/tr/builder_server.txt index 04e0249..c321123 100644 --- a/crates/client/lang/tr/builder_server.txt +++ b/crates/client/lang/tr/builder_server.txt @@ -23,3 +23,6 @@ rate_limits = Hız sınırları zero_unlimited = 0 = sınırsız zero_off = 0 = kapalı invalid_number = Geçersiz sayı +process = Süreç +restart = Sunucuyu yeniden başlat +restart_hint = Herkes birkaç saniye içinde yeniden bağlanır diff --git a/crates/client/src/gui/builder/server.rs b/crates/client/src/gui/builder/server.rs index 9ceef4f..26cd394 100644 --- a/crates/client/src/gui/builder/server.rs +++ b/crates/client/src/gui/builder/server.rs @@ -1,12 +1,21 @@ use crate::gui::glyph::Fill as _; -use common::{call::UpdateServerSettings, model::ServerSettings}; +use common::{ + call::{RestartServer, UpdateServerSettings}, + model::ServerSettings, +}; use gpui_kit::{ AnyElement, App, Context, Entity, IntoElement, Window, component::{WindowExt, input::InputState, notification::Notification}, }; use super::ui::{field_row, list_card, number, to_bytes, to_units}; -use crate::gui::{KB, MB, Palette, Yumush, notify_error, settings_page::segmented, widget::field}; +use crate::gui::{ + KB, MB, Palette, Yumush, + chat::Armed, + notify_error, + settings_page::{delete_control, segmented}, + widget::field, +}; use crate::lang::w; fn input_text(input: &Entity, cx: &App) -> String { @@ -143,6 +152,20 @@ impl Yumush { .detach(); } + fn restart_server(&mut self, window: &mut Window, cx: &mut Context) { + if !self.arm(Armed::RestartServer, cx) { + return; + } + let network = self.network.clone(); + cx.spawn_in(window, async move |this, cx| { + if let Err(error) = network.call(RestartServer).await { + notify_error(&this, cx, error); + } + }) + .detach(); + cx.notify(); + } + pub(super) fn server_body(&self, entity: &Entity, palette: Palette) -> Vec { let general = vec![ field_row( @@ -260,6 +283,25 @@ impl Yumush { list_card(Some(w().builder_server.limits), limits, "", palette).into_any_element(), list_card(Some(w().builder_server.storage), storage, "", palette).into_any_element(), list_card(Some(w().builder_server.rate_limits), rate, "", palette).into_any_element(), + list_card( + Some(w().builder_server.process), + vec![field_row( + w().builder_server.restart, + Some(w().builder_server.restart_hint), + delete_control( + "restart_server", + w().update.restart, + self.armed(Armed::RestartServer), + entity, + palette, + Yumush::restart_server, + ), + palette, + )], + "", + palette, + ) + .into_any_element(), ] } } diff --git a/crates/client/src/gui/chat.rs b/crates/client/src/gui/chat.rs index 0423666..c69c375 100644 --- a/crates/client/src/gui/chat.rs +++ b/crates/client/src/gui/chat.rs @@ -90,6 +90,7 @@ pub enum Armed { DeleteUser(Id), DeleteAccount, DeleteCommunity, + RestartServer, } #[derive(Debug, Clone, PartialEq, Eq)] diff --git a/crates/common/src/call.rs b/crates/common/src/call.rs index 16408ac..0921dd1 100644 --- a/crates/common/src/call.rs +++ b/crates/common/src/call.rs @@ -653,6 +653,9 @@ pub struct FollowStats { #[derive(Debug, Clone, Encode, Decode)] pub struct GetServerStats; +#[derive(Debug, Clone, Encode, Decode)] +pub struct RestartServer; + #[derive(Debug, Clone, Encode, Decode)] pub struct PushStats { pub stats: Stats, @@ -791,4 +794,5 @@ calls! { FollowStats: Builder -> Option; PushStats: User -> (); GetServerStats: Builder -> Stats; + RestartServer: Builder -> (); } diff --git a/crates/common/src/transport.rs b/crates/common/src/transport.rs index d398cb1..0ea6dce 100644 --- a/crates/common/src/transport.rs +++ b/crates/common/src/transport.rs @@ -13,7 +13,7 @@ use quinn::{ use quinn_proto::RttEstimator; use socket2::{Domain, Protocol, Socket, Type}; -pub const ALPN: &[u8] = b"yumush/4"; +pub const ALPN: &[u8] = b"yumush/5"; const IDLE_TIMEOUT: Duration = Duration::from_secs(10); pub const KEEP_ALIVE: Duration = Duration::from_secs(1); @@ -174,10 +174,11 @@ pub enum Close { Maintenance = 4, Slow = 5, Protocol = 6, + Restart = 7, } impl Close { - pub const ALL: [Self; 7] = [ + pub const ALL: [Self; 8] = [ Self::Normal, Self::SignedOut, Self::Revoked, @@ -185,6 +186,7 @@ impl Close { Self::Maintenance, Self::Slow, Self::Protocol, + Self::Restart, ]; pub fn of(code: VarInt) -> Option { @@ -200,6 +202,7 @@ impl Close { Self::Maintenance => "maintenance", Self::Slow => "slow", Self::Protocol => "protocol error", + Self::Restart => "server restarting", } } diff --git a/crates/server/src/call/admin.rs b/crates/server/src/call/admin.rs index 3803463..53c700c 100644 --- a/crates/server/src/call/admin.rs +++ b/crates/server/src/call/admin.rs @@ -4,8 +4,9 @@ use common::{ DeleteBackup, DeleteInviteCode, DeleteUser, GetDailyStats, GetOverview, GetStorageUsage, ListAdminLogs, ListAllCommunities, ListAttachments, ListBackups, ListInviteCodes, ListServerBans, ListUsers, Logout, PurgeUser, RemoveServerBan, RenameUser, ResetPassword, - RevokeSessions, SearchCommunity, SearchMessages, SetCommunityVoice, SetGhost, StorageUsage, - SuspendUser, TimeoutUser, TransferOwnership, UpdateServerSettings, WatchCommunity, + RestartServer, RevokeSessions, SearchCommunity, SearchMessages, SetCommunityVoice, + SetGhost, StorageUsage, SuspendUser, TimeoutUser, TransferOwnership, UpdateServerSettings, + WatchCommunity, }, error::Error, event::Event, @@ -685,6 +686,19 @@ impl Handle for SetCommunityVoice { } } +impl Handle for RestartServer { + async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { + let me = me.user()?; + tracing::info!("restart requested by {}", me.username); + let restart = cx.sh.services.restart.clone(); + tokio::spawn(async move { + tokio::time::sleep(std::time::Duration::from_millis(300)).await; + restart.notify_one(); + }); + Ok(()) + } +} + impl Handle for SetGhost { async fn handle(self, me: Me, cx: &mut Cx<'_>) -> Result<(), ServerError> { let me = me.user()?; diff --git a/crates/server/src/call/mod.rs b/crates/server/src/call/mod.rs index f74d59c..7befac5 100644 --- a/crates/server/src/call/mod.rs +++ b/crates/server/src/call/mod.rs @@ -213,6 +213,7 @@ pub(crate) async fn dispatch(request: Request, cx: &mut Cx<'_>) -> Vec { Request::GetStorageUsage(call) => serve(call, cx).await, Request::GetDailyStats(call) => serve(call, cx).await, Request::SetGhost(call) => serve(call, cx).await, + Request::RestartServer(call) => serve(call, cx).await, Request::CreateBackup(call) => serve(call, cx).await, Request::ListBackups(call) => serve(call, cx).await, Request::DeleteBackup(call) => serve(call, cx).await, diff --git a/crates/server/src/lib.rs b/crates/server/src/lib.rs index db30f81..1044106 100644 --- a/crates/server/src/lib.rs +++ b/crates/server/src/lib.rs @@ -65,7 +65,7 @@ fn sibling_directory(database_path: &str, name: &str) -> Result Result<(), ServerError> { +pub async fn showtime() -> Result { let _ = rustls::crypto::aws_lc_rs::default_provider().install_default(); let config = load_config()?; diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index f2bb16a..72ecddc 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -1,5 +1,6 @@ use common::model::Role; -use server::{set_role, showtime}; +use server::{network::Exit, set_role, showtime}; +use std::os::unix::process::CommandExt; #[tokio::main] async fn main() -> Result<(), server::error::ServerError> { @@ -16,7 +17,14 @@ async fn main() -> Result<(), server::error::ServerError> { { ["builder", username] => set_role(username, Role::Builder).await?, ["unbuilder", username] => set_role(username, Role::User).await?, - _ => return showtime().await, + _ => { + if let Exit::Restart = showtime().await? { + let mut args = std::env::args_os(); + let program = args.next().unwrap_or_default(); + return Err(std::process::Command::new(program).args(args).exec().into()); + } + return Ok(()); + } } println!("ok"); diff --git a/crates/server/src/network.rs b/crates/server/src/network.rs index c0c2149..3f792b9 100644 --- a/crates/server/src/network.rs +++ b/crates/server/src/network.rs @@ -32,6 +32,12 @@ use crate::{ }; const HELLO_DEADLINE: Duration = Duration::from_secs(5); +const RESTART_DRAIN: Duration = Duration::from_secs(2); + +pub enum Exit { + Stopped, + Restart, +} #[derive(Clone, Default)] pub struct Services { @@ -40,6 +46,7 @@ pub struct Services { pub rate_limit: RateLimit, pub stats: Follows, pub machine: Machine, + pub restart: Arc, } #[derive(Clone)] @@ -65,7 +72,7 @@ pub async fn serve( directory: PathBuf, backups: PathBuf, releases: crate::update::Releases, -) -> Result<(), ServerError> { +) -> Result { let endpoint = transport::endpoint( server_address.parse()?, quinn::EndpointConfig::default(), @@ -84,11 +91,22 @@ pub async fn serve( tokio::spawn(retention::collect(shared.clone())); - while let Some(incoming) = endpoint.accept().await { - tokio::spawn(handle_connection(incoming, shared.clone())); + let restart = shared.services.restart.clone(); + loop { + tokio::select! { + incoming = endpoint.accept() => match incoming { + Some(incoming) => { + tokio::spawn(handle_connection(incoming, shared.clone())); + } + None => return Ok(Exit::Stopped), + }, + _ = restart.notified() => break, + } } - - Ok(()) + info!("restarting"); + endpoint.close(Close::Restart.code(), Close::Restart.reason().as_bytes()); + let _ = tokio::time::timeout(RESTART_DRAIN, endpoint.wait_idle()).await; + Ok(Exit::Restart) } async fn handle_connection(incoming: Incoming, shared: Shared) {