From 46cfd6f3c71209aa72d769adc2ecbc299f37186c Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Mon, 10 Feb 2020 01:53:17 +0100 Subject: [PATCH] Moving crawler first call to a startup code --- Kyoo.Common/Controllers/ICrawler.cs | 4 +- Kyoo.Common/Models/Collection.cs | 88 ++++++++++++++--------------- Kyoo/ClientApp | 2 +- Kyoo/Controllers/Crawler.cs | 30 +--------- Kyoo/Controllers/StartupCode.cs | 34 +++++++++++ Kyoo/HtmlAPI/AdminAPI.cs | 3 +- Kyoo/Program.cs | 16 +----- Kyoo/Startup.cs | 2 + 8 files changed, 86 insertions(+), 93 deletions(-) create mode 100644 Kyoo/Controllers/StartupCode.cs diff --git a/Kyoo.Common/Controllers/ICrawler.cs b/Kyoo.Common/Controllers/ICrawler.cs index 6dc29922..2458d656 100644 --- a/Kyoo.Common/Controllers/ICrawler.cs +++ b/Kyoo.Common/Controllers/ICrawler.cs @@ -5,8 +5,6 @@ namespace Kyoo.Controllers { public interface ICrawler { - void Start(); - - void Cancel(); + Task StartAsync(CancellationToken cancellationToken); } } diff --git a/Kyoo.Common/Models/Collection.cs b/Kyoo.Common/Models/Collection.cs index ada746d0..590a38bc 100644 --- a/Kyoo.Common/Models/Collection.cs +++ b/Kyoo.Common/Models/Collection.cs @@ -6,53 +6,53 @@ namespace Kyoo.Models { public class Collection : IMergable { - [JsonIgnore] public long ID { get; set; } - public string Slug { get; set; } - public string Name { get; set; } - public string Poster { get; set; } - public string Overview { get; set; } - [JsonIgnore] public string ImgPrimary { get; set; } - public IEnumerable Shows; + [JsonIgnore] public long ID { get; set; } + public string Slug { get; set; } + public string Name { get; set; } + public string Poster { get; set; } + public string Overview { get; set; } + [JsonIgnore] public string ImgPrimary { get; set; } + public IEnumerable Shows; - public Collection() { } + public Collection() { } - public Collection(string slug, string name, string overview, string imgPrimary) - { - Slug = slug; - Name = name; - Overview = overview; - ImgPrimary = imgPrimary; - } - - public Show AsShow() - { - return new Show(Slug, Name, null, null, Overview, null, null, null, null, null, null) + public Collection(string slug, string name, string overview, string imgPrimary) { - IsCollection = true - }; - } + Slug = slug; + Name = name; + Overview = overview; + ImgPrimary = imgPrimary; + } - public Collection Merge(Collection collection) - { - if (collection == null) - return this; - if (ID == -1) - ID = collection.ID; - if (Slug == null) - Slug = collection.Slug; - if (Name == null) - Name = collection.Name; - if (Poster == null) - Poster = collection.Poster; - if (Overview == null) - Overview = collection.Overview; - if (ImgPrimary == null) - ImgPrimary = collection.ImgPrimary; - if (Shows == null) - Shows = collection.Shows; - else - Shows = Shows.Concat(collection.Shows); - return this; - } + public Show AsShow() + { + return new Show(Slug, Name, null, null, Overview, null, null, null, null, null, null) + { + IsCollection = true + }; + } + + public Collection Merge(Collection collection) + { + if (collection == null) + return this; + if (ID == -1) + ID = collection.ID; + if (Slug == null) + Slug = collection.Slug; + if (Name == null) + Name = collection.Name; + if (Poster == null) + Poster = collection.Poster; + if (Overview == null) + Overview = collection.Overview; + if (ImgPrimary == null) + ImgPrimary = collection.ImgPrimary; + if (Shows == null) + Shows = collection.Shows; + else + Shows = Shows.Concat(collection.Shows); + return this; + } } } diff --git a/Kyoo/ClientApp b/Kyoo/ClientApp index 1a709497..1831074c 160000 --- a/Kyoo/ClientApp +++ b/Kyoo/ClientApp @@ -1 +1 @@ -Subproject commit 1a7094979c79dd3e50760afbc6bf5b2286e7ab9d +Subproject commit 1831074c9d21ccc238409b6c21520780ca666bea diff --git a/Kyoo/Controllers/Crawler.cs b/Kyoo/Controllers/Crawler.cs index 7925bde6..70952f4c 100644 --- a/Kyoo/Controllers/Crawler.cs +++ b/Kyoo/Controllers/Crawler.cs @@ -13,9 +13,6 @@ namespace Kyoo.Controllers { public class Crawler : ICrawler { - private bool _isRunning; - private readonly CancellationTokenSource _cancellation; - private readonly ILibraryManager _libraryManager; private readonly IProviderManager _metadataProvider; private readonly ITranscoder _transcoder; @@ -27,26 +24,9 @@ namespace Kyoo.Controllers _metadataProvider = metadataProvider; _transcoder = transcoder; _config = configuration; - _cancellation = new CancellationTokenSource(); } - public void Start() - { - if (_isRunning) - return; - _isRunning = true; - StartAsync(_cancellation.Token); - } - - public void Cancel() - { - if (!_isRunning) - return; - _isRunning = false; - _cancellation.Cancel(); - } - - private async void StartAsync(CancellationToken cancellationToken) + public async Task StartAsync(CancellationToken cancellationToken) { try { @@ -66,7 +46,6 @@ namespace Kyoo.Controllers { Console.Error.WriteLine($"Unknown exception thrown durring libraries scan.\nException: {ex.Message}"); } - _isRunning = false; Console.WriteLine("Scan finished!"); } @@ -204,12 +183,5 @@ namespace Kyoo.Controllers { return VideoExtensions.Contains(Path.GetExtension(filePath)); } - - - public Task StopAsync() - { - _cancellation.Cancel(); - return null; - } } } diff --git a/Kyoo/Controllers/StartupCode.cs b/Kyoo/Controllers/StartupCode.cs new file mode 100644 index 00000000..2062fbb2 --- /dev/null +++ b/Kyoo/Controllers/StartupCode.cs @@ -0,0 +1,34 @@ +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace Kyoo.Controllers +{ + public class StartupCode : BackgroundService + { + private readonly IServiceProvider _serviceProvider; + + public StartupCode(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + using (IServiceScope serviceScope = _serviceProvider.CreateScope()) + { + serviceScope.ServiceProvider.GetService().Database.EnsureCreated(); + // Use the next line if the database is not SQLite (SQLite doesn't support complexe migrations). + // serviceScope.ServiceProvider.GetService().Database.Migrate();; + + IPluginManager pluginManager = serviceScope.ServiceProvider.GetService(); + pluginManager.ReloadPlugins(); + + ICrawler crawler = serviceScope.ServiceProvider.GetService(); + await crawler.StartAsync(stoppingToken); + } + } + } +} \ No newline at end of file diff --git a/Kyoo/HtmlAPI/AdminAPI.cs b/Kyoo/HtmlAPI/AdminAPI.cs index 2ac4ce86..23f9e4a1 100644 --- a/Kyoo/HtmlAPI/AdminAPI.cs +++ b/Kyoo/HtmlAPI/AdminAPI.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; namespace Kyoo.Controllers @@ -21,7 +22,7 @@ namespace Kyoo.Controllers [HttpGet("scan")] public IActionResult ScanLibrary() { - crawler.Start(); + crawler.StartAsync(new CancellationToken()); return Ok("Scanning"); } } diff --git a/Kyoo/Program.cs b/Kyoo/Program.cs index 8e68ac1f..70fa567d 100644 --- a/Kyoo/Program.cs +++ b/Kyoo/Program.cs @@ -11,22 +11,8 @@ namespace Kyoo { public static async Task Main(string[] args) { - IWebHost host = CreateWebHostBuilder(args).Build(); - Console.WriteLine($"Running as: {Environment.UserName}"); - using (IServiceScope serviceScope = host.Services.CreateScope()) - { - serviceScope.ServiceProvider.GetService().Database.EnsureCreated();; - // Use the next line if the database is not SQLite (SQLite doesn't support complexe migrations). - // serviceScope.ServiceProvider.GetService().Database.Migrate();; - - IPluginManager pluginManager = serviceScope.ServiceProvider.GetService(); - pluginManager.ReloadPlugins(); - - ICrawler crawler = serviceScope.ServiceProvider.GetService(); - crawler.Start(); - } - await host.RunAsync(); + await CreateWebHostBuilder(args).Build().RunAsync(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => diff --git a/Kyoo/Startup.cs b/Kyoo/Startup.cs index 29414f2b..1e0bdcbf 100644 --- a/Kyoo/Startup.cs +++ b/Kyoo/Startup.cs @@ -45,6 +45,8 @@ namespace Kyoo services.AddSingleton(); services.AddScoped(); services.AddSingleton(); + + services.AddHostedService(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.