Merge pull request #293 from cp89gamedev/just-oblivion-memes

Don't make the server exit when there is another server running
This commit is contained in:
CWX
2025-05-28 20:42:16 +01:00
committed by GitHub
3 changed files with 37 additions and 3 deletions
@@ -730,5 +730,6 @@
"chatbot-forced_event_enabled": "%s event has been enabled, restart your game client before starting a raid",
"chatbot-added_stash_rows_please_restart": "Added 2 rows to stash, please restart your game to see them",
"chatbot-snow_enabled": "Snow is enabled for all subsequent raids until the server is restarted",
"chatbot-summer_enabled": "Summer has been enabled for all subsequent raids until the server is restarted"
}
"chatbot-summer_enabled": "Summer has been enabled for all subsequent raids until the server is restarted",
"webserver_already_running": "Another copy of the webserver is already running. Please shut it down before starting a new one."
}
@@ -54,6 +54,28 @@ public class HttpServerHelper(ConfigServer configServer)
return $"wss://{BuildUrl()}";
}
/// <summary>
/// Method to determine if another version of the server is already running
/// </summary>
/// <returns>bool isAlreadyRunning</returns>
public async Task<bool> IsAlreadyRunning()
{
try
{
var handler = new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
};
var http = new HttpClient(handler);
var res = await http.PostAsync($"{GetBackendUrl()}/launcher/ping", null);
return res.IsSuccessStatusCode;
}
catch (Exception )
{
return false;
}
}
public void SendTextJson(HttpResponse resp, object output)
{
resp.Headers.Append("Content-Type", mime["json"]);
+12 -1
View File
@@ -1,5 +1,6 @@
using SPTarkov.DI.Annotations;
using SPTarkov.Server.Core.DI;
using SPTarkov.Server.Core.Helpers;
using SPTarkov.Server.Core.Models.Spt.Config;
using SPTarkov.Server.Core.Models.Utils;
using SPTarkov.Server.Core.Servers;
@@ -17,6 +18,7 @@ public class App
protected DatabaseService _databaseService;
protected EncodingUtil _encodingUtil;
protected HttpServer _httpServer;
protected HttpServerHelper _httpServerHelper;
protected LocalisationService _localisationService;
protected ISptLogger<App> _logger;
@@ -36,7 +38,8 @@ public class App
HttpServer httpServer,
DatabaseService databaseService,
IEnumerable<IOnLoad> onLoadComponents,
IEnumerable<IOnUpdate> onUpdateComponents
IEnumerable<IOnUpdate> onUpdateComponents,
HttpServerHelper httpServerHelper
)
{
_logger = logger;
@@ -46,6 +49,7 @@ public class App
_configServer = configServer;
_encodingUtil = encodingUtil;
_httpServer = httpServer;
_httpServerHelper = httpServerHelper;
_databaseService = databaseService;
_onLoad = onLoadComponents;
_onUpdate = onUpdateComponents;
@@ -58,6 +62,13 @@ public class App
// execute onLoad callbacks
_logger.Info(_localisationService.GetText("executing_startup_callbacks"));
var isAlreadyRunning = await _httpServerHelper.IsAlreadyRunning();
if (isAlreadyRunning)
{
_logger.Critical(_localisationService.GetText("webserver_already_running"));
await Task.Delay(Timeout.Infinite);
}
if (_logger.IsLogEnabled(LogLevel.Debug))
{
_logger.Debug($"OS: {Environment.OSVersion.Version} | {Environment.OSVersion.Platform}");