2021-03-03 13:43:27 +11:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
|
|
|
namespace Umbraco.Cms.Tests.Common
|
2021-03-03 13:43:27 +11:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Helper class to not repeat common patterns with Task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public sealed class TaskHelper
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger<TaskHelper> _logger;
|
|
|
|
|
|
|
|
|
|
public TaskHelper(ILogger<TaskHelper> logger) => _logger = logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes a fire and forget task outside of the current execution flow.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void RunBackgroundTask(Func<Task> fn) => ExecuteBackgroundTask(fn);
|
|
|
|
|
|
|
|
|
|
// for tests, returning the Task as a public API indicates it can be awaited that is not what we want to do
|
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
|
|
|
public Task ExecuteBackgroundTask(Func<Task> fn)
|
2021-03-03 13:43:27 +11:00
|
|
|
{
|
|
|
|
|
// it is also possible to use UnsafeQueueUserWorkItem which does not flow the execution context,
|
|
|
|
|
// however that seems more difficult to use for async operations.
|
|
|
|
|
|
|
|
|
|
// Do not flow AsyncLocal to the child thread
|
|
|
|
|
using (ExecutionContext.SuppressFlow())
|
|
|
|
|
{
|
|
|
|
|
// NOTE: ConfigureAwait(false) is irrelevant here, it is not needed because this is not being
|
|
|
|
|
// awaited. ConfigureAwait(false) is only relevant when awaiting to prevent the SynchronizationContext
|
|
|
|
|
// (very different from the ExecutionContext!) from running the continuation on the calling thread.
|
|
|
|
|
return Task.Run(LoggingWrapper(fn));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Executes a fire and forget task outside of the current execution flow on a dedicated (non thread-pool) thread.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void RunLongRunningBackgroundTask(Func<Task> fn) => ExecuteLongRunningBackgroundTask(fn);
|
|
|
|
|
|
|
|
|
|
// for tests, returning the Task as a public API indicates it can be awaited that is not what we want to do
|
Examine 2.0 integration (#10241)
* Init commit for examine 2.0 work, most old umb examine tests working, probably a lot that doesn't
* Gets Umbraco Examine tests passing and makes some sense out of them, fixes some underlying issues.
* Large refactor, remove TaskHelper, rename Notifications to be consistent, Gets all examine/lucene indexes building and startup ordered in the correct way, removes old files, creates new IUmbracoIndexingHandler for abstracting out all index operations for umbraco data, abstracts out IIndexRebuilder, Fixes Stack overflow with LiveModelsProvider and loading assemblies, ports some changes from v8 for startup handling with cold boots, refactors out LastSyncedFileManager
* fix up issues with rebuilding and management dashboard.
* removes old files, removes NetworkHelper, fixes LastSyncedFileManager implementation to ensure the machine name is used, fix up logging with cold boot state.
* Makes MainDom safer to use and makes PublishedSnapshotService lazily register with MainDom
* lazily acquire application id (fix unit tests)
* Fixes resource casing and missing test file
* Ensures caches when requiring internal services for PublishedSnapshotService, UseNuCache is a separate call, shouldn't be buried in AddWebComponents, was also causing issues in integration tests since nucache was being used for the Id2Key service.
* For UmbracoTestServerTestBase enable nucache services
* Fixing tests
* Fix another test
* Fixes tests, use TestHostingEnvironment, make Tests.Common use net5, remove old Lucene.Net.Contrib ref.
* Fixes up some review notes
* Fixes issue with doubly registering PublishedSnapshotService meanig there could be 2x instances of it
* Checks for parseexception when executing the query
* Use application root instead of duplicating functionality.
* Added Examine project to netcore only solution file
* Fixed casing issue with LazyLoad, that is not lowercase.
* uses cancellationToken instead of bool flag, fixes always reading lastId from the LastSyncedFileManager, fixes RecurringHostedServiceBase so that there isn't an overlapping thread for the same task type
* Fix tests
* remove legacy test project from solution file
* Fix test
Co-authored-by: Bjarke Berg <mail@bergmania.dk>
2021-05-18 18:31:38 +10:00
|
|
|
public Task ExecuteLongRunningBackgroundTask(Func<Task> fn)
|
2021-03-03 13:43:27 +11:00
|
|
|
{
|
|
|
|
|
// it is also possible to use UnsafeQueueUserWorkItem which does not flow the execution context,
|
|
|
|
|
// however that seems more difficult to use for async operations.
|
|
|
|
|
|
|
|
|
|
// Do not flow AsyncLocal to the child thread
|
|
|
|
|
using (ExecutionContext.SuppressFlow())
|
|
|
|
|
{
|
|
|
|
|
// NOTE: ConfigureAwait(false) is irrelevant here, it is not needed because this is not being
|
|
|
|
|
// awaited. ConfigureAwait(false) is only relevant when awaiting to prevent the SynchronizationContext
|
|
|
|
|
// (very different from the ExecutionContext!) from running the continuation on the calling thread.
|
|
|
|
|
return Task.Factory.StartNew(LoggingWrapper(fn), TaskCreationOptions.LongRunning);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ensure any exceptions are handled and do not take down the app pool
|
|
|
|
|
private Func<Task> LoggingWrapper(Func<Task> fn) =>
|
|
|
|
|
async () =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await fn();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Exception thrown in a background thread");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|