From 8fa838e8fb8b777c504571b1ba6ecd4c69e8dc07 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 5 May 2015 12:44:19 +1000 Subject: [PATCH] Fixes merge and re-code's the XmlCacheFilePersister to use ILogger --- .../XmlCacheFilePersister.cs | 38 +++++++++++------- .../umbraco.presentation/content.cs | 39 ++++++++++--------- 2 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlCacheFilePersister.cs b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlCacheFilePersister.cs index fced0ce5d7..4aa52fb95f 100644 --- a/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlCacheFilePersister.cs +++ b/src/Umbraco.Web/PublishedCache/XmlPublishedCache/XmlCacheFilePersister.cs @@ -21,6 +21,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache { private readonly IBackgroundTaskRunner _runner; private readonly content _content; + private readonly ProfilingLogger _logger; private readonly ManualResetEventSlim _latch = new ManualResetEventSlim(false); private readonly object _locko = new object(); private bool _released; @@ -41,14 +42,15 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache public bool RunsOnShutdown { get { return true; } } // initialize the first instance, which is inactive (not touched yet) - public XmlCacheFilePersister(IBackgroundTaskRunner runner, content content) - : this(runner, content, false) + public XmlCacheFilePersister(IBackgroundTaskRunner runner, content content, ProfilingLogger logger) + : this(runner, content, logger, false) { } - private XmlCacheFilePersister(IBackgroundTaskRunner runner, content content, bool touched) + private XmlCacheFilePersister(IBackgroundTaskRunner runner, content content, ProfilingLogger logger, bool touched) { _runner = runner; _content = content; + _logger = logger; if (runner.TryAdd(this) == false) { @@ -61,7 +63,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache if (touched == false) return; - LogHelper.Debug("Created, save in {0}ms.", () => WaitMilliseconds); + _logger.Logger.Debug("Created, save in {0}ms.", () => WaitMilliseconds); _initialTouch = DateTime.Now; _timer = new Timer(_ => TimerRelease()); _timer.Change(WaitMilliseconds, 0); @@ -83,22 +85,22 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache { if (_released) // our timer has triggered OR the runner is shutting down { - LogHelper.Debug("Touched, was released..."); + _logger.Logger.Debug("Touched, was released..."); // release: has run or is running, too late, return a new task (adds itself to runner) if (_runner == null) { - LogHelper.Debug("Runner is down, run now."); + _logger.Logger.Debug("Runner is down, run now."); runNow = true; } else { - LogHelper.Debug("Create new..."); - ret = new XmlCacheFilePersister(_runner, _content, true); + _logger.Logger.Debug("Create new..."); + ret = new XmlCacheFilePersister(_runner, _content, _logger, true); if (ret._runner == null) { // could not enlist with the runner, runner is completed, must run now - LogHelper.Debug("Runner is down, run now."); + _logger.Logger.Debug("Runner is down, run now."); runNow = true; } } @@ -106,7 +108,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache else if (_timer == null) // we don't have a timer yet { - LogHelper.Debug("Touched, was idle, start and save in {0}ms."); + _logger.Logger.Debug("Touched, was idle, start and save in {0}ms.", () => WaitMilliseconds); _initialTouch = DateTime.Now; _timer = new Timer(_ => TimerRelease()); _timer.Change(WaitMilliseconds, 0); @@ -119,12 +121,12 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache if (DateTime.Now - _initialTouch < TimeSpan.FromMilliseconds(MaxWaitMilliseconds)) { - LogHelper.Debug("Touched, was waiting, can delay, save in {0}ms.", () => WaitMilliseconds); + _logger.Logger.Debug("Touched, was waiting, can delay, save in {0}ms.", () => WaitMilliseconds); _timer.Change(WaitMilliseconds, 0); } else { - LogHelper.Debug("Touched, was waiting, cannot delay."); + _logger.Logger.Debug("Touched, was waiting, cannot delay."); } } } @@ -139,7 +141,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache { lock (_locko) { - LogHelper.Debug("Timer: release."); + _logger.Logger.Debug("Timer: release."); if (_timer != null) _timer.Dispose(); _timer = null; @@ -165,7 +167,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache { lock (_locko) { - LogHelper.Debug("Run now (async)."); + _logger.Logger.Debug("Run now (async)."); // just make sure - in case the runner is running the task on shutdown _released = true; } @@ -173,6 +175,12 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache // http://stackoverflow.com/questions/13489065/best-practice-to-call-configureawait-for-all-server-side-code // http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html // do we really need that ConfigureAwait here? + + // - In theory, no, because we are already executing on a background thread because we know it is there and + // there won't be any SynchronizationContext to resume to, however this is 'library' code and + // who are we to say that this will never be executed in a sync context... this is best practice to be sure + // it won't cause problems. + // .... so yes we want it. using (await _runLock.LockAsync()) { @@ -192,7 +200,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache { lock (_locko) { - LogHelper.Debug("Run now (sync)."); + _logger.Logger.Debug("Run now (sync)."); // not really needed but safer (it's only us invoking Run, but the method is public...) _released = true; } diff --git a/src/Umbraco.Web/umbraco.presentation/content.cs b/src/Umbraco.Web/umbraco.presentation/content.cs index 6e4cc3a213..855d04faa2 100644 --- a/src/Umbraco.Web/umbraco.presentation/content.cs +++ b/src/Umbraco.Web/umbraco.presentation/content.cs @@ -34,8 +34,6 @@ namespace umbraco /// public class content { - new BackgroundTaskRunnerOptions { LongRunning = true }, - LoggerResolver.HasCurrent ? LoggerResolver.Current.Logger : new DebugDiagnosticsLogger()); private XmlCacheFilePersister _persisterTask; #region Constructors @@ -44,16 +42,21 @@ namespace umbraco { if (SyncToXmlFile == false) return; + var logger = LoggerResolver.HasCurrent ? LoggerResolver.Current.Logger : new DebugDiagnosticsLogger(); + var profingLogger = new ProfilingLogger( + logger, + ProfilerResolver.HasCurrent ? ProfilerResolver.Current.Profiler : new LogProfiler(logger)); + // there's always be one task keeping a ref to the runner // so it's safe to just create it as a local var here var runner = new BackgroundTaskRunner(new BackgroundTaskRunnerOptions { LongRunning = true, KeepAlive = true - }); + }, logger); // create (and add to runner) - _persisterTask = new XmlCacheFilePersister(runner, this); + _persisterTask = new XmlCacheFilePersister(runner, this, profingLogger); InitializeFileLock(); @@ -191,8 +194,8 @@ namespace umbraco } } - return xmlContentCopy; - } + return xmlContentCopy; + } private static XmlNode GetPreviewOrPublishedNode(Document d, XmlDocument xmlContentCopy, bool isPreview) { @@ -256,8 +259,8 @@ namespace umbraco if (!e.Cancel) { - // lock the xml cache so no other thread can write to it at the same time - // note that some threads could read from it while we hold the lock, though + // lock the xml cache so no other thread can write to it at the same time + // note that some threads could read from it while we hold the lock, though using (var safeXml = GetSafeXmlWriter()) { safeXml.Xml = PublishNodeDo(d, safeXml.Xml, true); @@ -266,8 +269,8 @@ namespace umbraco ClearContextCache(); var cachedFieldKeyStart = string.Format("{0}{1}_", CacheKeys.ContentItemCacheKey, d.Id); - ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(cachedFieldKeyStart); - + ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(cachedFieldKeyStart); + FireAfterUpdateDocumentCache(d, e); } } @@ -336,13 +339,13 @@ namespace umbraco ClearContextCache(); } - + [Obsolete("Method obsolete in version 4.1 and later, please use UpdateDocumentCache", true)] public virtual void UpdateDocumentCacheAsync(int documentId) { UpdateDocumentCache(documentId); } - + [Obsolete("Method obsolete in version 4.1 and later, please use ClearDocumentCache", true)] public virtual void ClearDocumentCacheAsync(int documentId) { @@ -404,7 +407,7 @@ namespace umbraco { var prov = (UmbracoSiteMapProvider)SiteMap.Provider; prov.RemoveNode(doc.Id); - } + } } } @@ -447,7 +450,7 @@ namespace umbraco try { - LogHelper.Debug("Republishing starting"); + LogHelper.Debug("Republishing starting"); lock (DbReadSyncLock) { @@ -760,7 +763,7 @@ order by umbracoNode.level, umbracoNode.sortOrder"; private static XmlDocument Clone(XmlDocument xmlDoc) { - return xmlDoc == null ? null : (XmlDocument) xmlDoc.CloneNode(true); + return xmlDoc == null ? null : (XmlDocument)xmlDoc.CloneNode(true); } private static void EnsureSchema(string contentTypeAlias, XmlDocument xml) @@ -882,7 +885,7 @@ order by umbracoNode.level, umbracoNode.sortOrder"; { return _xml; } - set + set { if (_isWriter == false) throw new InvalidOperationException("Not writing."); @@ -942,7 +945,7 @@ order by umbracoNode.level, umbracoNode.sortOrder"; private AsyncLock _fileLock; // protects the file private IDisposable _fileLocked; // protects the file - private const int FileLockTimeoutMilliseconds = 4*60*1000; // 4' + private const int FileLockTimeoutMilliseconds = 4 * 60 * 1000; // 4' private void InitializeFileLock() { @@ -1327,7 +1330,7 @@ order by umbracoNode.level, umbracoNode.sortOrder"; // append all attributes from the document node to the published node if (documentNode.Attributes == null) throw new Exception("oops"); foreach (XmlAttribute att in documentNode.Attributes) - ((XmlElement) publishedNode).SetAttribute(att.Name, att.Value); + ((XmlElement)publishedNode).SetAttribute(att.Name, att.Value); // find the first child node, if any var childNodes = publishedNode.SelectNodes(ChildNodesXPath);