Files
Umbraco-CMS/src/Umbraco.Web/BatchedDatabaseServerMessenger.cs

78 lines
3.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Web;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Core.Sync;
using Umbraco.Web.Routing;
namespace Umbraco.Web
{
public class BatchedDatabaseServerMessenger : Core.Sync.BatchedDatabaseServerMessenger
{
public BatchedDatabaseServerMessenger(ApplicationContext appContext, bool enableDistCalls, DatabaseServerMessengerOptions options)
: base(appContext, enableDistCalls, options, GetBatch)
{
UmbracoApplicationBase.ApplicationStarted += Application_Started;
UmbracoModule.EndRequest += UmbracoModule_EndRequest;
UmbracoModule.RouteAttempt += UmbracoModule_RouteAttempt;
}
private void Application_Started(object sender, EventArgs eventArgs)
{
if (ApplicationContext.IsConfigured == false
|| ApplicationContext.DatabaseContext.IsDatabaseConfigured == false
|| ApplicationContext.DatabaseContext.CanConnect == false)
LogHelper.Warn<BatchedDatabaseServerMessenger>("The app is not configured or cannot connect to the database, this server cannot be initialized with "
+ typeof(BatchedDatabaseServerMessenger) + ", distributed calls will not be enabled for this server");
// because .ApplicationStarted triggers only once, this is thread-safe
Boot();
}
private void UmbracoModule_RouteAttempt(object sender, RoutableAttemptEventArgs e)
{
switch (e.Outcome)
{
case EnsureRoutableOutcome.IsRoutable:
Sync();
break;
case EnsureRoutableOutcome.NotDocumentRequest:
//so it's not a document request, we'll check if it's a back office request
if (e.HttpContext.Request.Url.IsBackOfficeRequest(HttpRuntime.AppDomainAppVirtualPath))
{
//it's a back office request, we should sync!
Sync();
}
break;
//case EnsureRoutableOutcome.NotReady:
//case EnsureRoutableOutcome.NotConfigured:
//case EnsureRoutableOutcome.NoContent:
//default:
// break;
}
}
private void UmbracoModule_EndRequest(object sender, EventArgs e)
{
// will clear the batch - will remain in HttpContext though - that's ok
FlushBatch();
}
private static ICollection<RefreshInstructionEnvelope> GetBatch(bool ensure)
{
var httpContext = UmbracoContext.Current == null ? null : UmbracoContext.Current.HttpContext;
if (httpContext == null)
throw new NotSupportedException("Cannot execute without a valid/current UmbracoContext with an HttpContext assigned.");
var key = typeof (BatchedDatabaseServerMessenger).Name;
// no thread-safety here because it'll run in only 1 thread (request) at a time
var batch = (ICollection<RefreshInstructionEnvelope>)httpContext.Items[key];
if (batch == null && ensure)
httpContext.Items[key] = batch = new List<RefreshInstructionEnvelope>();
return batch;
}
}
}