Scheduled publishing is executing request async with cancellation token
This commit is contained in:
@@ -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";
|
||||
|
||||
/// <summary>
|
||||
/// 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
|
||||
/// </summary>
|
||||
/// <param name="appContext"></param>
|
||||
/// <returns></returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -96,6 +96,21 @@ namespace Umbraco.Web.Scheduling
|
||||
StartUp();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current cancellation token
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of tasks in the queue.
|
||||
/// </summary>
|
||||
|
||||
@@ -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> _cancellationToken;
|
||||
|
||||
private static bool _isPublishingRunning;
|
||||
|
||||
public ScheduledPublishing(IBackgroundTaskRunner<ScheduledPublishing> 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;
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user