From d95d613a57792be99baf5a7baa4350052850dd7b Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Mon, 6 Nov 2023 16:34:52 +0100 Subject: [PATCH] Added basic restarting for instances running in docker --- Moonlight/App/Event/Events.cs | 1 + .../App/Services/Sys/MoonlightService.cs | 27 +++++++++++++++++++ Moonlight/Program.cs | 8 +++++- Moonlight/Properties/launchSettings.json | 2 +- .../Components/Alerts/RestartAlert.razor | 18 +++++++++++++ Moonlight/Shared/Layouts/MainLayout.razor | 25 +++++++++++++---- Moonlight/Shared/Layouts/OverlayLayout.razor | 12 ++++++++- Moonlight/Shared/Views/Admin/Sys/Index.razor | 14 +++++++++- 8 files changed, 98 insertions(+), 9 deletions(-) create mode 100644 Moonlight/App/Services/Sys/MoonlightService.cs create mode 100644 Moonlight/Shared/Components/Alerts/RestartAlert.razor diff --git a/Moonlight/App/Event/Events.cs b/Moonlight/App/Event/Events.cs index d296728..37cdf75 100644 --- a/Moonlight/App/Event/Events.cs +++ b/Moonlight/App/Event/Events.cs @@ -19,4 +19,5 @@ public class Events public static EventHandler OnPostLiked; public static EventHandler OnPostCommentCreated; public static EventHandler OnPostCommentDeleted; + public static EventHandler OnMoonlightRestart; } \ No newline at end of file diff --git a/Moonlight/App/Services/Sys/MoonlightService.cs b/Moonlight/App/Services/Sys/MoonlightService.cs new file mode 100644 index 0000000..4e2db0f --- /dev/null +++ b/Moonlight/App/Services/Sys/MoonlightService.cs @@ -0,0 +1,27 @@ +using Moonlight.App.Event; +using Moonlight.App.Extensions; +using Moonlight.App.Helpers; + +namespace Moonlight.App.Services.Sys; + +public class MoonlightService // This service can be used to perform strictly panel specific actions +{ + private readonly ConfigService ConfigService; + public WebApplication Application { get; set; } // Do NOT modify using a plugin + + public MoonlightService(ConfigService configService) + { + ConfigService = configService; + } + + public async Task Restart() + { + Logger.Info("Restarting moonlight"); + + // Notify all users that this instance will restart + await Events.OnMoonlightRestart.InvokeAsync(); + await Task.Delay(TimeSpan.FromSeconds(3)); + + await Application.StopAsync(); + } +} \ No newline at end of file diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index 2300633..d2cf345 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -12,10 +12,14 @@ using Moonlight.App.Services.Community; using Moonlight.App.Services.Interop; using Moonlight.App.Services.ServiceManage; using Moonlight.App.Services.Store; +using Moonlight.App.Services.Sys; using Moonlight.App.Services.Users; using Moonlight.App.Services.Utils; using Serilog; +var configService = new ConfigService(); +var moonlightService = new MoonlightService(configService); + Directory.CreateDirectory(PathBuilder.Dir("storage")); Directory.CreateDirectory(PathBuilder.Dir("storage", "logs")); @@ -77,10 +81,11 @@ builder.Services.AddSingleton(); // Services builder.Services.AddScoped(); -builder.Services.AddSingleton(); +builder.Services.AddSingleton(configService); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(moonlightService); builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); @@ -97,6 +102,7 @@ var config = builder.Logging.AddConfiguration(config.Build()); var app = builder.Build(); +moonlightService.Application = app; app.UseStaticFiles(); app.UseRouting(); diff --git a/Moonlight/Properties/launchSettings.json b/Moonlight/Properties/launchSettings.json index a03e9d9..e493d52 100644 --- a/Moonlight/Properties/launchSettings.json +++ b/Moonlight/Properties/launchSettings.json @@ -3,7 +3,7 @@ "http": { "commandName": "Project", "dotnetRunMessages": true, - "launchBrowser": true, + "launchBrowser": false, "applicationUrl": "http://localhost:5132", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/Moonlight/Shared/Components/Alerts/RestartAlert.razor b/Moonlight/Shared/Components/Alerts/RestartAlert.razor new file mode 100644 index 0000000..b383513 --- /dev/null +++ b/Moonlight/Shared/Components/Alerts/RestartAlert.razor @@ -0,0 +1,18 @@ +@inject NavigationManager Navigation + +
+
+
+

+ Restarting +

+
+ The panel is restarting. This may take a moment +
+
+ +
+ Reconnect +
+
+
\ No newline at end of file diff --git a/Moonlight/Shared/Layouts/MainLayout.razor b/Moonlight/Shared/Layouts/MainLayout.razor index d1e5097..cf4f018 100644 --- a/Moonlight/Shared/Layouts/MainLayout.razor +++ b/Moonlight/Shared/Layouts/MainLayout.razor @@ -2,6 +2,7 @@ @using Moonlight.App.Models.Abstractions @using Moonlight.App.Models.Enums @using Moonlight.Shared.Components.Auth +@using Moonlight.App.Event @inherits LayoutComponentBase @implements IDisposable @@ -11,6 +12,7 @@ @inject IdentityService IdentityService @inject SessionService SessionService @inject NavigationManager Navigation +@inject IJSRuntime JsRuntime @{ var url = new Uri(Navigation.Uri); @@ -23,13 +25,19 @@ if (!IdentityService.Flags[UserFlag.MailVerified] && ConfigService.Get().Security.EnableEmailVerify) { - + } else if (IdentityService.Flags[UserFlag.PasswordPending]) { - + + + } + else if (RestartLock) + { + + } else @@ -48,19 +56,19 @@ if (url.LocalPath == "/register") { - + } else if (url.LocalPath == "/password-reset") { - + } else { - + } } @@ -90,6 +98,7 @@ else @code { private bool Initialized = false; + private bool RestartLock = false; private Session? MySession; @@ -114,6 +123,12 @@ else MySession.UpdatedAt = DateTime.UtcNow; } }; + + Events.OnMoonlightRestart += async (_, _) => + { + RestartLock = true; + await InvokeAsync(StateHasChanged); + }; } protected override async Task OnAfterRenderAsync(bool firstRender) diff --git a/Moonlight/Shared/Layouts/OverlayLayout.razor b/Moonlight/Shared/Layouts/OverlayLayout.razor index 50da509..9c4a22e 100644 --- a/Moonlight/Shared/Layouts/OverlayLayout.razor +++ b/Moonlight/Shared/Layouts/OverlayLayout.razor @@ -1,4 +1,14 @@ -