Files
Umbraco-CMS/src/Umbraco.Web/Scheduling/SchedulerComponent.cs

159 lines
6.7 KiB
C#
Raw Normal View History

2017-09-13 17:35:20 +02:00
using System;
using System.Collections.Generic;
using System.Threading;
using Umbraco.Core;
using Umbraco.Core.Composing;
2019-01-07 10:43:28 +01:00
using Umbraco.Core.Configuration;
2017-09-13 17:35:20 +02:00
using Umbraco.Core.Configuration.HealthChecks;
using Umbraco.Core.Configuration.UmbracoSettings;
2014-06-25 11:00:11 +10:00
using Umbraco.Core.Logging;
2017-05-12 14:49:44 +02:00
using Umbraco.Core.Scoping;
using Umbraco.Core.Services;
2017-09-13 17:35:20 +02:00
using Umbraco.Web.HealthCheck;
using Umbraco.Web.Routing;
namespace Umbraco.Web.Scheduling
{
2019-01-03 21:00:28 +01:00
internal sealed class SchedulerComponent : IComponent
2014-06-25 11:00:11 +10:00
{
2019-01-03 21:00:28 +01:00
private readonly IRuntimeState _runtime;
private readonly IContentService _contentService;
private readonly IAuditService _auditService;
private readonly IProfilingLogger _logger;
private readonly IScopeProvider _scopeProvider;
private readonly HealthCheckCollection _healthChecks;
private readonly HealthCheckNotificationMethodCollection _notifications;
2019-02-14 12:11:06 +01:00
private readonly IUmbracoContextFactory _umbracoContextFactory;
2019-01-03 21:00:28 +01:00
2019-01-07 09:30:47 +01:00
private BackgroundTaskRunner<IBackgroundTask> _keepAliveRunner;
private BackgroundTaskRunner<IBackgroundTask> _publishingRunner;
private BackgroundTaskRunner<IBackgroundTask> _tasksRunner;
private BackgroundTaskRunner<IBackgroundTask> _scrubberRunner;
private BackgroundTaskRunner<IBackgroundTask> _healthCheckRunner;
private bool _started;
private object _locker = new object();
private IBackgroundTask[] _tasks;
2019-01-03 21:00:28 +01:00
public SchedulerComponent(IRuntimeState runtime,
2018-11-08 16:33:19 +01:00
IContentService contentService, IAuditService auditService,
2017-09-13 17:35:20 +02:00
HealthCheckCollection healthChecks, HealthCheckNotificationMethodCollection notifications,
2019-02-14 12:11:06 +01:00
IScopeProvider scopeProvider, IUmbracoContextFactory umbracoContextFactory, IProfilingLogger logger)
{
_runtime = runtime;
2017-09-13 17:35:20 +02:00
_contentService = contentService;
_auditService = auditService;
2017-05-12 14:49:44 +02:00
_scopeProvider = scopeProvider;
_logger = logger;
2019-02-14 12:11:06 +01:00
_umbracoContextFactory = umbracoContextFactory;
2017-09-13 17:35:20 +02:00
_healthChecks = healthChecks;
_notifications = notifications;
2019-01-07 09:30:47 +01:00
}
2017-09-13 17:35:20 +02:00
2019-01-07 09:30:47 +01:00
public void Initialize()
2019-01-07 10:43:28 +01:00
{
// backgrounds runners are web aware, if the app domain dies, these tasks will wind down correctly
2019-01-07 09:30:47 +01: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);
_healthCheckRunner = new BackgroundTaskRunner<IBackgroundTask>("HealthCheckNotifier", _logger);
// we will start the whole process when a successful request is made
2018-03-27 10:04:07 +02:00
UmbracoModule.RouteAttempt += RegisterBackgroundTasksOnce;
}
2019-01-07 09:30:47 +01:00
public void Terminate()
{
// the AppDomain / maindom / whatever takes care of stopping background task runners
2019-01-07 09:30:47 +01:00
}
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;
2017-09-13 17:35:20 +02:00
RegisterBackgroundTasks();
break;
}
}
2017-09-13 17:35:20 +02:00
private void RegisterBackgroundTasks()
{
LazyInitializer.EnsureInitialized(ref _tasks, ref _started, ref _locker, () =>
{
_logger.Debug<SchedulerComponent>("Initializing the scheduler");
2019-01-07 10:43:28 +01:00
var settings = Current.Configs.Settings();
2017-09-13 17:35:20 +02:00
var tasks = new List<IBackgroundTask>();
2017-09-13 17:35:20 +02:00
tasks.Add(RegisterKeepAlive());
tasks.Add(RegisterScheduledPublishing());
tasks.Add(RegisterLogScrubber(settings));
2014-06-25 11:00:11 +10:00
2019-01-07 10:43:28 +01:00
var healthCheckConfig = Current.Configs.HealthChecks();
2017-09-13 17:35:20 +02:00
if (healthCheckConfig.NotificationSettings.Enabled)
2019-01-03 21:00:28 +01:00
tasks.Add(RegisterHealthCheckNotifier(healthCheckConfig, _healthChecks, _notifications, _logger));
return tasks.ToArray();
});
}
2017-09-13 17:35:20 +02:00
private IBackgroundTask RegisterKeepAlive()
{
// ping/keepalive
// on all servers
2019-01-03 21:00:28 +01:00
var task = new KeepAlive(_keepAliveRunner, 60000, 300000, _runtime, _logger);
2017-09-13 17:35:20 +02:00
_keepAliveRunner.TryAdd(task);
return task;
}
private IBackgroundTask RegisterScheduledPublishing()
{
// scheduled publishing/unpublishing
2018-10-02 11:14:04 +02:00
// install on all, will only run on non-replica servers
2019-02-14 12:11:06 +01:00
var task = new ScheduledPublishing(_publishingRunner, 60000, 60000, _runtime, _contentService, _umbracoContextFactory, _logger);
2017-09-13 17:35:20 +02:00
_publishingRunner.TryAdd(task);
return task;
}
2019-02-14 12:11:06 +01:00
2017-09-13 17:35:20 +02:00
private IBackgroundTask RegisterHealthCheckNotifier(IHealthChecks healthCheckConfig,
HealthCheckCollection healthChecks, HealthCheckNotificationMethodCollection notifications,
2019-01-03 21:00:28 +01:00
IProfilingLogger logger)
2017-09-13 17:35:20 +02:00
{
// If first run time not set, start with just small delay after application start
int delayInMilliseconds;
if (string.IsNullOrEmpty(healthCheckConfig.NotificationSettings.FirstRunTime))
{
delayInMilliseconds = 60000;
}
else
{
// Otherwise start at scheduled time
delayInMilliseconds = DateTime.Now.PeriodicMinutesFrom(healthCheckConfig.NotificationSettings.FirstRunTime) * 60 * 1000;
if (delayInMilliseconds < 60000)
{
delayInMilliseconds = 60000;
}
}
var periodInMilliseconds = healthCheckConfig.NotificationSettings.PeriodInHours * 60 * 60 * 1000;
2019-01-03 21:00:28 +01:00
var task = new HealthCheckNotifier(_healthCheckRunner, delayInMilliseconds, periodInMilliseconds, healthChecks, notifications, _runtime, logger);
2019-01-15 17:27:18 +01:00
_healthCheckRunner.TryAdd(task);
2017-09-13 17:35:20 +02:00
return task;
}
private IBackgroundTask RegisterLogScrubber(IUmbracoSettingsSection settings)
{
// log scrubbing
2018-10-02 11:14:04 +02:00
// install on all, will only run on non-replica servers
2019-01-03 21:00:28 +01:00
var task = new LogScrubber(_scrubberRunner, 60000, LogScrubber.GetLogScrubbingInterval(settings, _logger), _runtime, _auditService, settings, _scopeProvider, _logger);
2017-09-13 17:35:20 +02:00
_scrubberRunner.TryAdd(task);
return task;
}
2014-06-25 11:00:11 +10:00
}
}