Updates scheduled tasks to be async with cancellation token
This commit is contained in:
@@ -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<ScheduledPublishing>("An error occurred calling scheduled publish url", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> _cancellationToken;
|
||||
private static readonly Hashtable ScheduledTaskTimes = new Hashtable();
|
||||
private static bool _isPublishingRunning = false;
|
||||
|
||||
public ScheduledTasks(IBackgroundTaskRunner<ScheduledTasks> runner, int delayMilliseconds, int periodMilliseconds,
|
||||
ApplicationContext appContext, IUmbracoSettingsSection settings)
|
||||
ApplicationContext appContext, IUmbracoSettingsSection settings, Func<CancellationToken> 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<ScheduledTasks>(string.Format("{0} has been called with response: {1}", t.Alias, taskResult));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool GetTaskByHttp(string url)
|
||||
private async Task<bool> 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<ScheduledTasks>("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<ScheduledTasks>("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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user