Skip to content

Commit

Permalink
tests thumbnail refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Oct 23, 2024
1 parent 9d38a0c commit cfbcfa7
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,35 +48,40 @@ public async Task StartBackgroundQueue(DateTime endTime)
var totalProcessed = 0;
var currentPage = 0;
const int batchSize = 100;


if ( _thumbnailQuery.IsRunningJobAsync() )
{
return;
}

//todo: add a check to see if the service is already running

List<ThumbnailItem> missingThumbnails;

do
{
// Query missing thumbnails in batches
missingThumbnails = await _thumbnailQuery.GetMissingThumbnailsBatchAsync(currentPage, batchSize);

missingThumbnails =
await _thumbnailQuery.GetMissingThumbnailsBatchAsync(currentPage, batchSize);

// Process each batch
var fileHashesList = missingThumbnails.Select(p => p.FileHash).ToList();
var queryItems = await _query.GetObjectsByFileHashAsync(fileHashesList);
if ( queryItems.Count == 0 )
{
break;
}

await _bgTaskQueue.QueueBackgroundWorkItemAsync(async _ =>
{
await WorkThumbnailGeneration(missingThumbnails, queryItems);
}, "DatabaseThumbnailGenerationService");


await _bgTaskQueue.QueueBackgroundWorkItemAsync(
async _ => { await WorkThumbnailGeneration(missingThumbnails, queryItems); },
"DatabaseThumbnailGenerationService");

totalProcessed += missingThumbnails.Count;
currentPage++;

_logger.LogInformation($"[DatabaseThumbnailGenerationService] Processed {totalProcessed} thumbnails so far...");
} while (missingThumbnails.Count == batchSize);
_logger.LogInformation(
$"[DatabaseThumbnailGenerationService] Processed {totalProcessed} thumbnails so far...");
} while ( missingThumbnails.Count == batchSize );
}

internal async Task<IEnumerable<ThumbnailItem>> WorkThumbnailGeneration(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ namespace starsky.foundation.database.Interfaces;

public interface IThumbnailQuery
{
Task<List<ThumbnailItem>?> AddThumbnailRangeAsync(List<ThumbnailResultDataTransferModel> thumbnailItems);
Task<List<ThumbnailItem>?> AddThumbnailRangeAsync(
List<ThumbnailResultDataTransferModel> thumbnailItems);

Task<List<ThumbnailItem>> Get(string? fileHash = null);
Task RemoveThumbnailsAsync(List<string> deletedFileHashes);
Task<bool> RenameAsync(string beforeFileHash, string newFileHash);
Task<List<ThumbnailItem>> GetMissingThumbnailsBatchAsync(int pageNumber, int pageSize);

/// <summary>
/// Update specific thumbnail item with data
/// Update specific thumbnail item with data
/// </summary>
/// <param name="item">the item received</param>
/// <returns></returns>
Task<bool> UpdateAsync(ThumbnailItem item);

bool IsRunningJob();
bool SetRunningJob(bool value);
}
43 changes: 29 additions & 14 deletions starsky/starsky.foundation.database/Thumbnails/ThumbnailQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using MySqlConnector;
using starsky.foundation.database.Data;
Expand All @@ -21,14 +22,16 @@ public class ThumbnailQuery : IThumbnailQuery
{
private readonly ApplicationDbContext _context;
private readonly IWebLogger _logger;
private readonly IMemoryCache _memoryCache;
private readonly IServiceScopeFactory? _scopeFactory;

public ThumbnailQuery(ApplicationDbContext context, IServiceScopeFactory? scopeFactory,
IWebLogger logger)
IWebLogger logger, IMemoryCache memoryCache)
{
_context = context;
_scopeFactory = scopeFactory;
_logger = logger;
_memoryCache = memoryCache;
}

public Task<List<ThumbnailItem>?> AddThumbnailRangeAsync(
Expand Down Expand Up @@ -141,6 +144,31 @@ public async Task<bool> UpdateAsync(ThumbnailItem item)
}
}

public bool IsRunningJob()
{
return _memoryCache.TryGetValue($"{nameof(ThumbnailQuery)}_IsRunningJob",
out bool isRunning) && isRunning;
}

public bool SetRunningJob(bool value)
{
_memoryCache.Set($"{nameof(ThumbnailQuery)}_IsRunningJob", value, TimeSpan.FromMinutes(30));
return true;
}

public async Task<List<ThumbnailItem>> GetMissingThumbnailsBatchAsync(int pageNumber,
int pageSize)
{
return await _context.Thumbnails
.Where(p => ( p.ExtraLarge == null
|| p.Large == null || p.Small == null )
&& !string.IsNullOrEmpty(p.FileHash))
.OrderBy(t => t.FileHash) // Ensure a consistent ordering
.Skip(pageNumber * pageSize)
.Take(pageSize)
.ToListAsync();
}

