-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Добавлена поддержка фоновых задач и сделана фоновая задача удаления с…
…ессий без игроков
- Loading branch information
Showing
6 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
|
||
|
||
|
||
|
||
</ItemGroup> | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters