Skip to content

Commit

Permalink
Добавлена поддержка фоновых задач и сделана фоновая задача удаления с…
Browse files Browse the repository at this point in the history
…ессий без игроков
  • Loading branch information
Fooxboy committed Nov 2, 2023
1 parent cf630e1 commit 90ce020
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 1 deletion.
38 changes: 38 additions & 0 deletions MyOwnGame.Backend/BackgroundTasks/BackgroundTaskRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using MyOwnGame.Backend.Helpers;

namespace MyOwnGame.Backend.BackgroundTasks;

public class BackgroundTaskRunner
{
private readonly IEnumerable<IBackgroundTask> _backgroundTasks;

public BackgroundTaskRunner(IEnumerable<IBackgroundTask> 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);
}
}
}
8 changes: 8 additions & 0 deletions MyOwnGame.Backend/BackgroundTasks/IBackgroundTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MyOwnGame.Backend.BackgroundTasks;

public interface IBackgroundTask
{
public Task Invoke();

public int Timeout { get; }
}
36 changes: 36 additions & 0 deletions MyOwnGame.Backend/BackgroundTasks/SessionCleaner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using MyOwnGame.Backend.Managers;

namespace MyOwnGame.Backend.BackgroundTasks;

public class SessionCleaner : IBackgroundTask
{
private readonly SessionsManager _sessionsManager;

private readonly ILogger<SessionCleaner> _logger;

public SessionCleaner(SessionsManager sessionsManager, ILogger<SessionCleaner> 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;
}
}
6 changes: 5 additions & 1 deletion MyOwnGame.Backend/Managers/SessionsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ public Session CreateSession(Package package, long number)

_sessions.Add(number, session);


_logger.LogInformation($"Добавлена новая сессия с номером '{number}'");
return session;
}

public Dictionary<long, Session> GetSessions()
{
return _sessions;
}

public Session? GetSessionById(long id)
{
return !_sessions.TryGetValue(id, out var session) ? null : session;
Expand Down
1 change: 1 addition & 0 deletions MyOwnGame.Backend/MyOwnGame.Backend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@




</ItemGroup>


Expand Down
9 changes: 9 additions & 0 deletions MyOwnGame.Backend/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using MyOwnGame.Backend.BackgroundTasks;
using MyOwnGame.Backend.Helpers;
using MyOwnGame.Backend.Hubs;
using MyOwnGame.Backend.Managers;
Expand Down Expand Up @@ -68,6 +69,10 @@ public static void Main(string[] args)
builder.Services.AddTransient<FilesService>();
builder.Services.AddTransient<SessionCallbackService>();

//Регистрация фоновых задач
builder.Services.AddSingleton<IBackgroundTask, SessionCleaner>();
builder.Services.AddSingleton<BackgroundTaskRunner>();


builder.Services.AddCors(options =>
{
Expand Down Expand Up @@ -100,6 +105,10 @@ public static void Main(string[] args)

app.UseCors("any");

var backgroundTaskRunner = app.Services.GetRequiredService<BackgroundTaskRunner>();

backgroundTaskRunner.Run();

app.Run();
}
}
Expand Down

0 comments on commit 90ce020

Please sign in to comment.