mirror of
				https://github.com/zoriya/Kyoo.git
				synced 2025-10-31 10:37: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 | ||||
|     { | ||||
|         void Start(); | ||||
| 
 | ||||
|         void Cancel(); | ||||
| 	    Task StartAsync(CancellationToken cancellationToken); | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -6,53 +6,53 @@ namespace Kyoo.Models | ||||
| { | ||||
|     public class Collection : IMergable<Collection> | ||||
|     { | ||||
|     [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<Show> 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<Show> 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; | ||||
| 		} | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -1 +1 @@ | ||||
| Subproject commit 1a7094979c79dd3e50760afbc6bf5b2286e7ab9d | ||||
| Subproject commit 1831074c9d21ccc238409b6c21520780ca666bea | ||||
| @ -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; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
							
								
								
									
										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.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"); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @ -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) => | ||||
|  | ||||
| @ -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. | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user