Update campaign handling

This commit is contained in:
2025-10-27 16:56:08 +09:00
parent 445bfe8273
commit 6b3733001a
7 changed files with 86 additions and 11 deletions

View File

@ -8,16 +8,21 @@ namespace Hcs.WebApp.BackgroundServices
ManagerFactory managerFactory,
IServiceScopeFactory scopeFactory) : BackgroundService
{
private const int ITERATION_TIME = 1000;
private const int SLEEP_TIME = 30000;
private readonly CampaignManagementState campaignManagementState = campaignManagementState;
private readonly ManagerFactory managerFactory = managerFactory;
private readonly IServiceScopeFactory scopeFactory = scopeFactory;
private readonly List<IManager> managers = [];
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await InitializeStateAsync();
using var scope = scopeFactory.CreateScope();
var headquartersService = scope.ServiceProvider.GetRequiredService<HeadquartersService>();
while (!stoppingToken.IsCancellationRequested)
{
while (campaignManagementState.TryDequeueCampaign(out var campaign))
@ -28,15 +33,25 @@ namespace Hcs.WebApp.BackgroundServices
{
var manager = managerFactory.CreateManager(campaign);
await manager.StartAsync(stoppingToken);
managers.Add(manager);
}
catch
catch (Exception e)
{
// TODO: Добавить таймауты
campaignManagementState.EnqueueCampaign(campaign);
await headquartersService.SetCampaignEndedWithFail(campaign.Id, e.Message);
}
}
await Task.Delay(SLEEP_TIME, stoppingToken);
managers.RemoveAll(x => x.State == IManager.ManagerState.Ended);
if (managers.Count > 0)
{
await Task.Delay(ITERATION_TIME, stoppingToken);
}
else
{
await Task.Delay(SLEEP_TIME, stoppingToken);
}
}
}