1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

Web server - Don't constantly restart during initialize

This commit is contained in:
Robert 2023-04-20 16:47:29 +02:00
parent de23b5449b
commit 18d69bbf42

View File

@ -17,14 +17,17 @@ internal class WebServerService : IWebServerService, IDisposable
{ {
private readonly List<WebApiControllerRegistration> _controllers; private readonly List<WebApiControllerRegistration> _controllers;
private readonly ILogger _logger; private readonly ILogger _logger;
private readonly ICoreService _coreService;
private readonly List<WebModuleRegistration> _modules; private readonly List<WebModuleRegistration> _modules;
private readonly PluginSetting<bool> _webServerEnabledSetting; private readonly PluginSetting<bool> _webServerEnabledSetting;
private readonly PluginSetting<int> _webServerPortSetting; private readonly PluginSetting<int> _webServerPortSetting;
private readonly object _webserverLock = new();
private CancellationTokenSource? _cts; private CancellationTokenSource? _cts;
public WebServerService(ILogger logger, ICoreService coreService, ISettingsService settingsService, IPluginManagementService pluginManagementService) public WebServerService(ILogger logger, ICoreService coreService, ISettingsService settingsService, IPluginManagementService pluginManagementService)
{ {
_logger = logger; _logger = logger;
_coreService = coreService;
_controllers = new List<WebApiControllerRegistration>(); _controllers = new List<WebApiControllerRegistration>();
_modules = new List<WebModuleRegistration>(); _modules = new List<WebModuleRegistration>();
@ -35,7 +38,10 @@ internal class WebServerService : IWebServerService, IDisposable
pluginManagementService.PluginFeatureDisabled += PluginManagementServiceOnPluginFeatureDisabled; pluginManagementService.PluginFeatureDisabled += PluginManagementServiceOnPluginFeatureDisabled;
PluginsModule = new PluginsModule("/plugins"); PluginsModule = new PluginsModule("/plugins");
StartWebServer(); if (coreService.IsInitialized)
StartWebServer();
else
coreService.Initialized += (_, _) => StartWebServer();
} }
public event EventHandler? WebServerStopped; public event EventHandler? WebServerStopped;
@ -144,21 +150,28 @@ internal class WebServerService : IWebServerService, IDisposable
private void StartWebServer() private void StartWebServer()
{ {
Server = CreateWebServer(); lock (_webserverLock)
if (!_webServerEnabledSetting.Value)
return;
if (Constants.StartupArguments.Contains("--disable-webserver"))
{ {
_logger.Warning("Artemis launched with --disable-webserver, not enabling the webserver"); // Don't create the webserver until after the core service is initialized, this avoids lots of useless re-creates during initialize
return; if (!_coreService.IsInitialized)
} return;
OnWebServerStarting(); if (!_webServerEnabledSetting.Value)
_cts = new CancellationTokenSource(); return;
Server.Start(_cts.Token);
OnWebServerStarted(); Server = CreateWebServer();
if (Constants.StartupArguments.Contains("--disable-webserver"))
{
_logger.Warning("Artemis launched with --disable-webserver, not enabling the webserver");
return;
}
OnWebServerStarting();
_cts = new CancellationTokenSource();
Server.Start(_cts.Token);
OnWebServerStarted();
}
} }
#endregion #endregion