Skip to content

Commit

Permalink
Disable pure live models, enable live app_data models
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan committed Jun 4, 2015
1 parent d3c01c6 commit 0c28449
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
52 changes: 42 additions & 10 deletions Zbu.ModelsBuilder.AspNet/LiveModelsProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@
using Zbu.ModelsBuilder.AspNet;
using Zbu.ModelsBuilder.Configuration;

// note
// we do not support pure LiveModels anymore - see notes in various places
// this should provide live AppData, AppCode or Dll models - but we do not want to
// restart the app anytime something changes, ?
//
// ideally we'd want something that does not generate models all the time, but
// only after a while, and then restarts, but that is prone to too much confusion,
// so we decide that ONLY live App_Data models are supported from now on.

// will install only if configuration says it needs to be installed
[assembly: PreApplicationStartMethod(typeof(LiveModelsProviderModule), "Install")]

namespace Zbu.ModelsBuilder.AspNet
Expand All @@ -17,12 +27,30 @@ public class LiveModelsProvider : ApplicationEventHandler
private static Mutex _mutex;
private static int _req;

internal static bool IsEnabled
{
get
{
if (!Config.EnableLiveModels)
return false;

// not supported anymore
//if (Config.EnableAppCodeModels || Config.EnableDllModels)
// return true;

if (Config.EnableAppDataModels)
return true;

// pure live is not supported anymore
return false;
}
}

protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarted(umbracoApplication, applicationContext);

// if no live, or pure live, do nothing
if (!Config.EnableLiveModels || (!Config.EnableAppCodeModels && !Config.EnableDllModels))
if (!IsEnabled)
return;

// initialize mutex
Expand All @@ -37,7 +65,7 @@ protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplica

// at the end of a request since we're restarting the pool
// NOTE - this does NOT trigger - see module below
umbracoApplication.EndRequest += GenerateModelsIfRequested;
//umbracoApplication.EndRequest += GenerateModelsIfRequested;
}

// NOTE
Expand All @@ -51,7 +79,7 @@ protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplica
private static void RequestModelsGeneration(object sender, EventArgs args)
{
//HttpContext.Current.Items[this] = true;
LogHelper.Debug<LiveModelsProvider>("Request to generate models.");
LogHelper.Debug<LiveModelsProvider>("Requested to generate models.");
Interlocked.Exchange(ref _req, 1);
}

Expand All @@ -62,14 +90,19 @@ public static void GenerateModelsIfRequested(object sender, EventArgs args)

// cannot use a simple lock here because we don't want another AppDomain
// to generate while we do... and there could be 2 AppDomains if the app restarts.
// or... am I being paranoid?

try
{
LogHelper.Debug<LiveModelsProvider>("Requested to generate models.");
_mutex.WaitOne(); // wait until it is safe, and acquire
LogHelper.Info<LiveModelsProvider>("Generate models.");
LogHelper.Debug<LiveModelsProvider>("Generate models...");
const int timeout = 2*60*1000; // 2 mins
_mutex.WaitOne(timeout); // wait until it is safe, and acquire
LogHelper.Info<LiveModelsProvider>("Generate models now.");
GenerateModels();
LogHelper.Info<LiveModelsProvider>("Generated.");
}
catch (TimeoutException)
{
LogHelper.Warn<LiveModelsProvider>("Timeout, models were NOT generated.");
}
catch (Exception e)
{
Expand Down Expand Up @@ -120,8 +153,7 @@ public void Dispose()

public static void Install()
{
// if no live, or pure live, do nothing
if (!Config.EnableLiveModels || (!Config.EnableAppCodeModels && !Config.EnableDllModels))
if (!LiveModelsProvider.IsEnabled)
return;

HttpApplication.RegisterModule(typeof(LiveModelsProviderModule));
Expand Down
25 changes: 12 additions & 13 deletions Zbu.ModelsBuilder/Configuration/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,6 @@ static Config()

if (count > 1)
throw new Exception("Configuration error: you can enable only one of Dll, AppCode or AppData models at a time.");

count = (EnableAppDataModels ? 1 : 0) + (EnableLiveModels ? 1 : 0);

if (count > 1)
throw new Exception("Configuration error: you can enable both AppData and Live models.");
}

/// <summary>
Expand All @@ -61,6 +56,8 @@ static Config()
/// <remarks>
/// <para>Indicates whether a dll containing the models should be generated in ~/bin by compiling
/// the models created in App_Data.</para>
/// <para>When "Dll models" is enabled, the dashboard shows the "generate" button so that
/// models can be generated in App_Data/Models and then compiled in a dll.</para>
/// <para>Default value is <c>false</c> because once enabled, Umbraco will restart anytime models
/// are re-generated from the dashboard. This is probably what you want to do, but we're forcing
/// you to make a concious decision at the moment.</para>
Expand Down Expand Up @@ -88,21 +85,21 @@ static Config()
/// <remarks>
/// <para>Default value is <c>false</c>.</para>
/// <para>When "App_Data models" is enabled, the dashboard shows the "generate" button so that
/// models can be generated in App_Data/Models. Whether they will be just sitting there or loaded
/// and compiled depends on EnableAppCoreModels.</para>
/// models can be generated in App_Data/Models. Nothing else happens so the site does not restart.</para>
/// </remarks>
public static bool EnableAppDataModels { get; private set; }

/// <summary>
/// Gets a value indicating whether "live models" are enabled.
/// </summary>
/// <remarks>
/// <para>When neither Dll nor App_Code models are enabled, indicates whether models
/// <para>When neither Dll, App_Data nor App_Code models are enabled, indicates whether models
/// should be automatically generated (in-memory), compiled and loaded into an assembly
/// referenced by our custom Razor engine, so they are available to views and are updated
/// when content types change, without Umbraco restarting.</para>
/// <para>When either Dll or App_Code models are enabled, indicates whether models
/// should be automatically generated anytime a content type changes.</para>
/// <para>When either Dll, App_Data or App_Code models are enabled, indicates whether models
/// should be automatically generated anytime a content type changes, see EnablePureLiveModels
/// below.</para>
/// <para>Default value is <c>false</c>.</para>
/// </remarks>
public static bool EnableLiveModels { get; private set; }
Expand All @@ -111,12 +108,14 @@ static Config()
/// Gets a value indicating whether only "live models" are enabled.
/// </summary>
/// <remarks>
/// <para>When true neither Dll nor App_Code models are enabled and we want our
/// custom Razor engine do handle models.</para>
/// <para>When true neither Dll, App_Data nor App_Code models are enabled and we want our
/// custom Razor engine do handle models - NOTE: pure live models are disabled because
/// they sort of don't work.</para>
/// </remarks>
public static bool EnablePureLiveModels
{
get { return EnableLiveModels && !(EnableAppCodeModels || EnableDllModels); }
get { return false; }
//get { return EnableLiveModels && !(EnableAppCodeModels || EnableDllModels || EnableAppDataModels); }
}

/// <summary>
Expand Down

0 comments on commit 0c28449

Please sign in to comment.