2015-03-04 12:16:28 +01:00
|
|
|
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)
|
2015-03-05 10:45:43 +01:00
|
|
|
{
|
|
|
|
|
if (ensure)
|
|
|
|
|
throw new NotSupportedException("Cannot execute without a valid/current UmbracoContext with an HttpContext assigned.");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2015-03-04 12:16:28 +01:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|