U4-9201 - use UsingSafeDatabase where appropriate

This commit is contained in:
Stephan
2016-11-18 09:27:17 +01:00
parent 13ed3757da
commit 6101b4edde
4 changed files with 55 additions and 45 deletions

View File

@@ -16,7 +16,7 @@ namespace Umbraco.Web.Scheduling
private readonly ApplicationContext _appContext;
private readonly IUmbracoSettingsSection _settings;
public LogScrubber(IBackgroundTaskRunner<RecurringTaskBase> runner, int delayMilliseconds, int periodMilliseconds,
public LogScrubber(IBackgroundTaskRunner<RecurringTaskBase> runner, int delayMilliseconds, int periodMilliseconds,
ApplicationContext appContext, IUmbracoSettingsSection settings)
: base(runner, delayMilliseconds, periodMilliseconds)
{
@@ -77,6 +77,8 @@ namespace Umbraco.Web.Scheduling
return false; // do NOT repeat, going down
}
// running on a background task, requires a safe database (see UsingSafeDatabase doc)
using (ApplicationContext.Current.DatabaseContext.UsingSafeDatabase)
using (DisposableTimer.DebugDuration<LogScrubber>("Log scrubbing executing", "Log scrubbing complete"))
{
Log.CleanLogs(GetLogScrubbingMaximumAge(_settings));

View File

@@ -29,7 +29,7 @@ namespace Umbraco.Web.Scheduling
}
public override async Task<bool> PerformRunAsync(CancellationToken token)
{
{
if (_appContext == null) return true; // repeat...
switch (_appContext.GetCurrentServerRole())
@@ -70,19 +70,27 @@ namespace Umbraco.Web.Scheduling
var url = umbracoAppUrl + "/RestServices/ScheduledPublish/Index";
using (DisposableTimer.DebugDuration<ScheduledPublishing>(
() => string.Format("Scheduled publishing executing @ {0}", url),
() => string.Format("Scheduled publishing executing @ {0}", url),
() => "Scheduled publishing complete"))
{
{
try
{
{
using (var wc = new HttpClient())
{
var request = new HttpRequestMessage(HttpMethod.Post, url)
{
Content = new StringContent(string.Empty)
};
//pass custom the authorization header
request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext);
// running on a background task, requires a safe database (see UsingSafeDatabase doc)
//
// this is because GetAuthenticationHeaderValue uses UserService to load the current user, hence requires a database
//
using (ApplicationContext.Current.DatabaseContext.UsingSafeDatabase)
{
//pass custom the authorization header
request.Headers.Authorization = AdminTokenAuthorizeAttribute.GetAuthenticationHeaderValue(_appContext);
}
var result = await wc.SendAsync(request, token);
}
@@ -100,7 +108,7 @@ namespace Umbraco.Web.Scheduling
{
get { return true; }
}
public override bool RunsOnShutdown
{
get { return false; }

View File

@@ -35,7 +35,7 @@ namespace Umbraco.Web.Strategies
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
_registrar = ServerRegistrarResolver.Current.Registrar as DatabaseServerRegistrar;
// only for the DatabaseServerRegistrar
if (_registrar == null) return;
@@ -65,10 +65,10 @@ namespace Umbraco.Web.Strategies
case EnsureRoutableOutcome.IsRoutable:
case EnsureRoutableOutcome.NotDocumentRequest:
RegisterBackgroundTasks(e);
break;
break;
}
}
private void RegisterBackgroundTasks(UmbracoRequestEventArgs e)
{
//remove handler, we're done
@@ -84,7 +84,7 @@ namespace Umbraco.Web.Strategies
15000, //delay before first execution
_registrar.Options.RecurringSeconds*1000, //amount of ms between executions
svc, _registrar, serverAddress);
//Perform the rest async, we don't want to block the startup sequence
// this will just reoccur on a background thread
_backgroundTaskRunner.TryAdd(task);
@@ -137,7 +137,11 @@ namespace Umbraco.Web.Strategies
{
try
{
_svc.TouchServer(_serverAddress, _svc.CurrentServerIdentity, _registrar.Options.StaleServerTimeout);
// running on a background task, requires a safe database (see UsingSafeDatabase doc)
using (ApplicationContext.Current.DatabaseContext.UsingSafeDatabase)
{
_svc.TouchServer(_serverAddress, _svc.CurrentServerIdentity, _registrar.Options.StaleServerTimeout);
}
return true; // repeat
}

View File

@@ -1,26 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core.Persistence;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Services;
using umbraco;
using System.Xml.Linq;
using System.Xml;
using umbraco.cms.businesslogic.web;
using System.Collections;
using System.Xml.XPath;
using umbraco.DataLayer;
using umbraco.BusinessLogic;
using UmbracoExamine.Config;
using Examine.LuceneEngine;
using System.Data.SqlClient;
using System.Diagnostics;
namespace UmbracoExamine.DataServices
{
@@ -31,9 +18,7 @@ namespace UmbracoExamine.DataServices
public UmbracoContentService()
: this(ApplicationContext.Current)
{
}
{ }
public UmbracoContentService(ApplicationContext applicationContext)
{
@@ -73,13 +58,18 @@ namespace UmbracoExamine.DataServices
[Obsolete("This should no longer be used, latest content will be indexed by using the IContentService directly")]
public XDocument GetLatestContentByXPath(string xpath)
{
var xmlContent = XDocument.Parse("<content></content>");
foreach (var c in _applicationContext.Services.ContentService.GetRootContent())
using (_applicationContext.DatabaseContext.UsingSafeDatabase)
{
xmlContent.Root.Add(c.ToDeepXml(_applicationContext.Services.PackagingService));
var xmlContent = XDocument.Parse("<content></content>");
var rootContent = _applicationContext.Services.ContentService.GetRootContent();
foreach (var c in rootContent)
{
// not sure this uses the database, but better be save
xmlContent.Root.Add(c.ToDeepXml(_applicationContext.Services.PackagingService));
}
var result = ((IEnumerable)xmlContent.XPathEvaluate(xpath)).Cast<XElement>();
return result.ToXDocument();
}
var result = ((IEnumerable)xmlContent.XPathEvaluate(xpath)).Cast<XElement>();
return result.ToXDocument();
}
/// <summary>
@@ -90,7 +80,10 @@ namespace UmbracoExamine.DataServices
/// <returns></returns>
public bool IsProtected(int nodeId, string path)
{
return _applicationContext.Services.PublicAccessService.IsProtected(path.EnsureEndsWith("," + nodeId));
using (_applicationContext.DatabaseContext.UsingSafeDatabase)
{
return _applicationContext.Services.PublicAccessService.IsProtected(path.EnsureEndsWith("," + nodeId));
}
}
/// <summary>
@@ -100,16 +93,19 @@ namespace UmbracoExamine.DataServices
public IEnumerable<string> GetAllUserPropertyNames()
{
try
{
var result = _applicationContext.DatabaseContext.Database.Fetch<string>("select distinct alias from cmsPropertyType order by alias");
return result;
}
catch (Exception ex)
{
LogHelper.Error<UmbracoContentService>("EXCEPTION OCCURRED reading GetAllUserPropertyNames", ex);
return Enumerable.Empty<string>();
}
using (_applicationContext.DatabaseContext.UsingSafeDatabase)
{
try
{
var result = _applicationContext.DatabaseContext.Database.Fetch<string>("select distinct alias from cmsPropertyType order by alias");
return result;
}
catch (Exception ex)
{
LogHelper.Error<UmbracoContentService>("EXCEPTION OCCURRED reading GetAllUserPropertyNames", ex);
return Enumerable.Empty<string>();
}
}
}
/// <summary>