2015-07-06 16:56:01 +02:00
|
|
|
|
using System.Web;
|
2014-06-20 14:34:21 +10:00
|
|
|
|
using Umbraco.Core;
|
2014-11-12 16:00:17 +11:00
|
|
|
|
using Umbraco.Core.Configuration;
|
2014-06-25 11:00:11 +10:00
|
|
|
|
using Umbraco.Core.Logging;
|
2014-06-20 14:34:21 +10:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Scheduling
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Used to do the scheduling for tasks, publishing, etc...
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
2014-11-12 16:00:17 +11:00
|
|
|
|
/// All tasks are run in a background task runner which is web aware and will wind down the task correctly instead of killing it completely when
|
|
|
|
|
|
/// the app domain shuts down.
|
2014-06-20 14:34:21 +10:00
|
|
|
|
/// </remarks>
|
2014-06-25 11:00:11 +10:00
|
|
|
|
internal sealed class Scheduler : ApplicationEventHandler
|
|
|
|
|
|
{
|
2015-07-06 16:56:01 +02:00
|
|
|
|
private static BackgroundTaskRunner<IBackgroundTask> _keepAliveRunner;
|
2015-02-06 18:10:19 +01:00
|
|
|
|
private static BackgroundTaskRunner<IBackgroundTask> _publishingRunner;
|
|
|
|
|
|
private static BackgroundTaskRunner<IBackgroundTask> _tasksRunner;
|
|
|
|
|
|
private static BackgroundTaskRunner<IBackgroundTask> _scrubberRunner;
|
|
|
|
|
|
private static volatile bool _started;
|
2014-11-12 16:00:17 +11:00
|
|
|
|
private static readonly object Locker = new object();
|
2014-06-20 14:34:21 +10:00
|
|
|
|
|
|
|
|
|
|
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (umbracoApplication.Context == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2014-11-12 16:00:17 +11:00
|
|
|
|
//subscribe to app init so we can subsribe to the application events
|
|
|
|
|
|
UmbracoApplicationBase.ApplicationInit += (sender, args) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var app = (HttpApplication)sender;
|
|
|
|
|
|
|
|
|
|
|
|
//subscribe to the end of a successful request (a handler actually executed)
|
|
|
|
|
|
app.PostRequestHandlerExecute += (o, eventArgs) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_started == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
lock (Locker)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_started == false)
|
|
|
|
|
|
{
|
|
|
|
|
|
_started = true;
|
|
|
|
|
|
LogHelper.Debug<Scheduler>(() => "Initializing the scheduler");
|
2014-06-25 11:36:58 +10:00
|
|
|
|
|
2015-02-06 18:10:19 +01:00
|
|
|
|
// backgrounds runners are web aware, if the app domain dies, these tasks will wind down correctly
|
2015-07-06 16:56:01 +02:00
|
|
|
|
_keepAliveRunner = new BackgroundTaskRunner<IBackgroundTask>("KeepAlive", applicationContext.ProfilingLogger.Logger);
|
2015-05-21 17:12:30 +10:00
|
|
|
|
_publishingRunner = new BackgroundTaskRunner<IBackgroundTask>("ScheduledPublishing", applicationContext.ProfilingLogger.Logger);
|
|
|
|
|
|
_tasksRunner = new BackgroundTaskRunner<IBackgroundTask>("ScheduledTasks", applicationContext.ProfilingLogger.Logger);
|
|
|
|
|
|
_scrubberRunner = new BackgroundTaskRunner<IBackgroundTask>("LogScrubber", applicationContext.ProfilingLogger.Logger);
|
2014-06-20 14:34:21 +10:00
|
|
|
|
|
2015-02-06 18:10:19 +01:00
|
|
|
|
var settings = UmbracoConfig.For.UmbracoSettings();
|
2014-06-20 14:34:21 +10:00
|
|
|
|
|
2015-07-06 16:56:01 +02:00
|
|
|
|
// ping/keepalive
|
|
|
|
|
|
// on all servers
|
|
|
|
|
|
_keepAliveRunner.Add(new KeepAlive(_keepAliveRunner, 60000, 300000, applicationContext));
|
2014-06-25 11:00:11 +10:00
|
|
|
|
|
2014-11-12 16:00:17 +11:00
|
|
|
|
// scheduled publishing/unpublishing
|
2015-02-06 18:10:19 +01:00
|
|
|
|
// install on all, will only run on non-slaves servers
|
|
|
|
|
|
_publishingRunner.Add(new ScheduledPublishing(_publishingRunner, 60000, 60000, applicationContext, settings));
|
|
|
|
|
|
_tasksRunner.Add(new ScheduledTasks(_tasksRunner, 60000, 60000, applicationContext, settings));
|
|
|
|
|
|
|
|
|
|
|
|
// log scrubbing
|
2015-07-06 16:56:01 +02:00
|
|
|
|
// install on all, will only run on non-slaves servers
|
2015-02-06 18:10:19 +01:00
|
|
|
|
_scrubberRunner.Add(new LogScrubber(_scrubberRunner, 60000, LogScrubber.GetLogScrubbingInterval(settings), applicationContext, settings));
|
2014-11-12 16:00:17 +11:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2014-06-25 11:00:11 +10:00
|
|
|
|
}
|
2014-06-20 14:34:21 +10:00
|
|
|
|
}
|