2020-12-05 09:54:59 +01:00
|
|
|
// Copyright (c) Umbraco.
|
|
|
|
|
// See LICENSE for more details.
|
|
|
|
|
|
|
|
|
|
using System;
|
2020-10-30 13:56:13 +01:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
|
|
|
2021-02-12 11:03:28 +01:00
|
|
|
namespace Umbraco.Cms.Infrastructure.HostedServices
|
2020-10-30 13:56:13 +01:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provides a base class for recurring background tasks implemented as hosted services.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// See: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services?view=aspnetcore-3.1&tabs=visual-studio#timed-background-tasks
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public abstract class RecurringHostedServiceBase : IHostedService, IDisposable
|
|
|
|
|
{
|
2020-12-05 09:54:59 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// The default delay to use for recurring tasks for the first run after application start-up if no alternative is configured.
|
|
|
|
|
/// </summary>
|
2020-10-30 13:56:13 +01:00
|
|
|
protected static readonly TimeSpan DefaultDelay = TimeSpan.FromMinutes(3);
|
|
|
|
|
|
|
|
|
|
private readonly TimeSpan _period;
|
|
|
|
|
private readonly TimeSpan _delay;
|
|
|
|
|
private Timer _timer;
|
|
|
|
|
|
2020-12-05 09:54:59 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="RecurringHostedServiceBase"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="period">Timepsan representing how often the task should recur.</param>
|
|
|
|
|
/// <param name="delay">Timespan represeting the initial delay after application start-up before the first run of the task occurs.</param>
|
2020-10-30 13:56:13 +01:00
|
|
|
protected RecurringHostedServiceBase(TimeSpan period, TimeSpan delay)
|
|
|
|
|
{
|
|
|
|
|
_period = period;
|
|
|
|
|
_delay = delay;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 09:54:59 +01:00
|
|
|
/// <inheritdoc/>
|
2020-10-30 13:56:13 +01:00
|
|
|
public Task StartAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_timer = new Timer(ExecuteAsync, null, (int)_delay.TotalMilliseconds, (int)_period.TotalMilliseconds);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 09:54:59 +01:00
|
|
|
/// <summary>
|
|
|
|
|
/// Executes the task.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The task state.</param>
|
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 async void ExecuteAsync(object state)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// First, stop the timer, we do not want tasks to execute in parallel
|
|
|
|
|
_timer?.Change(Timeout.Infinite, 0);
|
|
|
|
|
|
|
|
|
|
// Delegate work to method returning a task, that can be called and asserted in a unit test.
|
|
|
|
|
// Without this there can be behaviour where tests pass, but an error within them causes the test
|
|
|
|
|
// running process to crash.
|
|
|
|
|
// Hat-tip: https://stackoverflow.com/a/14207615/489433
|
|
|
|
|
await PerformExecuteAsync(state);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2021-06-15 11:02:55 +02:00
|
|
|
// Resume now that the task is complete - Note we use period in both because we don't want to execute again after the delay.
|
|
|
|
|
// So first execution is after _delay, and the we wait _period between each
|
|
|
|
|
_timer?.Change((int)_period.TotalMilliseconds, (int)_period.TotalMilliseconds);
|
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
|
|
|
}
|
|
|
|
|
}
|
2020-11-02 11:59:39 +01:00
|
|
|
|
2021-03-15 07:49:33 +01:00
|
|
|
public abstract Task PerformExecuteAsync(object state);
|
2020-10-30 13:56:13 +01:00
|
|
|
|
2020-12-05 09:54:59 +01:00
|
|
|
/// <inheritdoc/>
|
2020-10-30 13:56:13 +01:00
|
|
|
public Task StopAsync(CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
_timer?.Change(Timeout.Infinite, 0);
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-05 09:54:59 +01:00
|
|
|
/// <inheritdoc/>
|
|
|
|
|
public void Dispose() => _timer?.Dispose();
|
2020-10-30 13:56:13 +01:00
|
|
|
}
|
|
|
|
|
}
|