Skip to content

Commit

Permalink
Fix bug in cache reset: oversee command was long running task that wa…
Browse files Browse the repository at this point in the history
…s executed inside one transaction
  • Loading branch information
ImoutoChan committed Nov 24, 2023
1 parent 3cd7832 commit 17b6230
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 85 deletions.
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task FileInSourceFolderShouldBeAddedToDatabaseAndMovedToDestination
// act
var file = new FileInfo(Path.Combine(_webApp.TestsLocation, "Resources", "file1-5f30f9953332c230d11e3f26db5ae9a0.jpg"));
file.CopyTo(Path.Combine(sourceFolderPath, file.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
var savedFile = await _context.CollectionFiles.FirstOrDefaultAsync(x => x.CollectionId == collectionId);
Expand Down Expand Up @@ -132,7 +132,7 @@ public async Task FileInSourceFolderWithoutDestinationShouldBeAddedToDatabaseAnd
var testFilePath = Path.Combine(innerSourceFolderPath, file.Name);
file.CopyTo(testFilePath);

await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
var savedFile = await _context.CollectionFiles.FirstOrDefaultAsync(x => x.CollectionId == collectionId);
Expand Down Expand Up @@ -174,10 +174,10 @@ public async Task DuplicateImagesAreDeletedFromSourceFolder()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -201,10 +201,10 @@ public async Task ImagesWithSameMd5AreDeletedFromSourceFolder()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

testFile.CopyTo(Path.Combine(sourceFolderPath, "diff-name" + testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -229,10 +229,10 @@ public async Task ImagesWithSameNameButDifferentMd5AreProcessedCorrectly()

// act
testFile1.CopyTo(Path.Combine(sourceFolderPath, testFile1.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

testFile2.CopyTo(Path.Combine(sourceFolderPath, testFile1.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -256,7 +256,7 @@ public async Task BrokenImagesAreMovedToFormatErrorFolder()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -281,7 +281,7 @@ public async Task BrokenImagesAreNotMovedToFormatErrorFolderWhenFormatCheckIsDis

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -307,7 +307,7 @@ public async Task WrongHashImagesAreMovedToHashErrorFolder()
// act
// rename to wrong hash
testFile.CopyTo(Path.Combine(sourceFolderPath, "file1-1f30f9953332c230d11e3f26db5ae9a0.jpg"));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -333,7 +333,7 @@ public async Task WrongHashImagesAreNotMovedToHashErrorFolderWhenHashCheckIsDisa
// act
// rename to wrong hash
testFile.CopyTo(Path.Combine(sourceFolderPath, "file1-1f30f9953332c230d11e3f26db5ae9a0.jpg"));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -358,7 +358,7 @@ public async Task NoHashImagesAreMovedToNoHashFolder()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, "empty-file.jpg"));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -383,7 +383,7 @@ public async Task NoHashImagesAreNotMovedToNoHashFolderWhenHashCheckIsDisabled()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, "empty-file.jpg"));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -408,7 +408,7 @@ public async Task ImageNameIsPushedAsTag()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand Down Expand Up @@ -440,7 +440,7 @@ public async Task ImageNameIsNotPushedAsTagWhenShouldAddTagFromFilenameDisabled(

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand Down Expand Up @@ -472,7 +472,7 @@ public async Task ImageSubfolderIsPushedAsTag()
var testFilePath = Path.Combine(innerSourceFolderPath, file.Name);
file.CopyTo(testFilePath);

await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
var savedFile = await _context.CollectionFiles.FirstAsync(x => x.CollectionId == collectionId);
Expand Down Expand Up @@ -508,7 +508,7 @@ public async Task ImageSubfolderIsNotPushedAsTagWhenShouldCreateTagsFromSubfolde
var testFilePath = Path.Combine(innerSourceFolderPath, file.Name);
file.CopyTo(testFilePath);

await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
var savedFile = await _context.CollectionFiles.FirstAsync(x => x.CollectionId == collectionId);
Expand Down Expand Up @@ -542,7 +542,7 @@ public async Task ImagesAreIgnoredWhenArentMatchedByExtensions()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().NotBeEmpty();
Expand All @@ -566,7 +566,7 @@ public async Task ImagesAreNotIgnoredWhenExtensionsFilterIsEmpty()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -590,7 +590,7 @@ public async Task ImagesAreNotIgnoredWhenExtensionsFilterIsMatched()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
Directory.GetFiles(sourceFolderPath).Should().BeEmpty();
Expand All @@ -614,7 +614,7 @@ public async Task FileIsRenamedToHash()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
_context.CollectionFiles.Count(x => x.CollectionId == collectionId).Should().Be(1);
Expand All @@ -640,7 +640,7 @@ public async Task FileIsNotRenamedToHashWhenShouldRenameByHashIsDisabled()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
_context.CollectionFiles.Count(x => x.CollectionId == collectionId).Should().Be(1);
Expand All @@ -666,7 +666,7 @@ public async Task FileIsMovedToHashFolder()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
_context.CollectionFiles.Count(x => x.CollectionId == collectionId).Should().Be(1);
Expand All @@ -693,7 +693,7 @@ public async Task FileIsNotMovedToHashFolder()

// act
testFile.CopyTo(Path.Combine(sourceFolderPath, testFile.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
_context.CollectionFiles.Count(x => x.CollectionId == collectionId).Should().Be(1);
Expand All @@ -720,10 +720,10 @@ public async Task FileIsMovedToHashFolderIsNotRenamedWithSameName()

// act
testFile1.CopyTo(Path.Combine(sourceFolderPath, testFile1.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

testFile2.CopyTo(Path.Combine(sourceFolderPath, testFile1.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
_context.CollectionFiles.Count(x => x.CollectionId == collectionId).Should().Be(2);
Expand Down Expand Up @@ -753,10 +753,10 @@ public async Task FileIsMovedToDestinationWithSameName()

// act
testFile1.CopyTo(Path.Combine(sourceFolderPath, testFile1.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

testFile2.CopyTo(Path.Combine(sourceFolderPath, testFile1.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
_context.CollectionFiles.Count(x => x.CollectionId == collectionId).Should().Be(2);
Expand Down Expand Up @@ -787,13 +787,13 @@ public async Task MultipleFileIsMovedToDestinationWithSameName()

// act
testFile1.CopyTo(Path.Combine(sourceFolderPath, testFile1.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

testFile2.CopyTo(Path.Combine(sourceFolderPath, testFile1.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

testFile3.CopyTo(Path.Combine(sourceFolderPath, testFile1.Name));
await _mediator.Send(new OverseeCommand(false));
await _mediator.Send(new OverseeCommand());

// assert
_context.CollectionFiles.Count(x => x.CollectionId == collectionId).Should().Be(3);
Expand Down
Loading

0 comments on commit 17b6230

Please sign in to comment.