2016-03-10 15:17:46 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading;
|
2014-06-20 14:34:21 +10:00
|
|
|
|
using Umbraco.Core;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
using Umbraco.Core.Components;
|
2014-11-12 16:00:17 +11:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2014-06-25 11:00:11 +10:00
|
|
|
|
using Umbraco.Core.Logging;
|
2016-12-16 17:56:10 +01:00
|
|
|
|
using Umbraco.Core.Persistence;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using Umbraco.Core.Scoping;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
using Umbraco.Core.Services;
|
2016-03-10 15:17:46 +01:00
|
|
|
|
using Umbraco.Web.Routing;
|
2014-06-20 14:34:21 +10:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Scheduling
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used to do the scheduling for tasks, publishing, etc...
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
2016-12-14 14:06:30 +01:00
|
|
|
|
/// All tasks are run in a background task runner which is web aware and will wind down
|
2016-09-01 19:06:08 +02:00
|
|
|
|
/// the task correctly instead of killing it completely when the app domain shuts down.
|
2014-06-20 14:34:21 +10:00
|
|
|
|
/// </remarks>
|
2016-10-07 14:34:55 +02:00
|
|
|
|
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
|
2016-09-01 19:06:08 +02:00
|
|
|
|
internal sealed class SchedulerComponent : UmbracoComponentBase, IUmbracoCoreComponent
|
2014-06-25 11:00:11 +10:00
|
|
|
|
{
|
2016-09-01 19:06:08 +02:00
|
|
|
|
private IRuntimeState _runtime;
|
|
|
|
|
|
private IUserService _userService;
|
|
|
|
|
|
private IAuditService _auditService;
|
2016-09-11 19:57:33 +02:00
|
|
|
|
private ILogger _logger;
|
|
|
|
|
|
private ProfilingLogger _proflog;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
private IScopeProvider _scopeProvider;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
|
2016-03-10 15:17:46 +01:00
|
|
|
|
private BackgroundTaskRunner<IBackgroundTask> _keepAliveRunner;
|
|
|
|
|
|
private BackgroundTaskRunner<IBackgroundTask> _publishingRunner;
|
|
|
|
|
|
private BackgroundTaskRunner<IBackgroundTask> _tasksRunner;
|
|
|
|
|
|
private BackgroundTaskRunner<IBackgroundTask> _scrubberRunner;
|
2016-09-01 19:06:08 +02:00
|
|
|
|
|
|
|
|
|
|
private bool _started;
|
2016-03-10 15:17:46 +01:00
|
|
|
|
private object _locker = new object();
|
|
|
|
|
|
private IBackgroundTask[] _tasks;
|
2014-06-20 14:34:21 +10:00
|
|
|
|
|
2017-05-12 14:49:44 +02:00
|
|
|
|
public void Initialize(IRuntimeState runtime, IUserService userService, IAuditService auditService, IScopeProvider scopeProvider, ILogger logger, ProfilingLogger proflog)
|
2014-06-20 14:34:21 +10:00
|
|
|
|
{
|
2016-09-01 19:06:08 +02:00
|
|
|
|
_runtime = runtime;
|
|
|
|
|
|
_userService = userService;
|
|
|
|
|
|
_auditService = auditService;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
_scopeProvider = scopeProvider;
|
2016-09-11 19:57:33 +02:00
|
|
|
|
_logger = logger;
|
|
|
|
|
|
_proflog = proflog;
|
2014-06-20 14:34:21 +10:00
|
|
|
|
|
2016-03-10 15:17:46 +01:00
|
|
|
|
// backgrounds runners are web aware, if the app domain dies, these tasks will wind down correctly
|
2016-09-01 19:06:08 +02:00
|
|
|
|
_keepAliveRunner = new BackgroundTaskRunner<IBackgroundTask>("KeepAlive", logger);
|
|
|
|
|
|
_publishingRunner = new BackgroundTaskRunner<IBackgroundTask>("ScheduledPublishing", logger);
|
|
|
|
|
|
_tasksRunner = new BackgroundTaskRunner<IBackgroundTask>("ScheduledTasks", logger);
|
|
|
|
|
|
_scrubberRunner = new BackgroundTaskRunner<IBackgroundTask>("LogScrubber", logger);
|
2016-03-10 15:17:46 +01:00
|
|
|
|
|
2016-09-01 19:06:08 +02:00
|
|
|
|
// we will start the whole process when a successful request is made
|
2016-03-10 15:17:46 +01:00
|
|
|
|
UmbracoModule.RouteAttempt += UmbracoModuleRouteAttempt;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void UmbracoModuleRouteAttempt(object sender, RoutableAttemptEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (e.Outcome)
|
2014-11-12 16:00:17 +11:00
|
|
|
|
{
|
2016-03-10 15:17:46 +01:00
|
|
|
|
case EnsureRoutableOutcome.IsRoutable:
|
|
|
|
|
|
case EnsureRoutableOutcome.NotDocumentRequest:
|
|
|
|
|
|
RegisterBackgroundTasks(e);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2014-11-12 16:00:17 +11:00
|
|
|
|
|
2016-03-10 15:17:46 +01:00
|
|
|
|
private void RegisterBackgroundTasks(UmbracoRequestEventArgs e)
|
|
|
|
|
|
{
|
2016-09-01 19:06:08 +02:00
|
|
|
|
// remove handler, we're done
|
2016-03-10 15:17:46 +01:00
|
|
|
|
UmbracoModule.RouteAttempt -= UmbracoModuleRouteAttempt;
|
|
|
|
|
|
|
|
|
|
|
|
LazyInitializer.EnsureInitialized(ref _tasks, ref _started, ref _locker, () =>
|
|
|
|
|
|
{
|
2016-09-11 19:57:33 +02:00
|
|
|
|
_logger.Debug<SchedulerComponent>(() => "Initializing the scheduler");
|
2016-03-10 15:17:46 +01:00
|
|
|
|
var settings = UmbracoConfig.For.UmbracoSettings();
|
|
|
|
|
|
|
|
|
|
|
|
var tasks = new List<IBackgroundTask>
|
2014-11-12 16:00:17 +11:00
|
|
|
|
{
|
2016-09-11 19:57:33 +02:00
|
|
|
|
new KeepAlive(_keepAliveRunner, 60000, 300000, _runtime, _logger, _proflog),
|
2017-05-12 14:49:44 +02:00
|
|
|
|
new ScheduledPublishing(_publishingRunner, 60000, 60000, _runtime, _userService, _scopeProvider, _logger, _proflog),
|
2016-09-11 19:57:33 +02:00
|
|
|
|
new ScheduledTasks(_tasksRunner, 60000, 60000, _runtime, settings, _logger, _proflog),
|
2017-05-12 14:49:44 +02:00
|
|
|
|
new LogScrubber(_scrubberRunner, 60000, LogScrubber.GetLogScrubbingInterval(settings, _logger), _runtime, _auditService, settings, _scopeProvider, _logger, _proflog)
|
2016-03-10 15:17:46 +01:00
|
|
|
|
};
|
2014-06-25 11:36:58 +10:00
|
|
|
|
|
2016-03-10 15:17:46 +01:00
|
|
|
|
// ping/keepalive
|
|
|
|
|
|
// on all servers
|
|
|
|
|
|
_keepAliveRunner.TryAdd(tasks[0]);
|
2014-06-20 14:34:21 +10:00
|
|
|
|
|
2016-03-10 15:17:46 +01:00
|
|
|
|
// scheduled publishing/unpublishing
|
|
|
|
|
|
// install on all, will only run on non-slaves servers
|
|
|
|
|
|
_publishingRunner.TryAdd(tasks[1]);
|
2014-06-20 14:34:21 +10:00
|
|
|
|
|
2016-03-10 15:17:46 +01:00
|
|
|
|
_tasksRunner.TryAdd(tasks[2]);
|
2014-06-25 11:00:11 +10:00
|
|
|
|
|
2016-03-10 15:17:46 +01:00
|
|
|
|
// log scrubbing
|
|
|
|
|
|
// install on all, will only run on non-slaves servers
|
|
|
|
|
|
_scrubberRunner.TryAdd(tasks[3]);
|
2015-02-06 18:10:19 +01:00
|
|
|
|
|
2016-03-10 15:17:46 +01:00
|
|
|
|
return tasks.ToArray();
|
|
|
|
|
|
});
|
2014-11-12 16:00:17 +11:00
|
|
|
|
}
|
2014-06-25 11:00:11 +10:00
|
|
|
|
}
|
2014-06-20 14:34:21 +10:00
|
|
|
|
}
|