mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-07 10:14:13 -04:00
Moving crawler first call to a startup code
This commit is contained in:
parent
9d6da207fe
commit
46cfd6f3c7
@ -5,8 +5,6 @@ namespace Kyoo.Controllers
|
|||||||
{
|
{
|
||||||
public interface ICrawler
|
public interface ICrawler
|
||||||
{
|
{
|
||||||
void Start();
|
Task StartAsync(CancellationToken cancellationToken);
|
||||||
|
|
||||||
void Cancel();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,53 +6,53 @@ namespace Kyoo.Models
|
|||||||
{
|
{
|
||||||
public class Collection : IMergable<Collection>
|
public class Collection : IMergable<Collection>
|
||||||
{
|
{
|
||||||
[JsonIgnore] public long ID { get; set; }
|
[JsonIgnore] public long ID { get; set; }
|
||||||
public string Slug { get; set; }
|
public string Slug { get; set; }
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
public string Poster { get; set; }
|
public string Poster { get; set; }
|
||||||
public string Overview { get; set; }
|
public string Overview { get; set; }
|
||||||
[JsonIgnore] public string ImgPrimary { get; set; }
|
[JsonIgnore] public string ImgPrimary { get; set; }
|
||||||
public IEnumerable<Show> Shows;
|
public IEnumerable<Show> Shows;
|
||||||
|
|
||||||
public Collection() { }
|
public Collection() { }
|
||||||
|
|
||||||
public Collection(string slug, string name, string overview, string imgPrimary)
|
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)
|
|
||||||
{
|
{
|
||||||
IsCollection = true
|
Slug = slug;
|
||||||
};
|
Name = name;
|
||||||
}
|
Overview = overview;
|
||||||
|
ImgPrimary = imgPrimary;
|
||||||
|
}
|
||||||
|
|
||||||
public Collection Merge(Collection collection)
|
public Show AsShow()
|
||||||
{
|
{
|
||||||
if (collection == null)
|
return new Show(Slug, Name, null, null, Overview, null, null, null, null, null, null)
|
||||||
return this;
|
{
|
||||||
if (ID == -1)
|
IsCollection = true
|
||||||
ID = collection.ID;
|
};
|
||||||
if (Slug == null)
|
}
|
||||||
Slug = collection.Slug;
|
|
||||||
if (Name == null)
|
public Collection Merge(Collection collection)
|
||||||
Name = collection.Name;
|
{
|
||||||
if (Poster == null)
|
if (collection == null)
|
||||||
Poster = collection.Poster;
|
return this;
|
||||||
if (Overview == null)
|
if (ID == -1)
|
||||||
Overview = collection.Overview;
|
ID = collection.ID;
|
||||||
if (ImgPrimary == null)
|
if (Slug == null)
|
||||||
ImgPrimary = collection.ImgPrimary;
|
Slug = collection.Slug;
|
||||||
if (Shows == null)
|
if (Name == null)
|
||||||
Shows = collection.Shows;
|
Name = collection.Name;
|
||||||
else
|
if (Poster == null)
|
||||||
Shows = Shows.Concat(collection.Shows);
|
Poster = collection.Poster;
|
||||||
return this;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
Subproject commit 1a7094979c79dd3e50760afbc6bf5b2286e7ab9d
|
Subproject commit 1831074c9d21ccc238409b6c21520780ca666bea
|
@ -13,9 +13,6 @@ namespace Kyoo.Controllers
|
|||||||
{
|
{
|
||||||
public class Crawler : ICrawler
|
public class Crawler : ICrawler
|
||||||
{
|
{
|
||||||
private bool _isRunning;
|
|
||||||
private readonly CancellationTokenSource _cancellation;
|
|
||||||
|
|
||||||
private readonly ILibraryManager _libraryManager;
|
private readonly ILibraryManager _libraryManager;
|
||||||
private readonly IProviderManager _metadataProvider;
|
private readonly IProviderManager _metadataProvider;
|
||||||
private readonly ITranscoder _transcoder;
|
private readonly ITranscoder _transcoder;
|
||||||
@ -27,26 +24,9 @@ namespace Kyoo.Controllers
|
|||||||
_metadataProvider = metadataProvider;
|
_metadataProvider = metadataProvider;
|
||||||
_transcoder = transcoder;
|
_transcoder = transcoder;
|
||||||
_config = configuration;
|
_config = configuration;
|
||||||
_cancellation = new CancellationTokenSource();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start()
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
|
||||||
if (_isRunning)
|
|
||||||
return;
|
|
||||||
_isRunning = true;
|
|
||||||
StartAsync(_cancellation.Token);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Cancel()
|
|
||||||
{
|
|
||||||
if (!_isRunning)
|
|
||||||
return;
|
|
||||||
_isRunning = false;
|
|
||||||
_cancellation.Cancel();
|
|
||||||
}
|
|
||||||
|
|
||||||
private async void StartAsync(CancellationToken cancellationToken)
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -66,7 +46,6 @@ namespace Kyoo.Controllers
|
|||||||
{
|
{
|
||||||
Console.Error.WriteLine($"Unknown exception thrown durring libraries scan.\nException: {ex.Message}");
|
Console.Error.WriteLine($"Unknown exception thrown durring libraries scan.\nException: {ex.Message}");
|
||||||
}
|
}
|
||||||
_isRunning = false;
|
|
||||||
Console.WriteLine("Scan finished!");
|
Console.WriteLine("Scan finished!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -204,12 +183,5 @@ namespace Kyoo.Controllers
|
|||||||
{
|
{
|
||||||
return VideoExtensions.Contains(Path.GetExtension(filePath));
|
return VideoExtensions.Contains(Path.GetExtension(filePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Task StopAsync()
|
|
||||||
{
|
|
||||||
_cancellation.Cancel();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
34
Kyoo/Controllers/StartupCode.cs
Normal file
34
Kyoo/Controllers/StartupCode.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Kyoo.Controllers
|
namespace Kyoo.Controllers
|
||||||
@ -21,7 +22,7 @@ namespace Kyoo.Controllers
|
|||||||
[HttpGet("scan")]
|
[HttpGet("scan")]
|
||||||
public IActionResult ScanLibrary()
|
public IActionResult ScanLibrary()
|
||||||
{
|
{
|
||||||
crawler.Start();
|
crawler.StartAsync(new CancellationToken());
|
||||||
return Ok("Scanning");
|
return Ok("Scanning");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,22 +11,8 @@ namespace Kyoo
|
|||||||
{
|
{
|
||||||
public static async Task Main(string[] args)
|
public static async Task Main(string[] args)
|
||||||
{
|
{
|
||||||
IWebHost host = CreateWebHostBuilder(args).Build();
|
|
||||||
|
|
||||||
Console.WriteLine($"Running as: {Environment.UserName}");
|
Console.WriteLine($"Running as: {Environment.UserName}");
|
||||||
using (IServiceScope serviceScope = host.Services.CreateScope())
|
await CreateWebHostBuilder(args).Build().RunAsync();
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
|
||||||
|
@ -45,6 +45,8 @@ namespace Kyoo
|
|||||||
services.AddSingleton<IProviderManager, ProviderManager>();
|
services.AddSingleton<IProviderManager, ProviderManager>();
|
||||||
services.AddScoped<ICrawler, Crawler>();
|
services.AddScoped<ICrawler, Crawler>();
|
||||||
services.AddSingleton<IPluginManager, PluginManager>();
|
services.AddSingleton<IPluginManager, PluginManager>();
|
||||||
|
|
||||||
|
services.AddHostedService<StartupCode>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user