private async Task<List<ThumbnailItem>?> AddThumbnailRangeInternalRetryDisposedAsync(
List<ThumbnailResultDataTransferModel> thumbnailItems)
{
Expand Down Expand Up @@ -310,19 +338,6 @@ await dbContext.Thumbnails.AddRangeAsync(new ThumbnailItem(newFileHash,
return true;
}

public async Task<List<ThumbnailItem>> GetMissingThumbnailsBatchAsync(int pageNumber,
int pageSize)
{
return await _context.Thumbnails
.Where(p => ( p.ExtraLarge == null
|| p.Large == null || p.Small == null )
&& !string.IsNullOrEmpty(p.FileHash))
.OrderBy(t => t.FileHash) // Ensure a consistent ordering
.Skip(pageNumber * pageSize)
.Take(pageSize)
.ToListAsync();
}

internal static async Task<bool> UpdateInternalAsync(ApplicationDbContext dbContext,
ThumbnailItem item)
{
Expand Down
36 changes: 29 additions & 7 deletions starsky/starskytest/FakeMocks/FakeIThumbnailQuery.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
Expand All @@ -12,7 +13,7 @@ namespace starskytest.FakeMocks;

public class FakeIThumbnailQuery : IThumbnailQuery
{
private readonly List<ThumbnailItem> _content = new List<ThumbnailItem>();
private readonly List<ThumbnailItem> _content = new();

public FakeIThumbnailQuery(List<ThumbnailItem>? items = null)
{
Expand All @@ -23,20 +24,21 @@ public FakeIThumbnailQuery(List<ThumbnailItem>? items = null)
}

[SuppressMessage("ReSharper", "UnusedParameter.Local")]
public FakeIThumbnailQuery( ApplicationDbContext _, IServiceScope _2, IWebLogger _3)
public FakeIThumbnailQuery(ApplicationDbContext _, IServiceScope _2, IWebLogger _3)
{
// should bind to the context
}

public Task<List<ThumbnailItem>?> AddThumbnailRangeAsync(List<ThumbnailResultDataTransferModel> thumbnailItems)
public Task<List<ThumbnailItem>?> AddThumbnailRangeAsync(
List<ThumbnailResultDataTransferModel> thumbnailItems)
{
var results = new List<ThumbnailItem?>();
foreach ( var thumbnailItem in thumbnailItems )
{
var index = _content.FindIndex(p => p.FileHash == thumbnailItem.FileHash);
if ( index == -1 )
{
var item = new ThumbnailItem()
var item = new ThumbnailItem
{
FileHash = thumbnailItem.FileHash!,
Large = thumbnailItem.Large,
Expand All @@ -54,30 +56,38 @@ public FakeIThumbnailQuery( ApplicationDbContext _, IServiceScope _2, IWebLogger
{
_content[index].Large = thumbnailItem.Large;
}

if ( thumbnailItem.ExtraLarge != null )
{
_content[index].ExtraLarge = thumbnailItem.ExtraLarge;
}

if ( thumbnailItem.Reasons != null )
{
_content[index].Reasons = thumbnailItem.Reasons;
}

if ( thumbnailItem.Small != null )
{
_content[index].Small = thumbnailItem.Small;
}

if ( thumbnailItem.TinyMeta != null )
{
_content[index].TinyMeta = thumbnailItem.TinyMeta;
}

results.Add(_content[index]);
}

return Task.FromResult(results)!;
}

public Task<List<ThumbnailItem>> Get(string? fileHash = null)
{
return Task.FromResult(fileHash == null ? _content : _content.Where(p => p.FileHash == fileHash).ToList());
return Task.FromResult(fileHash == null
? _content
: _content.Where(p => p.FileHash == fileHash).ToList());
}

public Task RemoveThumbnailsAsync(List<string> deletedFileHashes)
Expand All @@ -88,11 +98,12 @@ public Task RemoveThumbnailsAsync(List<string> deletedFileHashes)

public Task<bool> RenameAsync(string beforeFileHash, string newFileHash)
{
var index = _content.FindIndex(p=> p.FileHash == beforeFileHash);
var index = _content.FindIndex(p => p.FileHash == beforeFileHash);
if ( index == -1 )
{
return Task.FromResult(false);
}

_content[index].FileHash = newFileHash;
return Task.FromResult(true);
}
Expand All @@ -110,12 +121,23 @@ public Task<List<ThumbnailItem>> GetMissingThumbnailsBatchAsync(int pageNumber,

public Task<bool> UpdateAsync(ThumbnailItem item)
{
var index = _content.FindIndex(p=> p.FileHash == item.FileHash);
var index = _content.FindIndex(p => p.FileHash == item.FileHash);
if ( index == -1 )
{
return Task.FromResult(false);
}

_content[index] = item;
return Task.FromResult(true);
}

public bool IsRunningJob()
{
throw new NotImplementedException();
}

public bool SetRunningJob(bool value)
{
throw new NotImplementedException();
}
}
Loading

0 comments on commit cfbcfa7

Please sign in to comment.