From 90ce02068efa96a4d7ad49229d71d66e2aa86ec3 Mon Sep 17 00:00:00 2001 From: Fooxboy Date: Thu, 2 Nov 2023 21:32:30 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BF=D0=BE=D0=B4=D0=B4=D0=B5=D1=80=D0=B6=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=84=D0=BE=D0=BD=D0=BE=D0=B2=D1=8B=D1=85=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B4=D0=B0=D1=87=20=D0=B8=20=D1=81=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B0=D0=BD=D0=B0=20=D1=84=D0=BE=D0=BD=D0=BE=D0=B2=D0=B0=D1=8F?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=B0=20=D1=83=D0=B4=D0=B0?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D1=8F=20=D1=81=D0=B5=D1=81=D1=81=D0=B8?= =?UTF-8?q?=D0=B9=20=D0=B1=D0=B5=D0=B7=20=D0=B8=D0=B3=D1=80=D0=BE=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BackgroundTasks/BackgroundTaskRunner.cs | 38 +++++++++++++++++++ .../BackgroundTasks/IBackgroundTask.cs | 8 ++++ .../BackgroundTasks/SessionCleaner.cs | 36 ++++++++++++++++++ MyOwnGame.Backend/Managers/SessionsManager.cs | 6 ++- MyOwnGame.Backend/MyOwnGame.Backend.csproj | 1 + MyOwnGame.Backend/Program.cs | 9 +++++ 6 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 MyOwnGame.Backend/BackgroundTasks/BackgroundTaskRunner.cs create mode 100644 MyOwnGame.Backend/BackgroundTasks/IBackgroundTask.cs create mode 100644 MyOwnGame.Backend/BackgroundTasks/SessionCleaner.cs diff --git a/MyOwnGame.Backend/BackgroundTasks/BackgroundTaskRunner.cs b/MyOwnGame.Backend/BackgroundTasks/BackgroundTaskRunner.cs new file mode 100644 index 0000000..7253098 --- /dev/null +++ b/MyOwnGame.Backend/BackgroundTasks/BackgroundTaskRunner.cs @@ -0,0 +1,38 @@ +using MyOwnGame.Backend.Helpers; + +namespace MyOwnGame.Backend.BackgroundTasks; + +public class BackgroundTaskRunner +{ + private readonly IEnumerable _backgroundTasks; + + public BackgroundTaskRunner(IEnumerable backgroundTasks) + { + _backgroundTasks = backgroundTasks; + } + + public void Run() + { + foreach (var backgroundTask in _backgroundTasks) + { + var thread = new Thread(async () => + { + await RunThread(backgroundTask); + }); + + thread.Name = nameof(backgroundTask); + + thread.Start(); + } + } + + private async Task RunThread(IBackgroundTask task) + { + while (true) + { + await task.Invoke(); + + Thread.Sleep(task.Timeout * 1000); + } + } +} \ No newline at end of file diff --git a/MyOwnGame.Backend/BackgroundTasks/IBackgroundTask.cs b/MyOwnGame.Backend/BackgroundTasks/IBackgroundTask.cs new file mode 100644 index 0000000..ea22d47 --- /dev/null +++ b/MyOwnGame.Backend/BackgroundTasks/IBackgroundTask.cs @@ -0,0 +1,8 @@ +namespace MyOwnGame.Backend.BackgroundTasks; + +public interface IBackgroundTask +{ + public Task Invoke(); + + public int Timeout { get; } +} \ No newline at end of file diff --git a/MyOwnGame.Backend/BackgroundTasks/SessionCleaner.cs b/MyOwnGame.Backend/BackgroundTasks/SessionCleaner.cs new file mode 100644 index 0000000..79774aa --- /dev/null +++ b/MyOwnGame.Backend/BackgroundTasks/SessionCleaner.cs @@ -0,0 +1,36 @@ +using MyOwnGame.Backend.Managers; + +namespace MyOwnGame.Backend.BackgroundTasks; + +public class SessionCleaner : IBackgroundTask +{ + private readonly SessionsManager _sessionsManager; + + private readonly ILogger _logger; + + public SessionCleaner(SessionsManager sessionsManager, ILogger logger) + { + _sessionsManager = sessionsManager; + _logger = logger; + } + + public int Timeout => 3600; + + public Task Invoke() + { + _logger.LogInformation("Поиск сессий, где больше нет игроков"); + + var sessions = _sessionsManager + .GetSessions() + .Where(s=> s.Value.Players.Count(p=> !p.IsDisconnected) == 0) + .ToList(); + + foreach (var session in sessions) + { + _logger.LogInformation($"Уничтожена сессия {session.Key}"); + _sessionsManager.CloseSession(session.Key); + } + + return Task.CompletedTask; + } +} \ No newline at end of file diff --git a/MyOwnGame.Backend/Managers/SessionsManager.cs b/MyOwnGame.Backend/Managers/SessionsManager.cs index 75d9631..7fcbeda 100644 --- a/MyOwnGame.Backend/Managers/SessionsManager.cs +++ b/MyOwnGame.Backend/Managers/SessionsManager.cs @@ -23,11 +23,15 @@ public Session CreateSession(Package package, long number) _sessions.Add(number, session); - _logger.LogInformation($"Добавлена новая сессия с номером '{number}'"); return session; } + public Dictionary GetSessions() + { + return _sessions; + } + public Session? GetSessionById(long id) { return !_sessions.TryGetValue(id, out var session) ? null : session; diff --git a/MyOwnGame.Backend/MyOwnGame.Backend.csproj b/MyOwnGame.Backend/MyOwnGame.Backend.csproj index 7efdffa..f76f11b 100644 --- a/MyOwnGame.Backend/MyOwnGame.Backend.csproj +++ b/MyOwnGame.Backend/MyOwnGame.Backend.csproj @@ -26,6 +26,7 @@ + diff --git a/MyOwnGame.Backend/Program.cs b/MyOwnGame.Backend/Program.cs index 6891760..77d37cd 100644 --- a/MyOwnGame.Backend/Program.cs +++ b/MyOwnGame.Backend/Program.cs @@ -1,3 +1,4 @@ +using MyOwnGame.Backend.BackgroundTasks; using MyOwnGame.Backend.Helpers; using MyOwnGame.Backend.Hubs; using MyOwnGame.Backend.Managers; @@ -68,6 +69,10 @@ public static void Main(string[] args) builder.Services.AddTransient(); builder.Services.AddTransient(); + //Регистрация фоновых задач + builder.Services.AddSingleton(); + builder.Services.AddSingleton(); + builder.Services.AddCors(options => { @@ -100,6 +105,10 @@ public static void Main(string[] args) app.UseCors("any"); + var backgroundTaskRunner = app.Services.GetRequiredService(); + + backgroundTaskRunner.Run(); + app.Run(); } }