Files
Umbraco-CMS/src/Umbraco.Web/Compose/DatabaseServerRegistrarAndMessengerComponent.cs

271 lines
12 KiB
C#
Raw Normal View History

using System;
using System.Threading;
using Umbraco.Core;
2018-11-28 11:05:41 +01:00
using Umbraco.Core.Composing;
using Umbraco.Core.Logging;
using Umbraco.Core.Services;
using Umbraco.Core.Services.Changes;
using Umbraco.Core.Sync;
using Umbraco.Examine;
using Umbraco.Web.Cache;
using Umbraco.Web.Routing;
using Umbraco.Web.Scheduling;
using Umbraco.Web.Search;
2018-11-28 11:05:41 +01:00
using Current = Umbraco.Web.Composing.Current;
2019-02-14 09:15:47 +01:00
namespace Umbraco.Web.Compose
{
/// <summary>
/// Ensures that servers are automatically registered in the database, when using the database server registrar.
/// </summary>
/// <remarks>
/// <para>At the moment servers are automatically registered upon first request and then on every
/// request but not more than once per (configurable) period. This really is "for information & debug" purposes so
/// we can look at the table and see what servers are registered - but the info is not used anywhere.</para>
/// <para>Should we actually want to use this, we would need a better and more deterministic way of figuring
/// out the "server address" ie the address to which server-to-server requests should be sent - because it
/// probably is not the "current request address" - especially in multi-domains configurations.</para>
/// </remarks>
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
2018-06-28 13:50:13 +02:00
// during Initialize / Startup, we end up checking Examine, which needs to be initialized beforehand
// TODO: should not be a strong dependency on "examine" but on an "indexing component"
2019-01-04 10:29:29 +01:00
[ComposeAfter(typeof(ExamineComposer))]
2018-06-28 13:50:13 +02:00
public sealed class DatabaseServerRegistrarAndMessengerComposer : ComponentComposer<DatabaseServerRegistrarAndMessengerComponent>, ICoreComposer
{
2019-03-18 17:51:04 +01:00
public static DatabaseServerMessengerOptions GetDefaultOptions(IFactory factory)
{
2019-03-18 17:51:04 +01:00
var logger = factory.GetInstance<ILogger>();
var indexRebuilder = factory.GetInstance<IndexRebuilder>();
2019-01-03 21:00:28 +01:00
2019-03-18 17:51:04 +01:00
return new DatabaseServerMessengerOptions
{
2019-03-18 17:51:04 +01:00
//These callbacks will be executed if the server has not been synced
// (i.e. it is a new server or the lastsynced.txt file has been removed)
InitializingCallbacks = new Action[]
{
//rebuild the xml cache file if the server is not synced
() =>
{
2019-03-18 17:51:04 +01:00
// rebuild the published snapshot caches entirely, if the server is not synced
// this is equivalent to DistributedCache RefreshAll... but local only
// (we really should have a way to reuse RefreshAll... locally)
// note: refresh all content & media caches does refresh content types too
var svc = Current.PublishedSnapshotService;
svc.Notify(new[] { new DomainCacheRefresher.JsonPayload(0, DomainChangeTypes.RefreshAll) });
svc.Notify(new[] { new ContentCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) }, out _, out _);
svc.Notify(new[] { new MediaCacheRefresher.JsonPayload(0, TreeChangeTypes.RefreshAll) }, out _);
},
//rebuild indexes if the server is not synced
// NOTE: This will rebuild ALL indexes including the members, if developers want to target specific
// indexes then they can adjust this logic themselves.
() => { ExamineComponent.RebuildIndexes(indexRebuilder, logger, false, 5000); }
}
};
}
2019-03-18 17:51:04 +01:00
public override void Compose(Composition composition)
{
base.Compose(composition);
composition.SetDatabaseServerMessengerOptions(GetDefaultOptions);
composition.SetServerMessenger<BatchedDatabaseServerMessenger>();
}
2019-01-03 21:00:28 +01:00
}
public sealed class DatabaseServerRegistrarAndMessengerComponent : IComponent
{
private object _locker = new object();
2019-01-04 08:36:38 +01:00
private readonly DatabaseServerRegistrar _registrar;
private readonly BatchedDatabaseServerMessenger _messenger;
private readonly IRuntimeState _runtime;
private readonly ILogger _logger;
private readonly IServerRegistrationService _registrationService;
private readonly BackgroundTaskRunner<IBackgroundTask> _touchTaskRunner;
private readonly BackgroundTaskRunner<IBackgroundTask> _processTaskRunner;
2019-01-03 21:00:28 +01:00
private bool _started;
private IBackgroundTask[] _tasks;
private IndexRebuilder _indexRebuilder;
2019-01-03 21:00:28 +01:00
public DatabaseServerRegistrarAndMessengerComponent(IRuntimeState runtime, IServerRegistrar serverRegistrar, IServerMessenger serverMessenger, IServerRegistrationService registrationService, ILogger logger, IndexRebuilder indexRebuilder)
{
_runtime = runtime;
_logger = logger;
_registrationService = registrationService;
_indexRebuilder = indexRebuilder;
// create task runner for DatabaseServerRegistrar
_registrar = serverRegistrar as DatabaseServerRegistrar;
if (_registrar != null)
{
_touchTaskRunner = new BackgroundTaskRunner<IBackgroundTask>("ServerRegistration",
new BackgroundTaskRunnerOptions { AutoStart = true }, logger);
}
// create task runner for BatchedDatabaseServerMessenger
_messenger = serverMessenger as BatchedDatabaseServerMessenger;
if (_messenger != null)
{
_processTaskRunner = new BackgroundTaskRunner<IBackgroundTask>("ServerInstProcess",
new BackgroundTaskRunnerOptions { AutoStart = true }, logger);
}
2019-01-07 09:30:47 +01:00
}
2019-01-07 09:30:47 +01:00
public void Initialize()
2019-03-18 17:51:04 +01:00
{
//We will start the whole process when a successful request is made
if (_registrar != null || _messenger != null)
UmbracoModule.RouteAttempt += RegisterBackgroundTasksOnce;
2018-12-05 17:34:34 +01:00
// must come last, as it references some _variables
_messenger?.Startup();
}
2019-01-07 09:30:47 +01:00
public void Terminate()
{ }
/// <summary>
/// Handle when a request is made
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks>
/// We require this because:
/// - ApplicationContext.UmbracoApplicationUrl is initialized by UmbracoModule in BeginRequest
/// - RegisterServer is called on UmbracoModule.RouteAttempt which is triggered in ProcessRequest
/// we are safe, UmbracoApplicationUrl has been initialized
/// </remarks>
2018-03-27 10:04:07 +02:00
private void RegisterBackgroundTasksOnce(object sender, RoutableAttemptEventArgs e)
{
switch (e.Outcome)
{
case EnsureRoutableOutcome.IsRoutable:
case EnsureRoutableOutcome.NotDocumentRequest:
2018-03-27 10:04:07 +02:00
UmbracoModule.RouteAttempt -= RegisterBackgroundTasksOnce;
RegisterBackgroundTasks();
2016-12-14 14:06:30 +01:00
break;
}
}
2016-12-14 14:06:30 +01:00
2018-03-27 10:04:07 +02:00
private void RegisterBackgroundTasks()
{
// only perform this one time ever
2018-03-27 10:04:07 +02:00
LazyInitializer.EnsureInitialized(ref _tasks, ref _started, ref _locker, () =>
{
var serverAddress = _runtime.ApplicationUrl.ToString();
2018-03-27 10:04:07 +02:00
return new[]
{
RegisterInstructionProcess(),
RegisterTouchServer(_registrationService, serverAddress)
};
});
}
2016-12-14 14:06:30 +01:00
2018-03-27 10:04:07 +02:00
private IBackgroundTask RegisterInstructionProcess()
{
if (_messenger == null)
return null;
2018-03-27 10:04:07 +02:00
var task = new InstructionProcessTask(_processTaskRunner,
60000, //delay before first execution
_messenger.Options.ThrottleSeconds*1000, //amount of ms between executions
2018-09-06 14:10:10 +02:00
_messenger,
_logger);
2018-03-27 10:04:07 +02:00
_processTaskRunner.TryAdd(task);
return task;
}
2018-03-27 10:04:07 +02:00
private IBackgroundTask RegisterTouchServer(IServerRegistrationService registrationService, string serverAddress)
{
if (_registrar == null)
return null;
2018-03-27 10:04:07 +02:00
var task = new TouchServerTask(_touchTaskRunner,
15000, //delay before first execution
_registrar.Options.RecurringSeconds*1000, //amount of ms between executions
registrationService, _registrar, serverAddress, _logger);
_touchTaskRunner.TryAdd(task);
return task;
}
private class InstructionProcessTask : RecurringTaskBase
{
private readonly DatabaseServerMessenger _messenger;
2018-09-06 14:10:10 +02:00
private readonly ILogger _logger;
2018-03-27 10:04:07 +02:00
public InstructionProcessTask(IBackgroundTaskRunner<RecurringTaskBase> runner, int delayMilliseconds, int periodMilliseconds,
2018-09-06 14:10:10 +02:00
DatabaseServerMessenger messenger, ILogger logger)
2018-03-27 10:04:07 +02:00
: base(runner, delayMilliseconds, periodMilliseconds)
{
_messenger = messenger;
2018-09-06 14:10:10 +02:00
_logger = logger;
2018-03-27 10:04:07 +02:00
}
public override bool IsAsync => false;
/// <summary>
/// Runs the background task.
/// </summary>
/// <returns>A value indicating whether to repeat the task.</returns>
public override bool PerformRun()
{
2018-09-06 14:10:10 +02:00
try
{
_messenger.Sync();
}
catch (Exception e)
{
_logger.Error<InstructionProcessTask>("Failed (will repeat).", e);
}
2018-03-27 10:04:07 +02:00
return true; // repeat
}
}
private class TouchServerTask : RecurringTaskBase
{
private readonly IServerRegistrationService _svc;
private readonly DatabaseServerRegistrar _registrar;
private readonly string _serverAddress;
private readonly ILogger _logger;
/// <summary>
2018-03-27 10:04:07 +02:00
/// Initializes a new instance of the <see cref="TouchServerTask"/> class.
/// </summary>
public TouchServerTask(IBackgroundTaskRunner<RecurringTaskBase> runner, int delayMilliseconds, int periodMilliseconds,
2018-03-27 10:04:07 +02:00
IServerRegistrationService svc, DatabaseServerRegistrar registrar, string serverAddress, ILogger logger)
: base(runner, delayMilliseconds, periodMilliseconds)
{
2018-03-27 10:04:07 +02:00
_svc = svc ?? throw new ArgumentNullException(nameof(svc));
_registrar = registrar;
_serverAddress = serverAddress;
_logger = logger;
}
public override bool IsAsync => false;
/// <summary>
/// Runs the background task.
/// </summary>
/// <returns>A value indicating whether to repeat the task.</returns>
public override bool PerformRun()
{
try
{
2017-05-12 14:49:44 +02:00
// TouchServer uses a proper unit of work etc underneath so even in a
// background task it is safe to call it without dealing with any scope
_svc.TouchServer(_serverAddress, _svc.CurrentServerIdentity, _registrar.Options.StaleServerTimeout);
return true; // repeat
}
catch (Exception ex)
{
_logger.Error<DatabaseServerRegistrarAndMessengerComponent>(ex, "Failed to update server record in database.");
return false; // probably stop if we have an error
}
}
}
}
}