From 6101b4eddebdd2d07a4f9ff217ddb919a2260eb9 Mon Sep 17 00:00:00 2001 From: Stephan Date: Fri, 18 Nov 2016 09:27:17 +0100 Subject: [PATCH] U4-9201 - use UsingSafeDatabase where appropriate --- src/Umbraco.Web/Scheduling/LogScrubber.cs | 4 +- .../Scheduling/ScheduledPublishing.cs | 22 ++++--- .../ServerRegistrationEventHandler.cs | 14 +++-- .../DataServices/UmbracoContentService.cs | 60 +++++++++---------- 4 files changed, 55 insertions(+), 45 deletions(-) diff --git a/src/Umbraco.Web/Scheduling/LogScrubber.cs b/src/Umbraco.Web/Scheduling/LogScrubber.cs index b3a5f303e3..da49404b4d 100644 --- a/src/Umbraco.Web/Scheduling/LogScrubber.cs +++ b/src/Umbraco.Web/Scheduling/LogScrubber.cs @@ -16,7 +16,7 @@ namespace Umbraco.Web.Scheduling private readonly ApplicationContext _appContext; private readonly IUmbracoSettingsSection _settings; - public LogScrubber(IBackgroundTaskRunner runner, int delayMilliseconds, int periodMilliseconds, + public LogScrubber(IBackgroundTaskRunner 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("Log scrubbing executing", "Log scrubbing complete")) { Log.CleanLogs(GetLogScrubbingMaximumAge(_settings)); diff --git a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs index 6649c3c474..dc6c27959d 100644 --- a/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs +++ b/src/Umbraco.Web/Scheduling/ScheduledPublishing.cs @@ -29,7 +29,7 @@ namespace Umbraco.Web.Scheduling } public override async Task 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( - () => 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; } diff --git a/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs b/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs index a6dfa07be1..84b3e220ff 100644 --- a/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs +++ b/src/Umbraco.Web/Strategies/ServerRegistrationEventHandler.cs @@ -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 } diff --git a/src/UmbracoExamine/DataServices/UmbracoContentService.cs b/src/UmbracoExamine/DataServices/UmbracoContentService.cs index 8f995a125e..4d2ea5bd9a 100644 --- a/src/UmbracoExamine/DataServices/UmbracoContentService.cs +++ b/src/UmbracoExamine/DataServices/UmbracoContentService.cs @@ -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(""); - foreach (var c in _applicationContext.Services.ContentService.GetRootContent()) + using (_applicationContext.DatabaseContext.UsingSafeDatabase) { - xmlContent.Root.Add(c.ToDeepXml(_applicationContext.Services.PackagingService)); + var xmlContent = XDocument.Parse(""); + 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(); + return result.ToXDocument(); } - var result = ((IEnumerable)xmlContent.XPathEvaluate(xpath)).Cast(); - return result.ToXDocument(); } /// @@ -90,7 +80,10 @@ namespace UmbracoExamine.DataServices /// 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)); + } } /// @@ -100,16 +93,19 @@ namespace UmbracoExamine.DataServices public IEnumerable GetAllUserPropertyNames() { - try - { - var result = _applicationContext.DatabaseContext.Database.Fetch("select distinct alias from cmsPropertyType order by alias"); - return result; - } - catch (Exception ex) - { - LogHelper.Error("EXCEPTION OCCURRED reading GetAllUserPropertyNames", ex); - return Enumerable.Empty(); - } + using (_applicationContext.DatabaseContext.UsingSafeDatabase) + { + try + { + var result = _applicationContext.DatabaseContext.Database.Fetch("select distinct alias from cmsPropertyType order by alias"); + return result; + } + catch (Exception ex) + { + LogHelper.Error("EXCEPTION OCCURRED reading GetAllUserPropertyNames", ex); + return Enumerable.Empty(); + } + } } ///