From 52cf5b5ec334c13675e6132c79fc09367a48d21c Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 4 Jun 2015 10:51:15 +0200 Subject: [PATCH 1/4] Changes header modification to PostReleaseRequestState --- src/Umbraco.Web/UmbracoModule.cs | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/Umbraco.Web/UmbracoModule.cs b/src/Umbraco.Web/UmbracoModule.cs index 6efe4d4d6a..0c0b7eebc3 100644 --- a/src/Umbraco.Web/UmbracoModule.cs +++ b/src/Umbraco.Web/UmbracoModule.cs @@ -596,21 +596,26 @@ namespace Umbraco.Web var httpContext = ((HttpApplication)sender).Context; LogHelper.Debug("Begin request: {0}.", () => httpContext.Request.Url); BeginRequest(new HttpContextWrapper(httpContext)); - - //disable asp.net headers (security) - try - { - httpContext.Response.Headers.Remove("Server"); - //this doesn't normally work since IIS sets it but we'll keep it here anyways. - httpContext.Response.Headers.Remove("X-Powered-By"); - } - catch (PlatformNotSupportedException ex) - { - // can't remove headers this way on IIS6 or cassini. - } - }; + //disable asp.net headers (security) + // This is the correct place to modify headers according to MS: + // https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/65241-Heap-error-from-header-manipulation?p=0#comment220889 + app.PostReleaseRequestState += (sender, args) => + { + var httpContext = ((HttpApplication)sender).Context; + try + { + httpContext.Response.Headers.Remove("Server"); + //this doesn't normally work since IIS sets it but we'll keep it here anyways. + httpContext.Response.Headers.Remove("X-Powered-By"); + } + catch (PlatformNotSupportedException ex) + { + // can't remove headers this way on IIS6 or cassini. + } + }; + app.AuthenticateRequest += AuthenticateRequest; app.PostResolveRequestCache += (sender, e) => From 8e114f1e8f4157465a56239b3aecd05ebabd51e1 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 4 Jun 2015 11:49:58 +0200 Subject: [PATCH 2/4] Scheduled publishing is executing request async with cancellation token --- .../Mvc/AdminTokenAuthorizeAttribute.cs | 18 +++++-- .../Scheduling/BackgroundTaskRunner.cs | 15 ++++++ .../Scheduling/ScheduledPublishing.cs | 47 ++++++++++++++----- src/Umbraco.Web/Scheduling/Scheduler.cs | 2 +- 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/src/Umbraco.Web/Mvc/AdminTokenAuthorizeAttribute.cs b/src/Umbraco.Web/Mvc/AdminTokenAuthorizeAttribute.cs index cbd2e0e519..dc8aa15f6a 100644 --- a/src/Umbraco.Web/Mvc/AdminTokenAuthorizeAttribute.cs +++ b/src/Umbraco.Web/Mvc/AdminTokenAuthorizeAttribute.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Net.Http.Headers; using System.Text; using System.Text.RegularExpressions; using System.Web; @@ -36,22 +37,33 @@ namespace Umbraco.Web.Mvc return _applicationContext ?? ApplicationContext.Current; } + public const string AuthorizationType = "AToken"; + /// - /// Used to return the value that needs to go in the Authorization header + /// Used to return the full value that needs to go in the Authorization header /// /// /// public static string GetAuthHeaderTokenVal(ApplicationContext appContext) { - var admin = appContext.Services.UserService.GetUserById(0); + return string.Format("{0} {1}", AuthorizationType, GetAuthHeaderVal(appContext)); + } + public static AuthenticationHeaderValue GetAuthenticationHeaderValue(ApplicationContext appContext) + { + return new AuthenticationHeaderValue(AuthorizationType, GetAuthHeaderVal(appContext)); + } + + private static string GetAuthHeaderVal(ApplicationContext appContext) + { + var admin = appContext.Services.UserService.GetUserById(0); var token = string.Format("{0}u____u{1}u____u{2}", admin.Email, admin.Username, admin.RawPasswordValue); var encrypted = token.EncryptWithMachineKey(); var bytes = Encoding.UTF8.GetBytes(encrypted); var base64 = Convert.ToBase64String(bytes); - return "AToken val=\"" + base64 + "\""; + return string.Format("val=\"{0}\"", base64); } /// diff --git a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs index 575845efba..78e5f4ce74 100644 --- a/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs +++ b/src/Umbraco.Web/Scheduling/BackgroundTaskRunner.cs @@ -96,6 +96,21 @@ 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/ScheduledPublishing.cs b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs index 9db21fba8a..cbc000b7e6 100644 --- a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs +++ b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs @@ -1,7 +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; @@ -16,15 +19,17 @@ 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) + ApplicationContext appContext, IUmbracoSettingsSection settings, Func cancellationToken) : base(runner, delayMilliseconds, periodMilliseconds) { _appContext = appContext; _settings = settings; + _cancellationToken = cancellationToken; } private ScheduledPublishing(ScheduledPublishing source) @@ -41,6 +46,12 @@ namespace Umbraco.Web.Scheduling public override void PerformRun() { + throw new NotImplementedException(); + } + + public override async Task PerformRunAsync() + { + if (_appContext == null) return; if (ServerEnvironmentHelper.GetStatus(_settings) == CurrentServerEnvironmentStatus.Slave) { @@ -57,7 +68,7 @@ namespace Umbraco.Web.Scheduling var umbracoBaseUrl = ServerEnvironmentHelper.GetCurrentServerUmbracoBaseUrl(_appContext, _settings); try - { + { if (string.IsNullOrWhiteSpace(umbracoBaseUrl)) { @@ -66,13 +77,30 @@ namespace Umbraco.Web.Scheduling else { var url = string.Format("{0}RestServices/ScheduledPublish/Index", umbracoBaseUrl.EnsureEndsWith('/')); - using (var wc = new WebClient()) + using (var wc = new HttpClient()) { + var request = new HttpRequestMessage() + { + RequestUri = new Uri(url), + Method = HttpMethod.Post, + Content = new StringContent(string.Empty) + }; //pass custom the authorization header - wc.Headers.Set("Authorization", AdminTokenAuthorizeAttribute.GetAuthHeaderTokenVal(_appContext)); + request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext); - var result = wc.UploadString(url, ""); - } + var token = new CancellationToken(); + try + { + token = _cancellationToken(); + } + catch (InvalidOperationException) + { + //There is no valid token, so we'll continue with the empty one + } + + var result = await wc.SendAsync(request, token); + + } } } catch (Exception ee) @@ -88,14 +116,9 @@ namespace Umbraco.Web.Scheduling } } - public override Task PerformRunAsync() - { - throw new NotImplementedException(); - } - public override bool IsAsync { - get { return false; } + get { return true; } } public override bool RunsOnShutdown diff --git a/src/Umbraco.Web/Scheduling/Scheduler.cs b/src/Umbraco.Web/Scheduling/Scheduler.cs index 409a67028f..71f2ab59c5 100644 --- a/src/Umbraco.Web/Scheduling/Scheduler.cs +++ b/src/Umbraco.Web/Scheduling/Scheduler.cs @@ -66,7 +66,7 @@ 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.Add(new ScheduledPublishing(_publishingRunner, 60000, 60000, applicationContext, settings, () => _publishingRunner.CurrentCancellationToken)); _tasksRunner.Add(new ScheduledTasks(_tasksRunner, 60000, 60000, applicationContext, settings)); // log scrubbing From e3973de880a7ea99c12a5db14ec60b4bdc6f23b8 Mon Sep 17 00:00:00 2001 From: Shannon Date: Thu, 4 Jun 2015 12:13:27 +0200 Subject: [PATCH 3/4] Updates scheduled tasks to be async with cancellation token --- .../Scheduling/ScheduledPublishing.cs | 10 ++- src/Umbraco.Web/Scheduling/ScheduledTasks.cs | 72 ++++++++++++------- src/Umbraco.Web/Scheduling/Scheduler.cs | 2 +- 3 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs index cbc000b7e6..c60226965f 100644 --- a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs +++ b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs @@ -98,8 +98,14 @@ namespace Umbraco.Web.Scheduling //There is no valid token, so we'll continue with the empty one } - var result = await wc.SendAsync(request, token); - + try + { + var result = await wc.SendAsync(request, token); + } + catch (Exception ex) + { + LogHelper.Error("An error occurred calling scheduled publish url", ex); + } } } } diff --git a/src/Umbraco.Web/Scheduling/ScheduledTasks.cs b/src/Umbraco.Web/Scheduling/ScheduledTasks.cs index cba3cb4fc8..600cc499de 100644 --- a/src/Umbraco.Web/Scheduling/ScheduledTasks.cs +++ b/src/Umbraco.Web/Scheduling/ScheduledTasks.cs @@ -2,6 +2,8 @@ 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; @@ -21,15 +23,17 @@ 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) + ApplicationContext appContext, IUmbracoSettingsSection settings, Func cancellationToken) : base(runner, delayMilliseconds, periodMilliseconds) { _appContext = appContext; _settings = settings; + _cancellationToken = cancellationToken; } public ScheduledTasks(ScheduledTasks source) @@ -44,18 +48,19 @@ namespace Umbraco.Web.Scheduling return new ScheduledTasks(this); } - private void ProcessTasks() + private async Task ProcessTasksAsync() { var scheduledTasks = _settings.ScheduledTasks.Tasks; foreach (var t in scheduledTasks) { var runTask = false; - if (!ScheduledTaskTimes.ContainsKey(t.Alias)) + if (ScheduledTaskTimes.ContainsKey(t.Alias) == false) { runTask = true; ScheduledTaskTimes.Add(t.Alias, DateTime.Now); } - /// Add 1 second to timespan to compensate for differencies in timer + + // Add 1 second to timespan to compensate for differencies in timer else if ( new TimeSpan( DateTime.Now.Ticks - ((DateTime)ScheduledTaskTimes[t.Alias]).Ticks).TotalSeconds + 1 >= t.Interval) @@ -66,33 +71,55 @@ namespace Umbraco.Web.Scheduling if (runTask) { - bool taskResult = GetTaskByHttp(t.Url); + bool taskResult = await GetTaskByHttpAync(t.Url); if (t.Log) LogHelper.Info(string.Format("{0} has been called with response: {1}", t.Alias, taskResult)); } } } - private bool GetTaskByHttp(string url) + private async Task GetTaskByHttpAync(string url) { - var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url); - - try + using (var wc = new HttpClient()) { - using (var response = (HttpWebResponse)myHttpWebRequest.GetResponse()) + var request = new HttpRequestMessage() { - return response.StatusCode == HttpStatusCode.OK; - } - } - catch (Exception ex) - { - LogHelper.Error("An error occurred calling web task for url: " + url, ex); - } + RequestUri = new Uri(url), + 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); - return false; + 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); + return result.StatusCode == HttpStatusCode.OK; + } + catch (Exception ex) + { + LogHelper.Error("An error occurred calling web task for url: " + url, ex); + } + return false; + } } public override void PerformRun() + { + throw new NotImplementedException(); + } + + public override async Task PerformRunAsync() { if (ServerEnvironmentHelper.GetStatus(_settings) == CurrentServerEnvironmentStatus.Slave) { @@ -108,7 +135,7 @@ namespace Umbraco.Web.Scheduling try { - ProcessTasks(); + await ProcessTasksAsync(); } catch (Exception ee) { @@ -121,14 +148,9 @@ namespace Umbraco.Web.Scheduling } } - public override Task PerformRunAsync() - { - throw new NotImplementedException(); - } - public override bool IsAsync { - get { return false; } + get { return true; } } public override bool RunsOnShutdown diff --git a/src/Umbraco.Web/Scheduling/Scheduler.cs b/src/Umbraco.Web/Scheduling/Scheduler.cs index 71f2ab59c5..904254ee79 100644 --- a/src/Umbraco.Web/Scheduling/Scheduler.cs +++ b/src/Umbraco.Web/Scheduling/Scheduler.cs @@ -67,7 +67,7 @@ namespace Umbraco.Web.Scheduling // 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.Add(new ScheduledTasks(_tasksRunner, 60000, 60000, applicationContext, settings, () => _tasksRunner.CurrentCancellationToken)); // log scrubbing // install & run on all servers From 07e9a3a4ea27e167816e2cce8cea1b89258b01a8 Mon Sep 17 00:00:00 2001 From: Stephan Date: Thu, 4 Jun 2015 13:57:45 +0200 Subject: [PATCH 4/4] Refactor scheduled tasks & publishing cancellation --- .../Scheduling/BackgroundTaskRunnerTests.cs | 4 +-- .../Scheduling/BackgroundTaskRunner.cs | 15 --------- src/Umbraco.Web/Scheduling/LogScrubber.cs | 3 +- .../Scheduling/RecurringTaskBase.cs | 5 +-- .../Scheduling/ScheduledPublishing.cs | 21 ++----------- src/Umbraco.Web/Scheduling/ScheduledTasks.cs | 31 +++++-------------- src/Umbraco.Web/Scheduling/Scheduler.cs | 4 +-- 7 files changed, 19 insertions(+), 64 deletions(-) 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