Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Lucene.Core): add reindex on archive item #65

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text eol=crlf
*.png binary
252 changes: 127 additions & 125 deletions src/Kentico.Xperience.Lucene.Core/LuceneSearchModule.cs
Original file line number Diff line number Diff line change
@@ -1,125 +1,127 @@
using CMS;
using CMS.Base;
using CMS.ContentEngine;
using CMS.Core;
using CMS.DataEngine;
using CMS.Websites;

using Kentico.Xperience.Lucene.Core;
using Kentico.Xperience.Lucene.Core.Indexing;

using Microsoft.Extensions.DependencyInjection;

[assembly: RegisterModule(typeof(LuceneSearchModule))]

namespace Kentico.Xperience.Lucene.Core;

/// <summary>
/// Initializes page event handlers, and ensures the thread queue workers for processing Lucene tasks.
/// </summary>
internal class LuceneSearchModule : Module
{
private ILuceneTaskLogger luceneTaskLogger = null!;
private IAppSettingsService appSettingsService = null!;
private IConversionService conversionService = null!;
private LuceneModuleInstaller installer = null!;

private const string APP_SETTINGS_KEY_INDEXING_DISABLED = "CMSLuceneSearchDisableIndexing";

private bool IndexingDisabled
{
get
{
if (appSettingsService[APP_SETTINGS_KEY_INDEXING_DISABLED] is string value1)
{
return conversionService.GetBoolean(value1, false);
}

return false;
}
}


/// <inheritdoc/>
public LuceneSearchModule() : base(nameof(LuceneSearchModule))
{
}


/// <inheritdoc/>
protected override void OnInit(ModuleInitParameters parameters)
{
base.OnInit(parameters);

var services = parameters.Services;

installer = services.GetRequiredService<LuceneModuleInstaller>();

ApplicationEvents.Initialized.Execute += InitializeModule;

luceneTaskLogger = services.GetRequiredService<ILuceneTaskLogger>();
appSettingsService = services.GetRequiredService<IAppSettingsService>();
conversionService = services.GetRequiredService<IConversionService>();

WebPageEvents.Publish.Execute += HandleEvent;
WebPageEvents.Delete.Execute += HandleEvent;
ContentItemEvents.Publish.Execute += HandleContentItemEvent;
ContentItemEvents.Delete.Execute += HandleContentItemEvent;

RequestEvents.RunEndRequestTasks.Execute += (sender, eventArgs) => LuceneQueueWorker.Current.EnsureRunningThread();
}


/// <summary>
/// Called when a page is published. Logs an Lucene task to be processed later.
/// </summary>
private void HandleEvent(object? sender, CMSEventArgs e)
{
if (IndexingDisabled || e is not WebPageEventArgsBase publishedEvent)
{
return;
}

var indexedItemModel = new IndexEventWebPageItemModel(
publishedEvent.ID,
publishedEvent.Guid,
publishedEvent.ContentLanguageName,
publishedEvent.ContentTypeName,
publishedEvent.Name,
publishedEvent.IsSecured,
publishedEvent.ContentTypeID,
publishedEvent.ContentLanguageID,
publishedEvent.WebsiteChannelName,
publishedEvent.TreePath,
publishedEvent.ParentID,
publishedEvent.Order)
{ };

luceneTaskLogger?.HandleEvent(indexedItemModel, e.CurrentHandler.Name).GetAwaiter().GetResult();
}


private void HandleContentItemEvent(object? sender, CMSEventArgs e)
{
if (IndexingDisabled || e is not ContentItemEventArgsBase publishedEvent)
{
return;
}

var indexedContentItemModel = new IndexEventReusableItemModel(
publishedEvent.ID,
publishedEvent.Guid,
publishedEvent.ContentLanguageName,
publishedEvent.ContentTypeName,
publishedEvent.Name,
publishedEvent.IsSecured,
publishedEvent.ContentTypeID,
publishedEvent.ContentLanguageID
);

luceneTaskLogger?.HandleReusableItemEvent(indexedContentItemModel, e.CurrentHandler.Name).GetAwaiter().GetResult();
}

private void InitializeModule(object? sender, EventArgs e) =>
installer.Install();
}
using CMS;
using CMS.Base;
using CMS.ContentEngine;
using CMS.Core;
using CMS.DataEngine;
using CMS.Websites;

