From 233a498f6b5a85c0d6627826fcca15a68bd01168 Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Sun, 16 Jun 2024 15:17:37 +0000 Subject: [PATCH] Migrate meilisearch on startup --- .env.example | 7 ++++++- back/src/Kyoo.Core/Program.cs | 17 ++++++++++++----- back/src/Kyoo.Meilisearch/MeiliSync.cs | 14 ++++++++++++++ back/src/Kyoo.Meilisearch/MeilisearchModule.cs | 12 +++++++----- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/.env.example b/.env.example index 2298b20a..f15219f1 100644 --- a/.env.example +++ b/.env.example @@ -64,7 +64,7 @@ OIDC_SERVICE_PROFILE=https://url-of-the-profile-endpoint-of-the-oidc-service.com OIDC_SERVICE_SCOPE="the list of scopes space separeted like email identity" # Token authentication method as seen in https://openid.net/specs/openid-connect-core-1_0.html#ClientAuthentication # Supported values: ClientSecretBasic (default) or ClientSecretPost -# If in doupt, leave this empty. +# If in doubt, leave this empty. OIDC_SERVICE_AUTHMETHOD=ClientSecretBasic # on the previous list, service is the internal name of your service, you can add as many as you want. @@ -81,6 +81,11 @@ POSTGRES_DB=kyooDB POSTGRES_SERVER=postgres POSTGRES_PORT=5432 +# Read by the api container to know if it should run meilisearch's migrations/sync +# and download missing images. This is a good idea to only have one instance with this on +# Note: it does not run postgres migrations, use the migration container for that. +RUN_MIGRATIONS=true + MEILI_HOST="http://meilisearch:7700" MEILI_MASTER_KEY="ghvjkgisbgkbgskegblfqbgjkebbhgwkjfb" diff --git a/back/src/Kyoo.Core/Program.cs b/back/src/Kyoo.Core/Program.cs index c95c9cc3..49afbd5c 100644 --- a/back/src/Kyoo.Core/Program.cs +++ b/back/src/Kyoo.Core/Program.cs @@ -17,6 +17,7 @@ // along with Kyoo. If not, see . using System; +using Kyoo.Abstractions.Controllers; using Kyoo.Authentication; using Kyoo.Core; using Kyoo.Core.Controllers; @@ -27,6 +28,7 @@ using Kyoo.RabbitMq; using Kyoo.Swagger; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Serilog; using Serilog.Events; @@ -94,12 +96,17 @@ app.MapControllers(); app.Services.GetRequiredService(); app.Services.GetRequiredService(); -await using (AsyncServiceScope scope = app.Services.CreateAsyncScope()) +// Only run sync on the main instance +if (app.Configuration.GetValue("RUN_MIGRATIONS", true)) { - await MeilisearchModule.Initialize(scope.ServiceProvider); + await using (AsyncServiceScope scope = app.Services.CreateAsyncScope()) + { + await MeilisearchModule.Initialize(app.Services); + } + + // The methods takes care of creating a scope and will download images on the background. + _ = MeilisearchModule.SyncDatabase(app.Services); + _ = MiscRepository.DownloadMissingImages(app.Services); } -// The methods takes care of creating a scope and will download images on the background. -_ = MiscRepository.DownloadMissingImages(app.Services); - await app.RunAsync(Environment.GetEnvironmentVariable("KYOO_BIND_URL") ?? "http://*:5000"); diff --git a/back/src/Kyoo.Meilisearch/MeiliSync.cs b/back/src/Kyoo.Meilisearch/MeiliSync.cs index 500fd974..e995ce6a 100644 --- a/back/src/Kyoo.Meilisearch/MeiliSync.cs +++ b/back/src/Kyoo.Meilisearch/MeiliSync.cs @@ -95,4 +95,18 @@ public class MeiliSync _ => value }; } + + public async Task SyncEverything(ILibraryManager database) + { + foreach (Movie movie in await database.Movies.GetAll(limit: 0)) + await CreateOrUpdate("items", movie, nameof(Movie)); + foreach (Show show in await database.Shows.GetAll(limit: 0)) + await CreateOrUpdate("items", show, nameof(Show)); + foreach (Collection collection in await database.Collections.GetAll(limit: 0)) + await CreateOrUpdate("items", collection, nameof(Collection)); + foreach (Episode episode in await database.Episodes.GetAll(limit: 0)) + await CreateOrUpdate(nameof(Episode), episode); + foreach (Studio studio in await database.Studios.GetAll(limit: 0)) + await CreateOrUpdate(nameof(Studio), studio); + } } diff --git a/back/src/Kyoo.Meilisearch/MeilisearchModule.cs b/back/src/Kyoo.Meilisearch/MeilisearchModule.cs index a290a366..d3e6f47b 100644 --- a/back/src/Kyoo.Meilisearch/MeilisearchModule.cs +++ b/back/src/Kyoo.Meilisearch/MeilisearchModule.cs @@ -119,11 +119,6 @@ public static class MeilisearchModule }, }; - /// - /// Init meilisearch indexes. - /// - /// The service list to retrieve the meilisearch client - /// A representing the asynchronous operation. public static async Task Initialize(IServiceProvider provider) { MeilisearchClient client = provider.GetRequiredService(); @@ -133,6 +128,13 @@ public static class MeilisearchModule await _CreateIndex(client, nameof(Studio), false); } + public static async Task SyncDatabase(IServiceProvider provider) + { + await using AsyncServiceScope scope = provider.CreateAsyncScope(); + ILibraryManager database = scope.ServiceProvider.GetRequiredService(); + await scope.ServiceProvider.GetRequiredService().SyncEverything(database); + } + private static async Task _CreateIndex(MeilisearchClient client, string index, bool hasKind) { TaskInfo task = await client.CreateIndexAsync(