Migrate meilisearch on startup

This commit is contained in:
Zoe Roux 2024-06-16 15:17:37 +00:00
parent f22ba144c7
commit 233a498f6b
No known key found for this signature in database
4 changed files with 39 additions and 11 deletions

View File

@ -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" 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 # Token authentication method as seen in https://openid.net/specs/openid-connect-core-1_0.html#ClientAuthentication
# Supported values: ClientSecretBasic (default) or ClientSecretPost # Supported values: ClientSecretBasic (default) or ClientSecretPost
# If in doupt, leave this empty. # If in doubt, leave this empty.
OIDC_SERVICE_AUTHMETHOD=ClientSecretBasic OIDC_SERVICE_AUTHMETHOD=ClientSecretBasic
# on the previous list, service is the internal name of your service, you can add as many as you want. # 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_SERVER=postgres
POSTGRES_PORT=5432 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_HOST="http://meilisearch:7700"
MEILI_MASTER_KEY="ghvjkgisbgkbgskegblfqbgjkebbhgwkjfb" MEILI_MASTER_KEY="ghvjkgisbgkbgskegblfqbgjkebbhgwkjfb"

View File

@ -17,6 +17,7 @@
// along with Kyoo. If not, see <https://www.gnu.org/licenses/>. // along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
using System; using System;
using Kyoo.Abstractions.Controllers;
using Kyoo.Authentication; using Kyoo.Authentication;
using Kyoo.Core; using Kyoo.Core;
using Kyoo.Core.Controllers; using Kyoo.Core.Controllers;
@ -27,6 +28,7 @@ using Kyoo.RabbitMq;
using Kyoo.Swagger; using Kyoo.Swagger;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Serilog; using Serilog;
using Serilog.Events; using Serilog.Events;
@ -94,12 +96,17 @@ app.MapControllers();
app.Services.GetRequiredService<MeiliSync>(); app.Services.GetRequiredService<MeiliSync>();
app.Services.GetRequiredService<RabbitProducer>(); app.Services.GetRequiredService<RabbitProducer>();
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"); await app.RunAsync(Environment.GetEnvironmentVariable("KYOO_BIND_URL") ?? "http://*:5000");

View File

@ -95,4 +95,18 @@ public class MeiliSync
_ => value _ => 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);
}
} }

View File

@ -119,11 +119,6 @@ public static class MeilisearchModule
}, },
}; };
/// <summary>
/// Init meilisearch indexes.
/// </summary>
/// <param name="provider">The service list to retrieve the meilisearch client</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
public static async Task Initialize(IServiceProvider provider) public static async Task Initialize(IServiceProvider provider)
{ {
MeilisearchClient client = provider.GetRequiredService<MeilisearchClient>(); MeilisearchClient client = provider.GetRequiredService<MeilisearchClient>();
@ -133,6 +128,13 @@ public static class MeilisearchModule
await _CreateIndex(client, nameof(Studio), false); await _CreateIndex(client, nameof(Studio), false);
} }
public static async Task SyncDatabase(IServiceProvider provider)
{
await using AsyncServiceScope scope = provider.CreateAsyncScope();
ILibraryManager database = scope.ServiceProvider.GetRequiredService<ILibraryManager>();
await scope.ServiceProvider.GetRequiredService<MeiliSync>().SyncEverything(database);
}
private static async Task _CreateIndex(MeilisearchClient client, string index, bool hasKind) private static async Task _CreateIndex(MeilisearchClient client, string index, bool hasKind)
{ {
TaskInfo task = await client.CreateIndexAsync( TaskInfo task = await client.CreateIndexAsync(