using Kentico.Xperience.Lucene.Core;
using Kentico.Xperience.Lucene.Core.Indexing;

using Microsoft.Extensions.DependencyInjection;

[assembly: RegisterModule(typeof(LuceneSearchModule))]

namespace Kentico.Xperience.Lucene.Core;

/// <summary>
/// Initializes page event handlers, and ensures the thread queue workers for processing Lucene tasks.
/// </summary>
internal class LuceneSearchModule : Module
{
private ILuceneTaskLogger luceneTaskLogger = null!;
private IAppSettingsService appSettingsService = null!;
private IConversionService conversionService = null!;
private LuceneModuleInstaller installer = null!;

private const string APP_SETTINGS_KEY_INDEXING_DISABLED = "CMSLuceneSearchDisableIndexing";

private bool IndexingDisabled
{
get
{
if (appSettingsService[APP_SETTINGS_KEY_INDEXING_DISABLED] is string value1)
{
return conversionService.GetBoolean(value1, false);
}

return false;
}
}


/// <inheritdoc/>
public LuceneSearchModule() : base(nameof(LuceneSearchModule))
{
}


/// <inheritdoc/>
protected override void OnInit(ModuleInitParameters parameters)
{
base.OnInit(parameters);

var services = parameters.Services;

installer = services.GetRequiredService<LuceneModuleInstaller>();

ApplicationEvents.Initialized.Execute += InitializeModule;

luceneTaskLogger = services.GetRequiredService<ILuceneTaskLogger>();
appSettingsService = services.GetRequiredService<IAppSettingsService>();
conversionService = services.GetRequiredService<IConversionService>();

WebPageEvents.Publish.Execute += HandleEvent;
WebPageEvents.Delete.Execute += HandleEvent;
WebPageEvents.Archive.Execute += HandleEvent;
ContentItemEvents.Publish.Execute += HandleContentItemEvent;
ContentItemEvents.Delete.Execute += HandleContentItemEvent;
ContentItemEvents.Archive.Execute += HandleContentItemEvent;

RequestEvents.RunEndRequestTasks.Execute += (sender, eventArgs) => LuceneQueueWorker.Current.EnsureRunningThread();
}


/// <summary>
/// Called when a page is published. Logs an Lucene task to be processed later.
/// </summary>
private void HandleEvent(object? sender, CMSEventArgs e)
{
if (IndexingDisabled || e is not WebPageEventArgsBase publishedEvent)
{
return;
}

var indexedItemModel = new IndexEventWebPageItemModel(
publishedEvent.ID,
publishedEvent.Guid,
publishedEvent.ContentLanguageName,
publishedEvent.ContentTypeName,
publishedEvent.Name,
publishedEvent.IsSecured,
publishedEvent.ContentTypeID,
publishedEvent.ContentLanguageID,
publishedEvent.WebsiteChannelName,
publishedEvent.TreePath,
publishedEvent.ParentID,
publishedEvent.Order)
{ };

luceneTaskLogger?.HandleEvent(indexedItemModel, e.CurrentHandler.Name).GetAwaiter().GetResult();
}


private void HandleContentItemEvent(object? sender, CMSEventArgs e)
{
if (IndexingDisabled || e is not ContentItemEventArgsBase publishedEvent)
{
return;
}

var indexedContentItemModel = new IndexEventReusableItemModel(
publishedEvent.ID,
publishedEvent.Guid,
publishedEvent.ContentLanguageName,
publishedEvent.ContentTypeName,
publishedEvent.Name,
publishedEvent.IsSecured,
publishedEvent.ContentTypeID,
publishedEvent.ContentLanguageID
);

luceneTaskLogger?.HandleReusableItemEvent(indexedContentItemModel, e.CurrentHandler.Name).GetAwaiter().GetResult();
}

private void InitializeModule(object? sender, EventArgs e) =>
installer.Install();
}
Loading