-
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.
Fix bug in cache reset: oversee command was long running task that wa…
…s executed inside one transaction
- Loading branch information
1 parent
3cd7832
commit 17b6230
Showing
4 changed files
with
143 additions
and
85 deletions.
There are no files selected for viewing
57 changes: 12 additions & 45 deletions
57
Source/ImoutoRebirth.Room/ImoutoRebirth.Room.Application/Cqrs/OverseeCommand.cs
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 |
---|---|---|
@@ -1,69 +1,36 @@ | ||
using ImoutoRebirth.Common.Cqrs.Abstract; | ||
using ImoutoRebirth.Room.Application.Services; | ||
using MediatR; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace ImoutoRebirth.Room.Application.Cqrs; | ||
|
||
public record OverseeCommand(bool RepeatWhenNewFilesAreDiscovered) : ICommand; | ||
public record OverseeCommand : ICommand<OverseeCollectionResult>; | ||
|
||
internal class OverseeCommandHandler : ICommandHandler<OverseeCommand> | ||
internal class OverseeCommandHandler : ICommandHandler<OverseeCommand, OverseeCollectionResult> | ||
{ | ||
private static readonly SemaphoreSlim SemaphoreSlim = new(1); | ||
private readonly IMediator _mediator; | ||
private readonly ICollectionRepository _collectionRepository; | ||
private readonly ILogger _logger; | ||
|
||
public OverseeCommandHandler( | ||
IMediator mediator, | ||
ICollectionRepository collectionRepository, | ||
ILogger<OverseeCommandHandler> logger) | ||
ICollectionRepository collectionRepository) | ||
{ | ||
_collectionRepository = collectionRepository; | ||
_logger = logger; | ||
_mediator = mediator; | ||
} | ||
|
||
public async Task Handle(OverseeCommand request, CancellationToken ct) | ||
public async Task<OverseeCollectionResult> Handle(OverseeCommand request, CancellationToken ct) | ||
{ | ||
if (!await SemaphoreSlim.WaitAsync(0, ct)) | ||
{ | ||
_logger.LogTrace("Oversee process have not finished yet"); | ||
return; | ||
} | ||
|
||
var runMoreTimes = 0; | ||
do | ||
{ | ||
var anyFileMoved = false; | ||
try | ||
{ | ||
var collectionIds = await _collectionRepository.GetAllIds(); | ||
var anyFileMoved = false; | ||
|
||
foreach (var id in collectionIds) | ||
{ | ||
var result = await _mediator.Send(new OverseeCollectionCommand(id), ct); | ||
anyFileMoved |= result.AnyFileMoved; | ||
} | ||
|
||
if (!request.RepeatWhenNewFilesAreDiscovered) | ||
break; | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Oversee process error"); | ||
break; | ||
} | ||
var collectionIds = await _collectionRepository.GetAllIds(); | ||
|
||
if (anyFileMoved) | ||
runMoreTimes = 10; | ||
|
||
runMoreTimes--; | ||
if (runMoreTimes > 0) | ||
await Task.Delay(500, ct); | ||
foreach (var id in collectionIds) | ||
{ | ||
var result = await _mediator.Send(new OverseeCollectionCommand(id), ct); | ||
anyFileMoved |= result.AnyFileMoved; | ||
} | ||
|
||
} while (runMoreTimes > 0); | ||
|
||
SemaphoreSlim.Release(); | ||
return new(anyFileMoved); | ||
} | ||
} |
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
Oops, something went wrong.