diff --git a/src/Umbraco.Web/UmbracoModule.cs b/src/Umbraco.Web/UmbracoModule.cs index 9c34af7ab8..86ede93ae1 100644 --- a/src/Umbraco.Web/UmbracoModule.cs +++ b/src/Umbraco.Web/UmbracoModule.cs @@ -498,7 +498,6 @@ namespace Umbraco.Web app.BeginRequest += (sender, e) => { var httpContext = ((HttpApplication)sender).Context; - httpContext.Trace.Write("UmbracoModule", "Umbraco request begins"); LogHelper.Debug("Begin request: {0}.", () => httpContext.Request.Url); BeginRequest(new HttpContextWrapper(httpContext)); }; @@ -523,9 +522,8 @@ namespace Umbraco.Web var httpContext = ((HttpApplication)sender).Context; if (UmbracoContext.Current != null && UmbracoContext.Current.IsFrontEndUmbracoRequest) { - //write the trace output for diagnostics at the end of the request - httpContext.Trace.Write("UmbracoModule", "Umbraco request completed"); - LogHelper.Debug("Total milliseconds for umbraco request to process: " + DateTime.Now.Subtract(UmbracoContext.Current.ObjectCreated).TotalMilliseconds); + LogHelper.Debug( + "Total milliseconds for umbraco request to process: {0}", () => DateTime.Now.Subtract(UmbracoContext.Current.ObjectCreated).TotalMilliseconds); } OnEndRequest(new EventArgs()); diff --git a/src/Umbraco.Web/umbraco.presentation/content.cs b/src/Umbraco.Web/umbraco.presentation/content.cs index ab611bda87..78a5fe7023 100644 --- a/src/Umbraco.Web/umbraco.presentation/content.cs +++ b/src/Umbraco.Web/umbraco.presentation/content.cs @@ -206,7 +206,6 @@ namespace umbraco FireAfterRefreshContent(new RefreshContentEventArgs()); // Only save new XML cache to disk if we just repopulated it - // TODO: Re-architect this so that a call to this method doesn't invoke a new thread for saving disk cache if (!UmbracoSettings.isXmlContentCacheDisabled && !IsValidDiskCachePresent()) { QueueXmlForPersistence(); @@ -215,7 +214,9 @@ namespace umbraco } } } - Trace.WriteLine("Content initialized (was already in context)"); + + LogHelper.Debug(() => "Content initialized (was already in context)"); + return false; } @@ -288,32 +289,33 @@ namespace umbraco #endregion - /// - /// Load content from database in a background thread - /// Replaces active content when done. - /// + [Obsolete("This is no longer used and will be removed in future versions, if you use this method it will not refresh 'async' it will perform the refresh on the current thread which is how it should be doing it")] public virtual void RefreshContentFromDatabaseAsync() + { + RefreshContentFromDatabase(); + } + + /// + /// Load content from database and replaces active content when done. + /// + public virtual void RefreshContentFromDatabase() { var e = new RefreshContentEventArgs(); FireBeforeRefreshContent(e); if (!e.Cancel) { - ThreadPool.QueueUserWorkItem( - delegate - { - XmlDocument xmlDoc = LoadContentFromDatabase(); - XmlContentInternal = xmlDoc; + XmlDocument xmlDoc = LoadContentFromDatabase(); + XmlContentInternal = xmlDoc; - // It is correct to manually call PersistXmlToFile here event though the setter of XmlContentInternal - // queues this up, because this delegate is executing on a different thread and may complete - // after the request which invoked it (which would normally persist the file on completion) - // So we are responsible for ensuring the content is persisted in this case. + // It is correct to manually call PersistXmlToFile here event though the setter of XmlContentInternal + // queues this up, because this delegate is executing on a different thread and may complete + // after the request which invoked it (which would normally persist the file on completion) + // So we are responsible for ensuring the content is persisted in this case. if (!UmbracoSettings.isXmlContentCacheDisabled && UmbracoSettings.continouslyUpdateXmlDiskCache) - PersistXmlToFile(xmlDoc); - }); - - FireAfterRefreshContent(e); + { + PersistXmlToFile(xmlDoc); + } } } @@ -551,33 +553,15 @@ namespace umbraco } } - /// - /// Updates the document cache async. - /// - /// The document id. [Obsolete("Method obsolete in version 4.1 and later, please use UpdateDocumentCache", true)] public virtual void UpdateDocumentCacheAsync(int documentId) { - //SD: WE've obsoleted this but then didn't make it call the method it should! So we've just - // left a bug behind...???? ARGH. - //.... changed now. - //ThreadPool.QueueUserWorkItem(delegate { UpdateDocumentCache(documentId); }); - UpdateDocumentCache(documentId); } - - /// - /// Clears the document cache async. - /// - /// The document id. + [Obsolete("Method obsolete in version 4.1 and later, please use ClearDocumentCache", true)] public virtual void ClearDocumentCacheAsync(int documentId) { - //SD: WE've obsoleted this but then didn't make it call the method it should! So we've just - // left a bug behind...???? ARGH. - //.... changed now. - //ThreadPool.QueueUserWorkItem(delegate { ClearDocumentCache(documentId); }); - ClearDocumentCache(documentId); } @@ -652,69 +636,6 @@ namespace umbraco ClearDocumentCache(documentId); } - ///// - ///// Uns the publish node async. - ///// - ///// The document id. - //[Obsolete("Please use: umbraco.content.ClearDocumentCacheAsync", true)] - //public virtual void UnPublishNodeAsync(int documentId) - //{ - - // ThreadPool.QueueUserWorkItem(delegate { ClearDocumentCache(documentId); }); - //} - - - ///// - ///// Legacy method - you should use the overloaded publishnode(document d) method whenever possible - ///// - ///// - //[Obsolete("Please use: umbraco.content.UpdateDocumentCache", true)] - //public virtual void PublishNode(int documentId) - //{ - - // // Get the document - // var d = new Document(documentId); - // PublishNode(d); - //} - - - ///// - ///// Publishes the node async. - ///// - ///// The document id. - //[Obsolete("Please use: umbraco.content.UpdateDocumentCacheAsync", true)] - //public virtual void PublishNodeAsync(int documentId) - //{ - - // UpdateDocumentCacheAsync(documentId); - //} - - - ///// - ///// Publishes the node. - ///// - ///// The documents. - //[Obsolete("Please use: umbraco.content.UpdateDocumentCache", true)] - //public virtual void PublishNode(List Documents) - //{ - - // UpdateDocumentCache(Documents); - //} - - - - ///// - ///// Publishes the node. - ///// - ///// The document. - //[Obsolete("Please use: umbraco.content.UpdateDocumentCache", true)] - //public virtual void PublishNode(Document d) - //{ - - // UpdateDocumentCache(d); - //} - - /// /// Occurs when [before document cache update]. /// @@ -932,9 +853,10 @@ namespace umbraco RemoveXmlFilePersistenceQueue(); } } - catch + catch (Exception ex) { // Nothing to catch here - we'll just persist + LogHelper.Error("An error occurred checking if xml file is queued for persistence", ex); } } } @@ -1036,22 +958,8 @@ namespace umbraco /// private XmlDocument LoadContentFromDatabase() { - // Alex N - 2010 06 - Very generic try-catch simply because at the moment, unfortunately, this method gets called inside a ThreadPool thread - // and we need to guarantee it won't tear down the app pool by throwing an unhandled exception try { - // Moved User to a local variable - why are we causing user 0 to load from the DB though? - // Alex N 20100212 - User staticUser = null; - try - { - staticUser = User.GetCurrent(); //User.GetUser(0); - } - catch - { - /* We don't care later if the staticUser is null */ - } - // Try to log to the DB LogHelper.Info("Loading content from database..."); @@ -1235,19 +1143,10 @@ order by umbracoNode.level, umbracoNode.sortOrder"; { if (xmlDoc != null) { - Trace.Write(string.Format("Saving content to disk on thread '{0}' (Threadpool? {1})", - Thread.CurrentThread.Name, Thread.CurrentThread.IsThreadPoolThread)); - - // Moved the user into a variable and avoided it throwing an error if one can't be loaded (e.g. empty / corrupt db on initial install) - User staticUser = null; - try - { - staticUser = User.GetCurrent(); - } - catch - { - } - + LogHelper.Debug("Saving content to disk on thread '{0}' (Threadpool? {1})", + () => Thread.CurrentThread.Name, + () => Thread.CurrentThread.IsThreadPoolThread); + try { Stopwatch stopWatch = Stopwatch.StartNew(); @@ -1263,23 +1162,20 @@ order by umbracoNode.level, umbracoNode.sortOrder"; } xmlDoc.Save(UmbracoXmlDiskCacheFileName); - - Trace.Write(string.Format("Saved content on thread '{0}' in {1} (Threadpool? {2})", - Thread.CurrentThread.Name, stopWatch.Elapsed, - Thread.CurrentThread.IsThreadPoolThread)); - - LogHelper.Debug(string.Format("Xml saved in {0}", stopWatch.Elapsed)); + + LogHelper.Debug("Saved content on thread '{0}' in {1} (Threadpool? {2})", + () => Thread.CurrentThread.Name, + () => stopWatch.Elapsed, + () => Thread.CurrentThread.IsThreadPoolThread); } catch (Exception ee) { // If for whatever reason something goes wrong here, invalidate disk cache DeleteXmlCache(); - - Trace.Write(string.Format( + + LogHelper.Error(string.Format( "Error saving content on thread '{0}' due to '{1}' (Threadpool? {2})", - Thread.CurrentThread.Name, ee.Message, Thread.CurrentThread.IsThreadPoolThread)); - - LogHelper.Error("Xml wasn't saved", ee); + Thread.CurrentThread.Name, ee.Message, Thread.CurrentThread.IsThreadPoolThread), ee); } } } @@ -1288,36 +1184,29 @@ order by umbracoNode.level, umbracoNode.sortOrder"; /// /// Marks a flag in the HttpContext so that, upon page execution completion, the Xml cache will /// get persisted to disk. Ensure this method is only called from a thread executing a page request - /// since umbraco.presentation.requestModule is the only monitor of this flag and is responsible + /// since UmbracoModule is the only monitor of this flag and is responsible /// for enacting the persistence at the PostRequestHandlerExecute stage of the page lifecycle. /// private void QueueXmlForPersistence() { - /* Alex Norcliffe 2010 06 03 - removing all launching of ThreadPool threads, instead we just - * flag on the context that the Xml should be saved and an event in the requestModule - * will check for this and call PersistXmlToFile() if necessary */ + //if this is called outside a web request we cannot queue it. + if (HttpContext.Current != null) { HttpContext.Current.Application.Lock(); - if (HttpContext.Current.Application[PersistenceFlagContextKey] != null) - HttpContext.Current.Application.Add(PersistenceFlagContextKey, null); - HttpContext.Current.Application[PersistenceFlagContextKey] = DateTime.UtcNow; - HttpContext.Current.Application.UnLock(); - } - else - { - //// Save copy of content - if (UmbracoSettings.CloneXmlCacheOnPublish) + try { - XmlDocument xmlContentCopy = CloneXmlDoc(_xmlContent); - - ThreadPool.QueueUserWorkItem( - delegate { PersistXmlToFile(xmlContentCopy); }); + if (HttpContext.Current.Application[PersistenceFlagContextKey] != null) + { + HttpContext.Current.Application.Add(PersistenceFlagContextKey, null); + } + HttpContext.Current.Application[PersistenceFlagContextKey] = DateTime.UtcNow; } - else - ThreadPool.QueueUserWorkItem( - delegate { PersistXmlToFile(); }); - } + finally + { + HttpContext.Current.Application.UnLock(); + } + } } internal DateTime GetCacheFileUpdateTime()