Handling rush conditions on collection/show/season register

This commit is contained in:
Zoe Roux 2020-06-12 04:31:27 +02:00
parent 51fa75214b
commit 33d9c5d9b4
3 changed files with 38 additions and 30 deletions

View File

@ -49,21 +49,6 @@ namespace Kyoo
} }
public class DatabaseFactory
{
private readonly DbContextOptions<DatabaseContext> _options;
public DatabaseFactory(DbContextOptions<DatabaseContext> options)
{
_options = options;
}
public DatabaseContext NewDatabaseConnection()
{
return new DatabaseContext(_options);
}
}
public class DatabaseContext : DbContext public class DatabaseContext : DbContext
{ {
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { } public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { }

View File

@ -43,11 +43,6 @@ namespace Kyoo
services.AddControllers().AddNewtonsoftJson(); services.AddControllers().AddNewtonsoftJson();
services.AddHttpClient(); services.AddHttpClient();
services.AddSingleton(x => new DatabaseFactory(
new DbContextOptionsBuilder<DatabaseContext>()
.UseLazyLoadingProxies()
.UseNpgsql(_configuration.GetConnectionString("Database")).Options));
services.AddDbContext<DatabaseContext>(options => services.AddDbContext<DatabaseContext>(options =>
{ {
options.UseLazyLoadingProxies() options.UseLazyLoadingProxies()

View File

@ -7,6 +7,7 @@ using System.Linq;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Kyoo.Models.Exceptions;
using Kyoo.Models.Watch; using Kyoo.Models.Watch;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -40,7 +41,9 @@ namespace Kyoo.Controllers
return null; return null;
} }
public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string argument = null) public async Task Run(IServiceProvider serviceProvider,
CancellationToken cancellationToken,
string argument = null)
{ {
_serviceProvider = serviceProvider; _serviceProvider = serviceProvider;
_thumbnailsManager = serviceProvider.GetService<IThumbnailsManager>(); _thumbnailsManager = serviceProvider.GetService<IThumbnailsManager>();
@ -68,7 +71,6 @@ namespace Kyoo.Controllers
library.Providers = library.Providers; library.Providers = library.Providers;
await Task.WhenAll(libraries.Select(x => Scan(x, episodes, cancellationToken)).ToArray()); await Task.WhenAll(libraries.Select(x => Scan(x, episodes, cancellationToken)).ToArray());
Console.WriteLine("Done.");
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -111,6 +113,8 @@ namespace Kyoo.Controllers
return Task.CompletedTask; return Task.CompletedTask;
} }
// TODO Should register shows first to prevent all tasks of a same show to register it 150+ times.
return Task.WhenAll(files.Select(file => return Task.WhenAll(files.Select(file =>
{ {
if (!IsVideo(file) || episodes.Any(x => x.Path == file)) if (!IsVideo(file) || episodes.Any(x => x.Path == file))
@ -118,6 +122,7 @@ namespace Kyoo.Controllers
string relativePath = file.Substring(path.Length); string relativePath = file.Substring(path.Length);
return RegisterFile(file, relativePath, library, cancellationToken); return RegisterFile(file, relativePath, library, cancellationToken);
}).ToArray()); }).ToArray());
}).ToArray()); }).ToArray());
} }
@ -166,9 +171,17 @@ namespace Kyoo.Controllers
if (collection != null) if (collection != null)
return collection; return collection;
collection = await _metadataProvider.GetCollectionFromName(collectionName, library); collection = await _metadataProvider.GetCollectionFromName(collectionName, library);
try
{
await libraryManager.RegisterCollection(collection); await libraryManager.RegisterCollection(collection);
return collection; return collection;
} }
catch (DuplicatedItemException)
{
return await libraryManager.GetCollection(collection.Slug);
}
}
private async Task<Show> GetShow(ILibraryManager libraryManager, private async Task<Show> GetShow(ILibraryManager libraryManager,
string showTitle, string showTitle,
@ -182,11 +195,19 @@ namespace Kyoo.Controllers
show = await _metadataProvider.SearchShow(showTitle, isMovie, library); show = await _metadataProvider.SearchShow(showTitle, isMovie, library);
show.Path = showPath; show.Path = showPath;
show.People = await _metadataProvider.GetPeople(show, library); show.People = await _metadataProvider.GetPeople(show, library);
try
{
await libraryManager.RegisterShow(show); await libraryManager.RegisterShow(show);
await _thumbnailsManager.Validate(show.People); await _thumbnailsManager.Validate(show.People);
await _thumbnailsManager.Validate(show); await _thumbnailsManager.Validate(show);
return show; return show;
} }
catch (DuplicatedItemException)
{
return await libraryManager.GetShow(show.Slug);
}
}
private async Task<Season> GetSeason(ILibraryManager libraryManager, private async Task<Season> GetSeason(ILibraryManager libraryManager,
Show show, Show show,
@ -199,9 +220,16 @@ namespace Kyoo.Controllers
if (season == null) if (season == null)
{ {
season = await _metadataProvider.GetSeason(show, seasonNumber, library); season = await _metadataProvider.GetSeason(show, seasonNumber, library);
try
{
await libraryManager.RegisterSeason(season); await libraryManager.RegisterSeason(season);
await _thumbnailsManager.Validate(season); await _thumbnailsManager.Validate(season);
} }
catch (DuplicatedItemException)
{
season = await libraryManager.GetSeason(show.Slug, season.SeasonNumber);
}
}
season.Show = show; season.Show = show;
return season; return season;
} }