Added basic restarting for instances running in docker

This commit is contained in:
Marcel Baumgartner 2023-11-06 16:34:52 +01:00
parent 9e182768f6
commit d95d613a57
8 changed files with 98 additions and 9 deletions

View file

@ -19,4 +19,5 @@ public class Events
public static EventHandler<Post> OnPostLiked;
public static EventHandler<PostComment> OnPostCommentCreated;
public static EventHandler<PostComment> OnPostCommentDeleted;
public static EventHandler OnMoonlightRestart;
}

View file

@ -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();
}
}

View file

@ -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<ServiceAdminService>();
// Services
builder.Services.AddScoped<IdentityService>();
builder.Services.AddSingleton<ConfigService>();
builder.Services.AddSingleton(configService);
builder.Services.AddSingleton<SessionService>();
builder.Services.AddSingleton<BucketService>();
builder.Services.AddSingleton<MailService>();
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();

View file

@ -3,7 +3,7 @@
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchBrowser": false,
"applicationUrl": "http://localhost:5132",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"

View file

@ -0,0 +1,18 @@
@inject NavigationManager Navigation
<div class="w-100">
<div class="card-body">
<div class="text-start mb-8">
<h1 class="text-dark mb-3 fs-3x">
Restarting
</h1>
<div class="text-gray-400 fw-semibold fs-6">
The panel is restarting. This may take a moment
</div>
</div>
<div class="d-flex flex-stack">
<a href="javascript:location.reload()" class="btn btn-primary me-2 flex-shrink-0">Reconnect</a>
</div>
</div>
</div>

View file

@ -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)
{
<OverlayLayout>
<MailVerify />
<MailVerify/>
</OverlayLayout>
}
else if (IdentityService.Flags[UserFlag.PasswordPending])
{
<OverlayLayout>
<ChangePassword />
<ChangePassword/>
</OverlayLayout>
}
else if (RestartLock)
{
<OverlayLayout>
<RestartAlert/>
</OverlayLayout>
}
else
@ -48,19 +56,19 @@
if (url.LocalPath == "/register")
{
<OverlayLayout>
<Register />
<Register/>
</OverlayLayout>
}
else if (url.LocalPath == "/password-reset")
{
<OverlayLayout>
<PasswordReset />
<PasswordReset/>
</OverlayLayout>
}
else
{
<OverlayLayout>
<Login />
<Login/>
</OverlayLayout>
}
}
@ -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)

View file

@ -1,4 +1,14 @@
<div class="d-flex flex-column flex-root" id="kt_app_root">
@* disable any reconnect screens *@
<style>
#components-reconnect-modal
{
display: none !important;
pointer-events: none;
}
</style>
<div class="d-flex flex-column flex-root" id="kt_app_root">
<div class="d-flex flex-column flex-lg-row flex-column-fluid">
<a href="/" class="d-block d-lg-none mx-auto py-20">
<img alt="Logo" src="/metronic8/demo38/assets/media/logos/default.svg" class="theme-light-show h-25px">

View file

@ -2,7 +2,19 @@
@using Moonlight.App.Extensions.Attributes
@using Moonlight.App.Models.Enums
@using Moonlight.App.Services
@using Moonlight.App.Services.Sys
@attribute [RequirePermission(Permission.AdminRoot)]
<AdminSysNavigation Index="0" />
@inject MoonlightService MoonlightService
<AdminSysNavigation Index="0" />
<div class="row mt-5">
<div class="col-md-4 col-12">
<div class="card card-body">
<ConfirmButton OnClick="MoonlightService.Restart" Text="Restart" CssClasses="btn-danger" />
</div>
</div>
</div>