diff --git a/Kyoo/Tasks/Crawler.cs b/Kyoo/Tasks/Crawler.cs index 0604cd27..6a02005b 100644 --- a/Kyoo/Tasks/Crawler.cs +++ b/Kyoo/Tasks/Crawler.cs @@ -31,7 +31,7 @@ namespace Kyoo.Controllers public async Task> GetPossibleParameters() { using IServiceScope serviceScope = _serviceProvider.CreateScope(); - ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService(); + await using ILibraryManager libraryManager = serviceScope.ServiceProvider.GetService(); return (await libraryManager.GetLibraries()).Select(x => x.Slug); } @@ -82,10 +82,10 @@ namespace Kyoo.Controllers private Task Scan(Library library, IEnumerable episodes, CancellationToken cancellationToken) { Console.WriteLine($"Scanning library {library.Name} at {string.Join(", ", library.Paths)}."); - return Task.WhenAll(library.Paths.Select(path => + return Task.WhenAll(library.Paths.Select(async path => { if (cancellationToken.IsCancellationRequested) - return Task.CompletedTask; + return; string[] files; try @@ -94,35 +94,39 @@ namespace Kyoo.Controllers } catch (DirectoryNotFoundException) { - Console.Error.WriteLine($"The library's directory {path} could not be found (library slug: {library.Slug})"); - return Task.CompletedTask; + await Console.Error.WriteLineAsync($"The library's directory {path} could not be found (library slug: {library.Slug})"); + return; } catch (PathTooLongException) { - Console.Error.WriteLine($"The library's directory {path} is too long for this system. (library slug: {library.Slug})"); - return Task.CompletedTask; + await Console.Error.WriteLineAsync($"The library's directory {path} is too long for this system. (library slug: {library.Slug})"); + return; } catch (ArgumentException) { - Console.Error.WriteLine($"The library's directory {path} is invalid. (library slug: {library.Slug})"); - return Task.CompletedTask; + await Console.Error.WriteLineAsync($"The library's directory {path} is invalid. (library slug: {library.Slug})"); + return; } catch (UnauthorizedAccessException) { - Console.Error.WriteLine($"Permission denied: can't access library's directory at {path}. (library slug: {library.Slug})"); - return Task.CompletedTask; + await Console.Error.WriteLineAsync($"Permission denied: can't access library's directory at {path}. (library slug: {library.Slug})"); + return; } - // TODO Should register shows first to prevent all tasks of a same show to register it 150+ times. + List> shows = files + .Where(x => IsVideo(x) && episodes.All(y => y.Path != x)) + .GroupBy(Path.GetDirectoryName) + .ToList(); - return Task.WhenAll(files.Select(file => - { - if (!IsVideo(file) || episodes.Any(x => x.Path == file)) - return Task.CompletedTask; - string relativePath = file.Substring(path.Length); - return RegisterFile(file, relativePath, library, cancellationToken); - }).ToArray()); + await Task.WhenAll(shows + .Select(x => x.First()) + .Select(x => RegisterFile(x, x.Substring(path.Length), library, cancellationToken)) + .ToArray()); + await Task.WhenAll(shows + .SelectMany(x => x.Skip(1)) + .Select(x => RegisterFile(x, x.Substring(path.Length), library, cancellationToken)) + .ToArray()); }).ToArray()); } diff --git a/Kyoo/appsettings.json b/Kyoo/appsettings.json index 5abce11c..6d669b3b 100644 --- a/Kyoo/appsettings.json +++ b/Kyoo/appsettings.json @@ -8,12 +8,13 @@ "Default": "Warning", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information", - "Microsoft.EntityFrameworkCore.DbUpdateException": "None" + "Microsoft.EntityFrameworkCore.DbUpdateException": "None", + "Microsoft.EntityFrameworkCore.Update": "None" } }, "AllowedHosts": "*", "ConnectionStrings": { - "Database": "Server=127.0.0.1; Port=5432; Database=kyooDB; User Id=kyoo; Password=kyooPassword; Pooling=true;" + "Database": "Server=127.0.0.1; Port=5432; Database=kyooDB; User Id=kyoo; Password=kyooPassword; Pooling=true; MaxPoolSize=60;" }, "scheduledTasks": {