Chunk images redownload to prevent http timeouts

This commit is contained in:
Zoe Roux 2024-04-21 21:23:24 +02:00
parent aca7c815a0
commit 9e38c3f333
No known key found for this signature in database
2 changed files with 16 additions and 7 deletions

View File

@ -27,6 +27,7 @@ using Kyoo.Abstractions.Controllers;
using Kyoo.Abstractions.Models; using Kyoo.Abstractions.Models;
using Kyoo.Postgresql; using Kyoo.Postgresql;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Kyoo.Core.Controllers; namespace Kyoo.Core.Controllers;
@ -36,6 +37,12 @@ public class MiscRepository(
IThumbnailsManager thumbnails IThumbnailsManager thumbnails
) )
{ {
public static async Task DownloadMissingImages(IServiceProvider services)
{
await using AsyncServiceScope scope = services.CreateAsyncScope();
await scope.ServiceProvider.GetRequiredService<MiscRepository>().DownloadMissingImages();
}
private async Task<ICollection<Image>> _GetAllImages() private async Task<ICollection<Image>> _GetAllImages()
{ {
string GetSql(string type) => string GetSql(string type) =>
@ -60,11 +67,12 @@ public class MiscRepository(
public async Task DownloadMissingImages() public async Task DownloadMissingImages()
{ {
ICollection<Image> images = await _GetAllImages(); ICollection<Image> images = await _GetAllImages();
await Task.WhenAll( IEnumerable<Task> tasks = images
images .Where(x => !File.Exists(thumbnails.GetImagePath(x.Id, ImageQuality.Low)))
.Where(x => !File.Exists(thumbnails.GetImagePath(x.Id, ImageQuality.Low))) .Select(x => thumbnails.DownloadImage(x, x.Id.ToString()));
.Select(x => thumbnails.DownloadImage(x, x.Id.ToString())) // Chunk tasks to prevent http timouts
); foreach (IEnumerable<Task> batch in tasks.Chunk(30))
await Task.WhenAll(batch);
} }
public async Task<ICollection<string>> GetRegisteredPaths() public async Task<ICollection<string>> GetRegisteredPaths()

View File

@ -95,7 +95,8 @@ app.Services.GetRequiredService<RabbitProducer>();
await using (AsyncServiceScope scope = app.Services.CreateAsyncScope()) await using (AsyncServiceScope scope = app.Services.CreateAsyncScope())
{ {
await MeilisearchModule.Initialize(scope.ServiceProvider); await MeilisearchModule.Initialize(scope.ServiceProvider);
await scope.ServiceProvider.GetRequiredService<MiscRepository>().DownloadMissingImages();
} }
// The methods takes care of creating a scope and will download images on the background.
_ = MiscRepository.DownloadMissingImages(app.Services);
app.Run(Environment.GetEnvironmentVariable("KYOO_BIND_URL") ?? "http://*:5000"); await app.RunAsync(Environment.GetEnvironmentVariable("KYOO_BIND_URL") ?? "http://*:5000");