using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using umbraco.interfaces;
using Umbraco.Core;
using Umbraco.Core.Models.Rdbms;
using Umbraco.Core.Sync;
using Umbraco.Web.Routing;
using Umbraco.Core.Logging;
namespace Umbraco.Web
{
///
/// An implementation that works by storing messages in the database.
///
///
/// This binds to appropriate umbraco events in order to trigger the Boot(), Sync() & FlushBatch() calls
///
public class BatchedDatabaseServerMessenger : DatabaseServerMessenger
{
public BatchedDatabaseServerMessenger(ApplicationContext appContext, bool enableDistCalls, DatabaseServerMessengerOptions options)
: base(appContext, enableDistCalls, options)
{ }
// invoked by BatchedDatabaseServerMessengerStartup which is an ApplicationEventHandler
// with default "ShouldExecute", so that method will run if app IsConfigured and database
// context IsDatabaseConfigured - we still want to check CanConnect though to be safe
internal void Startup()
{
UmbracoModule.EndRequest += UmbracoModule_EndRequest;
UmbracoModule.RouteAttempt += UmbracoModule_RouteAttempt;
if (ApplicationContext.DatabaseContext.CanConnect == false)
{
ApplicationContext.ProfilingLogger.Logger.Warn(
"Cannot connect to the database, distributed calls will not be enabled for this server.");
}
else
{
Boot();
}
}
private void UmbracoModule_RouteAttempt(object sender, RoutableAttemptEventArgs e)
{
// as long as umbraco is ready & configured, sync
switch (e.Outcome)
{
case EnsureRoutableOutcome.IsRoutable:
case EnsureRoutableOutcome.NotDocumentRequest:
case EnsureRoutableOutcome.NoContent:
Sync();
break;
//case EnsureRoutableOutcome.NotReady:
//case EnsureRoutableOutcome.NotConfigured:
//default:
// break;
}
}
private void UmbracoModule_EndRequest(object sender, EventArgs e)
{
// will clear the batch - will remain in HttpContext though - that's ok
FlushBatch();
}
protected override void DeliverRemote(IEnumerable servers, ICacheRefresher refresher, MessageType messageType, IEnumerable