diff --git a/src/Umbraco.Examine.Lucene/ConfigurationEnabledDirectoryFactory.cs b/src/Umbraco.Examine.Lucene/ConfigurationEnabledDirectoryFactory.cs index f58dffacac0d..e627802222f0 100644 --- a/src/Umbraco.Examine.Lucene/ConfigurationEnabledDirectoryFactory.cs +++ b/src/Umbraco.Examine.Lucene/ConfigurationEnabledDirectoryFactory.cs @@ -54,7 +54,7 @@ private IDirectoryFactory CreateFactory() case LuceneDirectoryFactory.SyncedTempFileSystemDirectoryFactory: return _services.GetRequiredService(); case LuceneDirectoryFactory.TempFileSystemDirectoryFactory: - return _services.GetRequiredService(); + return _services.GetRequiredService(); case LuceneDirectoryFactory.Default: default: return _services.GetRequiredService(); diff --git a/src/Umbraco.Examine.Lucene/DependencyInjection/UmbracoBuilderExtensions.cs b/src/Umbraco.Examine.Lucene/DependencyInjection/UmbracoBuilderExtensions.cs index baaf10907c73..211ebd8b71b4 100644 --- a/src/Umbraco.Examine.Lucene/DependencyInjection/UmbracoBuilderExtensions.cs +++ b/src/Umbraco.Examine.Lucene/DependencyInjection/UmbracoBuilderExtensions.cs @@ -1,8 +1,10 @@ using Examine; using Examine.Lucene.Directories; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Umbraco.Cms.Core; using Umbraco.Cms.Core.DependencyInjection; +using Umbraco.Cms.Core.Hosting; namespace Umbraco.Cms.Infrastructure.Examine.DependencyInjection; @@ -22,8 +24,26 @@ public static IUmbracoBuilder AddExamineIndexes(this IUmbracoBuilder umbracoBuil services.AddExamine(); - // Create the indexes - services + services.AddSingleton(); + services.RemoveAll(); + services.AddSingleton( + s => + { + var baseDir = s.GetRequiredService().ApplicationRoot; + + var tempDir = UmbracoTempEnvFileSystemDirectoryFactory.GetTempPath( + s.GetRequiredService(), s.GetRequiredService()); + + return ActivatorUtilities.CreateInstance( + s, + new object[] + { + new DirectoryInfo(tempDir), s.GetRequiredService().ApplicationRoot + }); + }); + + // Create the indexes + services .AddExamineLuceneIndex(Constants.UmbracoIndexes .InternalIndexName) .AddExamineLuceneIndex(Constants.UmbracoIndexes diff --git a/src/Umbraco.Examine.Lucene/UmbracoTempEnvFileSystemDirectoryFactory.cs b/src/Umbraco.Examine.Lucene/UmbracoTempEnvFileSystemDirectoryFactory.cs new file mode 100644 index 000000000000..367181d1437e --- /dev/null +++ b/src/Umbraco.Examine.Lucene/UmbracoTempEnvFileSystemDirectoryFactory.cs @@ -0,0 +1,36 @@ +using Examine; +using Examine.Lucene.Directories; +using IHostingEnvironment = Umbraco.Cms.Core.Hosting.IHostingEnvironment; + +namespace Umbraco.Cms.Infrastructure.Examine +{ + /// + /// Custom version of https://github.com/Shazwazza/Examine/blob/release/3.0/src/Examine.Lucene/Directories/TempEnvFileSystemDirectoryFactory.cs that includes the Umbraco SiteName property in the path hash + /// + public class UmbracoTempEnvFileSystemDirectoryFactory : FileSystemDirectoryFactory + { + public UmbracoTempEnvFileSystemDirectoryFactory( + IApplicationIdentifier applicationIdentifier, + ILockFactory lockFactory, + IHostingEnvironment hostingEnvironment) + : base(new DirectoryInfo(GetTempPath(applicationIdentifier, hostingEnvironment)), lockFactory) + { + } + + public static string GetTempPath(IApplicationIdentifier applicationIdentifier, IHostingEnvironment hostingEnvironment) + { + var hashString = hostingEnvironment.SiteName + "::" + applicationIdentifier.GetApplicationUniqueIdentifier(); + var appDomainHash = hashString.GenerateHash(); + + var cachePath = Path.Combine( + Path.GetTempPath(), + "ExamineIndexes", + //include the appdomain hash is just a safety check, for example if a website is moved from worker A to worker B and then back + // to worker A again, in theory the %temp% folder should already be empty but we really want to make sure that its not + // utilizing an old index + appDomainHash); + + return cachePath; + } + } +}