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");
if (coreService.IsInitialized)
StartWebServer(); StartWebServer();
else
coreService.Initialized += (_, _) => StartWebServer();
} }
public event EventHandler? WebServerStopped; public event EventHandler? WebServerStopped;
@ -144,11 +150,17 @@ internal class WebServerService : IWebServerService, IDisposable
private void StartWebServer() private void StartWebServer()
{ {
Server = CreateWebServer(); lock (_webserverLock)
{
// Don't create the webserver until after the core service is initialized, this avoids lots of useless re-creates during initialize
if (!_coreService.IsInitialized)
return;
if (!_webServerEnabledSetting.Value) if (!_webServerEnabledSetting.Value)
return; return;
Server = CreateWebServer();
if (Constants.StartupArguments.Contains("--disable-webserver")) if (Constants.StartupArguments.Contains("--disable-webserver"))
{ {
_logger.Warning("Artemis launched with --disable-webserver, not enabling the webserver"); _logger.Warning("Artemis launched with --disable-webserver, not enabling the webserver");
@ -160,6 +172,7 @@ internal class WebServerService : IWebServerService, IDisposable
Server.Start(_cts.Token); Server.Start(_cts.Token);
OnWebServerStarted(); OnWebServerStarted();
} }
}
#endregion #endregion