Skip to content

Commit

Permalink
Algolia Installer work around to solve problem where Application is n…
Browse files Browse the repository at this point in the history
…ot Initialized during Module.OnInit()
  • Loading branch information
bkapustik committed Feb 12, 2024
1 parent 8d4fdb2 commit c15a155
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
12 changes: 5 additions & 7 deletions src/Kentico.Xperience.Algolia/Admin/AlgoliaAdminModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@ namespace Kentico.Xperience.Algolia.Admin;
internal class AlgoliaAdminModule : AdminModule
{
private IAlgoliaConfigurationStorageService storageService = null!;
private AlgoliaModuleInstaller installer = null!;
private IServiceProvider serviceProvider = null!;

public AlgoliaAdminModule() : base(nameof(AlgoliaAdminModule)) { }

protected override void OnInit(ModuleInitParameters parameters)
{
base.OnInit(parameters);

RegisterClientModule("kentico", "xperience-integrations-algolia");

var services = parameters.Services;

installer = services.GetRequiredService<AlgoliaModuleInstaller>();
storageService = services.GetRequiredService<IAlgoliaConfigurationStorageService>();
serviceProvider = parameters.Services;
storageService = serviceProvider.GetRequiredService<IAlgoliaConfigurationStorageService>();

ApplicationEvents.Initialized.Execute += InitializeModule;
ApplicationEvents.PostStart.Execute += InitializeModule;
}

private void InitializeModule(object? sender, EventArgs e)
{
var installer = serviceProvider.GetRequiredService<AlgoliaModuleInstaller>();
installer.Install();

AlgoliaIndexStore.SetIndicies(storageService);
Expand Down
4 changes: 0 additions & 4 deletions src/Kentico.Xperience.Algolia/AlgoliaSearchModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,11 @@ public AlgoliaSearchModule() : base(nameof(AlgoliaSearchModule))
protected override void OnInit(ModuleInitParameters parameters)
{
base.OnInit();
var services = parameters.Services;

services.GetRequiredService<AlgoliaModuleInstaller>().Install();
algoliaTaskLogger = Service.Resolve<IAlgoliaTaskLogger>();
appSettingsService = Service.Resolve<IAppSettingsService>();
conversionService = Service.Resolve<IConversionService>();


AddRegisteredIndices();
WebPageEvents.Publish.Execute += HandleEvent;
WebPageEvents.Delete.Execute += HandleEvent;
ContentItemEvents.Publish.Execute += HandleContentItemEvent;
Expand Down
22 changes: 20 additions & 2 deletions src/Kentico.Xperience.Algolia/AlgoliaStartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class AlgoliaStartupExtensions
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configuration">The application configuration.</param>
public static IServiceCollection AddKenticoAlgolia(this IServiceCollection services, IConfiguration configuration) =>
private static IServiceCollection AddKenticoAlgoliaInternal(this IServiceCollection services, IConfiguration configuration) =>
services.AddSingleton<AlgoliaModuleInstaller>()
.Configure<AlgoliaOptions>(configuration.GetSection(AlgoliaOptions.CMS_ALGOLIA_SECTION_NAME))
.AddSingleton<IInsightsClient>(s =>
Expand Down Expand Up @@ -53,7 +53,7 @@ public static IServiceCollection AddKenticoAlgolia(this IServiceCollection servi
/// <returns></returns>
public static IServiceCollection AddKenticoAlgolia(this IServiceCollection serviceCollection, Action<IAlgoliaBuilder> configure, IConfiguration configuration)
{
serviceCollection.AddKenticoAlgolia(configuration);
serviceCollection.AddKenticoAlgoliaInternal(configuration);

var builder = new AlgoliaBuilder(serviceCollection);

Expand All @@ -67,6 +67,24 @@ public static IServiceCollection AddKenticoAlgolia(this IServiceCollection servi

return serviceCollection;
}

/// <summary>
/// Adds Algolia services and custom module to application with <see cref="DefaultAlgoliaIndexingStrategy"/>
/// </summary>
/// <param name="serviceCollection"></param>
/// <param name="configuration">The application configuration.</param>
/// <returns></returns>
public static IServiceCollection AddKenticoAlgolia(this IServiceCollection serviceCollection, IConfiguration configuration)
{
serviceCollection.AddKenticoAlgoliaInternal(configuration);

var builder = new AlgoliaBuilder(serviceCollection);

serviceCollection.AddTransient<DefaultAlgoliaIndexingStrategy>();
builder.RegisterStrategy<DefaultAlgoliaIndexingStrategy>("Default");

return serviceCollection;
}
}


Expand Down

0 comments on commit c15a155

Please sign in to comment.