diff --git a/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs b/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs index 52962879d4..80ecc588f4 100644 --- a/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs +++ b/src/Umbraco.Tests/Scheduling/BackgroundTaskRunnerTests.cs @@ -805,7 +805,7 @@ namespace Umbraco.Tests.Scheduling HasRun = true; } - public override Task PerformRunAsync() + public override Task PerformRunAsync(CancellationToken token) { throw new NotImplementedException(); } @@ -889,7 +889,7 @@ namespace Umbraco.Tests.Scheduling Thread.Sleep(_runMilliseconds); } - public override Task PerformRunAsync() + public override Task PerformRunAsync(CancellationToken token) { throw new NotImplementedException(); } diff --git a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs index 78e5f4ce74..575845efba 100644 --- a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs +++ b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs @@ -96,21 +96,6 @@ namespace Umbraco.Web.Scheduling StartUp(); } - /// - /// Returns the current cancellation token - /// - public CancellationToken CurrentCancellationToken - { - get - { - if (_tokenSource == null) - { - throw new InvalidOperationException("The token source has not been created which means the task runner has not been started"); - } - return _tokenSource.Token; - } - } - /// /// Gets the number of tasks in the queue. /// diff --git a/src/Umbraco.Web/Scheduling/LogScrubber.cs b/src/Umbraco.Web/Scheduling/LogScrubber.cs index a9f70a612e..79b4c0f47b 100644 --- a/src/Umbraco.Web/Scheduling/LogScrubber.cs +++ b/src/Umbraco.Web/Scheduling/LogScrubber.cs @@ -1,4 +1,5 @@ using System; +using System.Threading; using System.Threading.Tasks; using System.Web; using System.Web.Caching; @@ -73,7 +74,7 @@ namespace Umbraco.Web.Scheduling } } - public override Task PerformRunAsync() + public override Task PerformRunAsync(CancellationToken token) { throw new NotImplementedException(); } diff --git a/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs b/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs index d710a70e03..dc82795852 100644 --- a/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs +++ b/src/Umbraco.Web/Scheduling/RecurringTaskBase.cs @@ -55,7 +55,7 @@ namespace Umbraco.Web.Scheduling /// Classes inheriting from RecurringTaskBase must implement PerformRun. public virtual async Task RunAsync(CancellationToken token) { - await PerformRunAsync(); + await PerformRunAsync(token); Repeat(); } @@ -95,8 +95,9 @@ namespace Umbraco.Web.Scheduling /// /// Runs the task asynchronously. /// + /// A cancellation token. /// A instance representing the execution of the background task. - public abstract Task PerformRunAsync(); + public abstract Task PerformRunAsync(CancellationToken token); /// /// Gets a new occurence of the recurring task. diff --git a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs index c60226965f..0f7cb8c205 100644 --- a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs +++ b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs @@ -1,15 +1,10 @@ using System; -using System.Diagnostics; -using System.Net; using System.Net.Http; -using System.Net.Http.Headers; -using System.Text; using System.Threading; using System.Threading.Tasks; using Umbraco.Core; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; -using Umbraco.Core.Publishing; using Umbraco.Core.Sync; using Umbraco.Web.Mvc; @@ -19,17 +14,15 @@ namespace Umbraco.Web.Scheduling { private readonly ApplicationContext _appContext; private readonly IUmbracoSettingsSection _settings; - private readonly Func _cancellationToken; private static bool _isPublishingRunning; public ScheduledPublishing(IBackgroundTaskRunner runner, int delayMilliseconds, int periodMilliseconds, - ApplicationContext appContext, IUmbracoSettingsSection settings, Func cancellationToken) + ApplicationContext appContext, IUmbracoSettingsSection settings) : base(runner, delayMilliseconds, periodMilliseconds) { _appContext = appContext; _settings = settings; - _cancellationToken = cancellationToken; } private ScheduledPublishing(ScheduledPublishing source) @@ -49,7 +42,7 @@ namespace Umbraco.Web.Scheduling throw new NotImplementedException(); } - public override async Task PerformRunAsync() + public override async Task PerformRunAsync(CancellationToken token) { if (_appContext == null) return; @@ -88,16 +81,6 @@ namespace Umbraco.Web.Scheduling //pass custom the authorization header request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext); - var token = new CancellationToken(); - try - { - token = _cancellationToken(); - } - catch (InvalidOperationException) - { - //There is no valid token, so we'll continue with the empty one - } - try { var result = await wc.SendAsync(request, token); diff --git a/src/Umbraco.Web/Scheduling/ScheduledTasks.cs b/src/Umbraco.Web/Scheduling/ScheduledTasks.cs index 600cc499de..1015b2d4f6 100644 --- a/src/Umbraco.Web/Scheduling/ScheduledTasks.cs +++ b/src/Umbraco.Web/Scheduling/ScheduledTasks.cs @@ -1,17 +1,13 @@ using System; using System.Collections; -using System.Linq; using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; -using System.Xml; -using Umbraco.Core.Configuration; +using Umbraco.Core; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; -using Umbraco.Core.Publishing; using Umbraco.Core.Sync; -using Umbraco.Core; namespace Umbraco.Web.Scheduling { @@ -23,17 +19,15 @@ namespace Umbraco.Web.Scheduling { private readonly ApplicationContext _appContext; private readonly IUmbracoSettingsSection _settings; - private readonly Func _cancellationToken; private static readonly Hashtable ScheduledTaskTimes = new Hashtable(); private static bool _isPublishingRunning = false; public ScheduledTasks(IBackgroundTaskRunner runner, int delayMilliseconds, int periodMilliseconds, - ApplicationContext appContext, IUmbracoSettingsSection settings, Func cancellationToken) + ApplicationContext appContext, IUmbracoSettingsSection settings) : base(runner, delayMilliseconds, periodMilliseconds) { _appContext = appContext; _settings = settings; - _cancellationToken = cancellationToken; } public ScheduledTasks(ScheduledTasks source) @@ -48,7 +42,7 @@ namespace Umbraco.Web.Scheduling return new ScheduledTasks(this); } - private async Task ProcessTasksAsync() + private async Task ProcessTasksAsync(CancellationToken token) { var scheduledTasks = _settings.ScheduledTasks.Tasks; foreach (var t in scheduledTasks) @@ -71,14 +65,14 @@ namespace Umbraco.Web.Scheduling if (runTask) { - bool taskResult = await GetTaskByHttpAync(t.Url); + var taskResult = await GetTaskByHttpAync(t.Url, token); if (t.Log) LogHelper.Info(string.Format("{0} has been called with response: {1}", t.Alias, taskResult)); } } } - private async Task GetTaskByHttpAync(string url) + private async Task GetTaskByHttpAync(string url, CancellationToken token) { using (var wc = new HttpClient()) { @@ -88,19 +82,10 @@ namespace Umbraco.Web.Scheduling Method = HttpMethod.Get, Content = new StringContent(string.Empty) }; + //TODO: pass custom the authorization header, currently these aren't really secured! //request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext); - var token = new CancellationToken(); - try - { - token = _cancellationToken(); - } - catch (InvalidOperationException) - { - //There is no valid token, so we'll continue with the empty one - } - try { var result = await wc.SendAsync(request, token); @@ -119,7 +104,7 @@ namespace Umbraco.Web.Scheduling throw new NotImplementedException(); } - public override async Task PerformRunAsync() + public override async Task PerformRunAsync(CancellationToken token) { if (ServerEnvironmentHelper.GetStatus(_settings) == CurrentServerEnvironmentStatus.Slave) { @@ -135,7 +120,7 @@ namespace Umbraco.Web.Scheduling try { - await ProcessTasksAsync(); + await ProcessTasksAsync(token); } catch (Exception ee) { diff --git a/src/Umbraco.Web/Scheduling/Scheduler.cs b/src/Umbraco.Web/Scheduling/Scheduler.cs index 904254ee79..409a67028f 100644 --- a/src/Umbraco.Web/Scheduling/Scheduler.cs +++ b/src/Umbraco.Web/Scheduling/Scheduler.cs @@ -66,8 +66,8 @@ namespace Umbraco.Web.Scheduling // scheduled publishing/unpublishing // install on all, will only run on non-slaves servers // both are delayed recurring tasks - _publishingRunner.Add(new ScheduledPublishing(_publishingRunner, 60000, 60000, applicationContext, settings, () => _publishingRunner.CurrentCancellationToken)); - _tasksRunner.Add(new ScheduledTasks(_tasksRunner, 60000, 60000, applicationContext, settings, () => _tasksRunner.CurrentCancellationToken)); + _publishingRunner.Add(new ScheduledPublishing(_publishingRunner, 60000, 60000, applicationContext, settings)); + _tasksRunner.Add(new ScheduledTasks(_tasksRunner, 60000, 60000, applicationContext, settings)); // log scrubbing // install & run on all servers