Moving crawler first call to a startup code

This commit is contained in:
Zoe Roux 2020-02-10 01:53:17 +01:00
parent 9d6da207fe
commit 46cfd6f3c7
8 changed files with 86 additions and 93 deletions

View File

@ -5,8 +5,6 @@ namespace Kyoo.Controllers
{
public interface ICrawler
{
void Start();
void Cancel();
Task StartAsync(CancellationToken cancellationToken);
}
}

@ -1 +1 @@
Subproject commit 1a7094979c79dd3e50760afbc6bf5b2286e7ab9d
Subproject commit 1831074c9d21ccc238409b6c21520780ca666bea

View File

@ -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;
}
}
}

View File

@ -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<DatabaseContext>().Database.EnsureCreated();
// Use the next line if the database is not SQLite (SQLite doesn't support complexe migrations).
// serviceScope.ServiceProvider.GetService<DatabaseContext>().Database.Migrate();;
IPluginManager pluginManager = serviceScope.ServiceProvider.GetService<IPluginManager>();
pluginManager.ReloadPlugins();
ICrawler crawler = serviceScope.ServiceProvider.GetService<ICrawler>();
await crawler.StartAsync(stoppingToken);
}
}
}
}

View File

@ -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");
}
}

View File

@ -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<DatabaseContext>().Database.EnsureCreated();;
// Use the next line if the database is not SQLite (SQLite doesn't support complexe migrations).
// serviceScope.ServiceProvider.GetService<DatabaseContext>().Database.Migrate();;
IPluginManager pluginManager = serviceScope.ServiceProvider.GetService<IPluginManager>();
pluginManager.ReloadPlugins();
ICrawler crawler = serviceScope.ServiceProvider.GetService<ICrawler>();
crawler.Start();
}
await host.RunAsync();
await CreateWebHostBuilder(args).Build().RunAsync();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>

View File

@ -45,6 +45,8 @@ namespace Kyoo
services.AddSingleton<IProviderManager, ProviderManager>();
services.AddScoped<ICrawler, Crawler>();
services.AddSingleton<IPluginManager, PluginManager>();
services.AddHostedService<StartupCode>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.