2016-08-25 15:09:51 +02:00
using System ;
using System.Collections.Generic ;
using System.Threading ;
2016-11-03 10:31:44 +01:00
using System.Web ;
2016-08-25 15:09:51 +02:00
using Umbraco.Core.Cache ;
using Umbraco.Core.Components ;
2017-05-30 15:46:25 +02:00
using Umbraco.Core.Composing ;
2018-07-20 09:49:05 +02:00
using Umbraco.Core.Composing.Composers ;
2017-12-28 09:27:57 +01:00
using Umbraco.Core.Configuration ;
2016-08-25 15:09:51 +02:00
using Umbraco.Core.Exceptions ;
using Umbraco.Core.IO ;
using Umbraco.Core.Logging ;
2017-12-26 11:35:21 +01:00
using Umbraco.Core.Migrations.Upgrade ;
2016-09-01 19:06:08 +02:00
using Umbraco.Core.Persistence ;
2017-12-28 09:06:33 +01:00
using Umbraco.Core.Persistence.Dtos ;
2016-10-17 15:16:07 +02:00
using Umbraco.Core.Persistence.Mappers ;
2016-09-01 19:06:08 +02:00
using Umbraco.Core.Persistence.SqlSyntax ;
2017-05-12 14:49:44 +02:00
using Umbraco.Core.Scoping ;
2018-10-17 15:09:59 +02:00
using Umbraco.Core.Services.Implement ;
2016-08-25 15:09:51 +02:00
2017-12-28 09:27:57 +01:00
namespace Umbraco.Core.Runtime
2016-08-25 15:09:51 +02:00
{
/// <summary>
/// Represents the Core Umbraco runtime.
/// </summary>
/// <remarks>Does not handle any of the web-related aspects of Umbraco (startup, etc). It
/// should be possible to use this runtime in console apps.</remarks>
public class CoreRuntime : IRuntime
{
2016-09-19 12:04:22 +02:00
private readonly UmbracoApplicationBase _app ;
2016-08-25 15:09:51 +02:00
private BootLoader _bootLoader ;
2016-09-01 19:06:08 +02:00
private RuntimeState _state ;
2016-08-25 15:09:51 +02:00
2016-08-31 16:48:57 +02:00
/// <summary>
/// Initializes a new instance of the <see cref="CoreRuntime"/> class.
/// </summary>
/// <param name="umbracoApplication">The Umbraco HttpApplication.</param>
public CoreRuntime ( UmbracoApplicationBase umbracoApplication )
{
2017-05-30 15:33:13 +02:00
_app = umbracoApplication ? ? throw new ArgumentNullException ( nameof ( umbracoApplication ) ) ;
2016-08-31 16:48:57 +02:00
}
/// <inheritdoc/>
2018-07-20 15:45:01 +02:00
public virtual void Boot ( IContainer container )
2016-08-25 15:09:51 +02:00
{
2016-09-08 18:43:58 +02:00
// some components may want to initialize with the UmbracoApplicationBase
2018-05-30 11:56:31 +02:00
// well, they should not - we should not do this
// TODO remove this eventually.
2016-09-19 12:04:22 +02:00
container . RegisterInstance ( _app ) ;
2016-09-08 18:43:58 +02:00
2016-09-01 19:06:08 +02:00
Compose ( container ) ;
2016-08-25 15:09:51 +02:00
2016-09-01 19:06:08 +02:00
// prepare essential stuff
2016-08-25 15:09:51 +02:00
2016-09-08 18:43:58 +02:00
var path = GetApplicationRootPath ( ) ;
if ( string . IsNullOrWhiteSpace ( path ) = = false )
IOHelper . SetRootDirectory ( path ) ;
2016-09-01 19:06:08 +02:00
_state = ( RuntimeState ) container . GetInstance < IRuntimeState > ( ) ;
_state . Level = RuntimeLevel . Boot ;
2016-08-25 15:09:51 +02:00
2016-09-01 19:06:08 +02:00
Logger = container . GetInstance < ILogger > ( ) ;
Profiler = container . GetInstance < IProfiler > ( ) ;
ProfilingLogger = container . GetInstance < ProfilingLogger > ( ) ;
2016-08-25 15:09:51 +02:00
// the boot loader boots using a container scope, so anything that is PerScope will
// be disposed after the boot loader has booted, and anything else will remain.
// note that this REQUIRES that perWebRequestScope has NOT been enabled yet, else
// the container will fail to create a scope since there is no http context when
// the application starts.
// the boot loader is kept in the runtime for as long as Umbraco runs, and components
// are NOT disposed - which is not a big deal as long as they remain lightweight
// objects.
2016-09-11 19:57:33 +02:00
using ( var bootTimer = ProfilingLogger . TraceDuration < CoreRuntime > (
$"Booting Umbraco {UmbracoVersion.SemanticVersion.ToSemanticString()} on {NetworkHelper.MachineName}." ,
"Booted." ,
"Boot failed." ) )
2016-09-01 19:06:08 +02:00
{
2016-11-03 10:31:44 +01:00
// throws if not full-trust
new AspNetHostingPermission ( AspNetHostingPermissionLevel . Unrestricted ) . Demand ( ) ;
2016-09-11 19:57:33 +02:00
try
{
2018-08-14 15:08:32 +01:00
Logger . Debug < CoreRuntime > ( "Runtime: {Runtime}" , GetType ( ) . FullName ) ;
2016-09-11 19:57:33 +02:00
AquireMainDom ( container ) ;
DetermineRuntimeLevel ( container ) ;
var componentTypes = ResolveComponentTypes ( ) ;
_bootLoader = new BootLoader ( container ) ;
_bootLoader . Boot ( componentTypes , _state . Level ) ;
}
catch ( Exception e )
{
_state . Level = RuntimeLevel . BootFailed ;
var bfe = e as BootFailedException ? ? new BootFailedException ( "Boot failed." , e ) ;
2017-05-31 15:25:07 +02:00
_state . BootFailedException = bfe ;
2016-09-11 19:57:33 +02:00
bootTimer . Fail ( exception : bfe ) ; // be sure to log the exception - even if we repeat ourselves
// throwing here can cause w3wp to hard-crash and we want to avoid it.
// instead, we're logging the exception and setting level to BootFailed.
// various parts of Umbraco such as UmbracoModule and UmbracoDefaultOwinStartup
// understand this and will nullify themselves, while UmbracoModule will
// throw a BootFailedException for every requests.
}
}
2016-12-14 14:06:30 +01:00
2016-12-14 19:21:50 +01:00
//fixme
2016-12-14 14:06:30 +01:00
// after Umbraco has started there is a scope in "context" and that context is
// going to stay there and never get destroyed nor reused, so we have to ensure that
// everything is cleared
2016-12-14 19:21:50 +01:00
//var sa = container.GetInstance<IDatabaseScopeAccessor>();
//sa.Scope?.Dispose();
2016-09-11 19:57:33 +02:00
}
2016-09-01 19:06:08 +02:00
2018-07-20 15:45:01 +02:00
private void AquireMainDom ( IContainer container )
2016-09-11 19:57:33 +02:00
{
using ( var timer = ProfilingLogger . DebugDuration < CoreRuntime > ( "Acquiring MainDom." , "Aquired." ) )
{
try
2016-09-01 19:06:08 +02:00
{
var mainDom = container . GetInstance < MainDom > ( ) ;
mainDom . Acquire ( ) ;
}
2016-09-11 19:57:33 +02:00
catch
{
timer . Fail ( ) ;
throw ;
}
}
}
2016-09-01 19:06:08 +02:00
2016-10-13 21:08:07 +02:00
// internal for tests
2018-07-20 15:45:01 +02:00
internal void DetermineRuntimeLevel ( IContainer container )
2016-09-11 19:57:33 +02:00
{
using ( var timer = ProfilingLogger . DebugDuration < CoreRuntime > ( "Determining runtime level." , "Determined." ) )
{
try
2016-09-01 19:06:08 +02:00
{
2016-12-16 14:18:37 +01:00
var dbfactory = container . GetInstance < IUmbracoDatabaseFactory > ( ) ;
2018-03-29 11:31:33 +02:00
SetRuntimeStateLevel ( dbfactory , Logger ) ;
2018-03-30 10:16:21 +02:00
2018-08-14 15:08:32 +01:00
Logger . Debug < CoreRuntime > ( "Runtime level: {RuntimeLevel}" , _state . Level ) ;
2018-03-30 10:16:21 +02:00
if ( _state . Level = = RuntimeLevel . Upgrade )
{
2018-08-14 15:08:32 +01:00
Logger . Debug < CoreRuntime > ( "Configure database factory for upgrades." ) ;
2018-03-30 10:16:21 +02:00
dbfactory . ConfigureForUpgrade ( ) ;
}
2016-09-01 19:06:08 +02:00
}
2016-09-11 19:57:33 +02:00
catch
2016-09-01 19:06:08 +02:00
{
2016-09-11 19:57:33 +02:00
timer . Fail ( ) ;
throw ;
2016-09-01 19:06:08 +02:00
}
2016-09-11 19:57:33 +02:00
}
}
2016-09-01 19:06:08 +02:00
2016-09-11 19:57:33 +02:00
private IEnumerable < Type > ResolveComponentTypes ( )
{
using ( var timer = ProfilingLogger . TraceDuration < CoreRuntime > ( "Resolving component types." , "Resolved." ) )
{
try
{
return GetComponentTypes ( ) ;
}
catch
{
timer . Fail ( ) ;
throw ;
}
2016-09-01 19:06:08 +02:00
}
2016-08-25 15:09:51 +02:00
}
2016-09-01 19:06:08 +02:00
/// <inheritdoc/>
2016-08-25 15:09:51 +02:00
public virtual void Terminate ( )
{
2016-09-08 18:43:58 +02:00
using ( ProfilingLogger . DebugDuration < CoreRuntime > ( "Terminating Umbraco." , "Terminated." ) )
{
_bootLoader ? . Terminate ( ) ;
}
2016-08-25 15:09:51 +02:00
}
2017-12-26 11:35:21 +01:00
/// <summary>
/// Composes the runtime.
/// </summary>
2018-07-20 15:45:01 +02:00
public virtual void Compose ( IContainer container )
2016-08-25 15:09:51 +02:00
{
2016-11-29 10:31:25 +01:00
// compose the very essential things that are needed to bootstrap, before anything else,
// and only these things - the rest should be composed in runtime components
// register basic things
2016-09-01 19:06:08 +02:00
container . RegisterSingleton < IProfiler , LogProfiler > ( ) ;
container . RegisterSingleton < ProfilingLogger > ( ) ;
container . RegisterSingleton < IRuntimeState , RuntimeState > ( ) ;
2018-07-20 09:49:05 +02:00
container . ComposeConfiguration ( ) ;
2018-04-06 13:51:54 +10:00
2016-11-29 10:31:25 +01:00
// register caches
// need the deep clone runtime cache profiver to ensure entities are cached properly, ie
// are cloned in and cloned out - no request-based cache here since no web-based context,
// will be overriden later or
2016-09-01 19:06:08 +02:00
container . RegisterSingleton ( _ = > new CacheHelper (
new DeepCloneRuntimeCacheProvider ( new ObjectCacheRuntimeCacheProvider ( ) ) ,
new StaticCacheProvider ( ) ,
2017-12-15 16:29:14 +01:00
NullCacheProvider . Instance ,
2016-11-29 10:31:25 +01:00
new IsolatedRuntimeCache ( type = > new DeepCloneRuntimeCacheProvider ( new ObjectCacheRuntimeCacheProvider ( ) ) ) ) ) ;
container . RegisterSingleton ( f = > f . GetInstance < CacheHelper > ( ) . RuntimeCache ) ;
2016-08-25 15:09:51 +02:00
2016-11-29 10:31:25 +01:00
// register the plugin manager
2018-04-06 13:51:54 +10:00
container . RegisterSingleton ( f = > new TypeLoader ( f . GetInstance < IRuntimeCacheProvider > ( ) , f . GetInstance < IGlobalSettings > ( ) , f . GetInstance < ProfilingLogger > ( ) ) ) ;
2016-08-25 15:09:51 +02:00
2016-11-29 10:31:25 +01:00
// register syntax providers - required by database factory
2016-09-01 19:06:08 +02:00
container . Register < ISqlSyntaxProvider , MySqlSyntaxProvider > ( "MySqlSyntaxProvider" ) ;
container . Register < ISqlSyntaxProvider , SqlCeSyntaxProvider > ( "SqlCeSyntaxProvider" ) ;
container . Register < ISqlSyntaxProvider , SqlServerSyntaxProvider > ( "SqlServerSyntaxProvider" ) ;
2016-10-17 15:16:07 +02:00
// register persistence mappers - required by database factory so needs to be done here
2016-11-30 18:30:34 +01:00
// means the only place the collection can be modified is in a runtime - afterwards it
2016-10-17 15:16:07 +02:00
// has been frozen and it is too late
2016-10-19 13:26:30 +02:00
var mapperCollectionBuilder = container . RegisterCollectionBuilder < MapperCollectionBuilder > ( ) ;
ComposeMapperCollection ( mapperCollectionBuilder ) ;
2016-09-01 19:06:08 +02:00
2016-11-29 10:31:25 +01:00
// register database factory - required to check for migrations
2016-09-01 19:06:08 +02:00
// will be initialized with syntax providers and a logger, and will try to configure
// from the default connection string name, if possible, else will remain non-configured
2017-05-12 14:49:44 +02:00
// until properly configured (eg when installing)
2016-12-16 14:18:37 +01:00
container . RegisterSingleton < IUmbracoDatabaseFactory , UmbracoDatabaseFactory > ( ) ;
2017-09-22 18:28:21 +02:00
container . RegisterSingleton ( f = > f . GetInstance < IUmbracoDatabaseFactory > ( ) . SqlContext ) ;
2016-09-01 19:06:08 +02:00
2017-05-12 14:49:44 +02:00
// register the scope provider
2017-12-18 17:13:29 +01:00
container . RegisterSingleton < ScopeProvider > ( ) ; // implements both IScopeProvider and IScopeAccessor
2017-12-14 17:04:44 +01:00
container . RegisterSingleton < IScopeProvider > ( f = > f . GetInstance < ScopeProvider > ( ) ) ;
container . RegisterSingleton < IScopeAccessor > ( f = > f . GetInstance < ScopeProvider > ( ) ) ;
2016-09-01 19:06:08 +02:00
// register MainDom
container . RegisterSingleton < MainDom > ( ) ;
2016-08-25 15:09:51 +02:00
}
2016-10-19 13:26:30 +02:00
protected virtual void ComposeMapperCollection ( MapperCollectionBuilder builder )
{
2016-11-30 18:30:34 +01:00
builder . AddCore ( ) ;
2016-10-19 13:26:30 +02:00
}
2018-03-29 11:31:33 +02:00
private void SetRuntimeStateLevel ( IUmbracoDatabaseFactory databaseFactory , ILogger logger )
2016-09-01 19:06:08 +02:00
{
2018-03-27 10:04:07 +02:00
var localVersion = UmbracoVersion . Local ; // the local, files, version
2018-03-29 11:31:33 +02:00
var codeVersion = _state . SemanticVersion ; // the executing code version
2016-09-01 19:06:08 +02:00
var connect = false ;
2016-09-08 18:43:58 +02:00
// we don't know yet
2018-03-29 11:31:33 +02:00
_state . Level = RuntimeLevel . Unknown ;
2016-09-08 18:43:58 +02:00
2018-03-27 10:04:07 +02:00
if ( localVersion = = null )
2016-09-01 19:06:08 +02:00
{
// there is no local version, we are not installed
logger . Debug < CoreRuntime > ( "No local version, need to install Umbraco." ) ;
2018-03-29 11:31:33 +02:00
_state . Level = RuntimeLevel . Install ;
2016-09-01 19:06:08 +02:00
}
2018-06-12 11:03:18 +02:00
else if ( localVersion < codeVersion )
2016-09-01 19:06:08 +02:00
{
// there *is* a local version, but it does not match the code version
// need to upgrade
2018-08-14 15:08:32 +01:00
logger . Debug < CoreRuntime > ( "Local version '{LocalVersion}' < code version '{CodeVersion}', need to upgrade Umbraco." , localVersion , codeVersion ) ;
2018-03-29 11:31:33 +02:00
_state . Level = RuntimeLevel . Upgrade ;
2016-09-01 19:06:08 +02:00
}
2018-06-12 11:03:18 +02:00
else if ( localVersion > codeVersion )
{
2018-08-14 15:08:32 +01:00
logger . Warn < CoreRuntime > ( "Local version '{LocalVersion}' > code version '{CodeVersion}', downgrading is not supported." , localVersion , codeVersion ) ;
2018-06-12 11:03:18 +02:00
_state . Level = RuntimeLevel . BootFailed ;
// in fact, this is bad enough that we want to throw
throw new BootFailedException ( $"Local version \" { localVersion } \ " > code version \"{codeVersion}\", downgrading is not supported." ) ;
}
2016-09-01 19:06:08 +02:00
else if ( databaseFactory . Configured = = false )
{
// local version *does* match code version, but the database is not configured
// install (again? this is a weird situation...)
logger . Debug < CoreRuntime > ( "Database is not configured, need to install Umbraco." ) ;
2018-03-29 11:31:33 +02:00
_state . Level = RuntimeLevel . Install ;
2016-09-01 19:06:08 +02:00
}
2016-09-08 18:43:58 +02:00
// install? not going to test anything else
2018-03-29 11:31:33 +02:00
if ( _state . Level = = RuntimeLevel . Install )
2016-09-08 18:43:58 +02:00
return ;
// else, keep going,
2016-09-01 19:06:08 +02:00
// anything other than install wants a database - see if we can connect
2017-09-15 18:22:19 +02:00
// (since this is an already existing database, assume localdb is ready)
2016-09-08 18:43:58 +02:00
for ( var i = 0 ; i < 5 ; i + + )
2016-09-01 19:06:08 +02:00
{
2016-09-08 18:43:58 +02:00
connect = databaseFactory . CanConnect ;
if ( connect ) break ;
2018-08-14 15:08:32 +01:00
logger . Debug < CoreRuntime > ( "Could not immediately connect to database, trying again." ) ;
2016-09-08 18:43:58 +02:00
Thread . Sleep ( 1000 ) ;
}
2016-09-01 19:06:08 +02:00
2016-09-08 18:43:58 +02:00
if ( connect = = false )
{
// cannot connect to configured database, this is bad, fail
2018-08-14 15:08:32 +01:00
logger . Debug < CoreRuntime > ( "Could not connect to database." ) ;
2018-03-29 11:31:33 +02:00
_state . Level = RuntimeLevel . BootFailed ;
2016-09-01 19:06:08 +02:00
2016-09-08 18:43:58 +02:00
// in fact, this is bad enough that we want to throw
throw new BootFailedException ( "A connection string is configured but Umbraco could not connect to the database." ) ;
2016-09-01 19:06:08 +02:00
}
2018-10-22 10:34:04 +02:00
// if we already know we want to upgrade,
// still run EnsureUmbracoUpgradeState to get the states
// (v7 will just get a null state, that's ok)
2016-09-01 19:06:08 +02:00
// else
// look for a matching migration entry - bypassing services entirely - they are not 'up' yet
// fixme - in a LB scenario, ensure that the DB gets upgraded only once!
2018-10-17 15:09:59 +02:00
bool noUpgrade ;
2016-09-01 19:06:08 +02:00
try
{
2018-10-17 15:09:59 +02:00
noUpgrade = EnsureUmbracoUpgradeState ( databaseFactory , logger ) ;
2016-09-01 19:06:08 +02:00
}
2018-03-29 11:31:33 +02:00
catch ( Exception e )
2016-09-01 19:06:08 +02:00
{
2018-10-17 15:09:59 +02:00
// can connect to the database but cannot check the upgrade state... oops
2018-03-29 11:31:33 +02:00
logger . Warn < CoreRuntime > ( e , "Could not check the upgrade state." ) ;
2018-10-17 15:09:59 +02:00
throw new BootFailedException ( "Could not check the upgrade state." , e ) ;
2016-09-01 19:06:08 +02:00
}
2018-10-17 15:09:59 +02:00
if ( noUpgrade )
2016-09-01 19:06:08 +02:00
{
// the database version matches the code & files version, all clear, can run
2018-03-29 11:31:33 +02:00
_state . Level = RuntimeLevel . Run ;
2016-09-01 19:06:08 +02:00
return ;
}
// the db version does not match... but we do have a migration table
// so, at least one valid table, so we quite probably are installed & need to upgrade
// although the files version matches the code version, the database version does not
// which means the local files have been upgraded but not the database - need to upgrade
2017-12-26 11:35:21 +01:00
logger . Debug < CoreRuntime > ( "Has not reached the final upgrade step, need to upgrade Umbraco." ) ;
2018-03-29 11:31:33 +02:00
_state . Level = RuntimeLevel . Upgrade ;
2016-09-01 19:06:08 +02:00
}
2017-12-26 11:35:21 +01:00
protected virtual bool EnsureUmbracoUpgradeState ( IUmbracoDatabaseFactory databaseFactory , ILogger logger )
2016-09-08 18:43:58 +02:00
{
2018-03-21 11:32:07 +01:00
var umbracoPlan = new UmbracoPlan ( ) ;
var stateValueKey = Upgrader . GetStateValueKey ( umbracoPlan ) ;
2018-10-17 15:09:59 +02:00
// no scope, no service - just directly accessing the database
2017-12-26 11:35:21 +01:00
using ( var database = databaseFactory . CreateDatabase ( ) )
2016-12-14 19:21:50 +01:00
{
2018-10-17 15:09:59 +02:00
_state . CurrentMigrationState = KeyValueService . GetValue ( database , stateValueKey ) ;
_state . FinalMigrationState = umbracoPlan . FinalState ;
2016-12-14 19:21:50 +01:00
}
2017-12-26 11:35:21 +01:00
2018-10-17 15:09:59 +02:00
logger . Debug < CoreRuntime > ( "Final upgrade state is {FinalMigrationState}, database contains {DatabaseState}" , _state . FinalMigrationState , _state . CurrentMigrationState ? ? "<null>" ) ;
2017-12-26 11:35:21 +01:00
2018-10-17 15:09:59 +02:00
return _state . CurrentMigrationState = = _state . FinalMigrationState ;
2016-09-08 18:43:58 +02:00
}
2016-08-25 15:09:51 +02:00
#region Locals
protected ILogger Logger { get ; private set ; }
protected IProfiler Profiler { get ; private set ; }
protected ProfilingLogger ProfilingLogger { get ; private set ; }
#endregion
#region Getters
2016-08-31 16:48:57 +02:00
// getters can be implemented by runtimes inheriting from CoreRuntime
2017-05-30 15:33:13 +02:00
// fixme - inject! no Current!
protected virtual IEnumerable < Type > GetComponentTypes ( ) = > Current . TypeLoader . GetTypes < IUmbracoComponent > ( ) ;
2016-08-25 15:09:51 +02:00
2016-09-08 18:43:58 +02:00
// by default, returns null, meaning that Umbraco should auto-detect the application root path.
// override and return the absolute path to the Umbraco site/solution, if needed
protected virtual string GetApplicationRootPath ( ) = > null ;
2016-08-25 15:09:51 +02:00
#endregion
}
}