From 23dd223c87a0157ffbc8efc1c6786b4c47574292 Mon Sep 17 00:00:00 2001 From: Renaud Paquay Date: Thu, 22 Jul 2010 11:13:55 -0700 Subject: [PATCH] Implementing retry logic in command host --HG-- branch : dev --- src/Orchard/Commands/CommandHostAgent.cs | 25 ++++++++++++++----- .../Commands/CommandHostEnvironment.cs | 17 ++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Orchard/Commands/CommandHostAgent.cs b/src/Orchard/Commands/CommandHostAgent.cs index 9f5f855e3..08ed67eaa 100644 --- a/src/Orchard/Commands/CommandHostAgent.cs +++ b/src/Orchard/Commands/CommandHostAgent.cs @@ -42,14 +42,19 @@ namespace Orchard.Commands { } public int StartHost(TextReader input, TextWriter output) { - try { - _hostContainer = OrchardStarter.CreateHostContainer(ContainerRegistrations); - _tenants = new Dictionary(); - var host = _hostContainer.Resolve(); - host.Initialize(); + // This retry logic is not exactly clean, but a change in configuration + // sometimes need us to re-try initializing a host container. + Retry: + try { + _hostContainer = CreateHostContainer(); + _tenants = new Dictionary(); return 0; } + catch (OrchardCommandHostRetryException e) { + output.WriteLine("{0} (Retrying...)", e.Message); + goto Retry; + } catch (Exception e) { for (; e != null; e = e.InnerException) { output.WriteLine("Error: {0}", e.Message); @@ -59,6 +64,14 @@ namespace Orchard.Commands { } } + private IContainer CreateHostContainer() { + var hostContainer = OrchardStarter.CreateHostContainer(ContainerRegistrations); + + var host = hostContainer.Resolve(); + host.Initialize(); + return hostContainer; + } + public int StopHost(TextReader input, TextWriter output) { try { @@ -133,7 +146,7 @@ namespace Orchard.Commands { } else { // In case of an unitiliazed site (no default settings yet), we create a default settings instance. - var settings = new ShellSettings {Name = "Default", State = new TenantState("Uninitialized")}; + var settings = new ShellSettings { Name = "Default", State = new TenantState("Uninitialized") }; return host.CreateStandaloneEnvironment(settings); } } diff --git a/src/Orchard/Commands/CommandHostEnvironment.cs b/src/Orchard/Commands/CommandHostEnvironment.cs index 301ccb4b8..8b42245e7 100644 --- a/src/Orchard/Commands/CommandHostEnvironment.cs +++ b/src/Orchard/Commands/CommandHostEnvironment.cs @@ -1,10 +1,25 @@ using System; using System.Linq; +using System.Runtime.Serialization; using System.Web.Hosting; using Orchard.Environment; using Orchard.Localization; namespace Orchard.Commands { + public class OrchardCommandHostRetryException : OrchardCoreException { + public OrchardCommandHostRetryException(LocalizedString message) + : base(message) { + } + + public OrchardCommandHostRetryException(LocalizedString message, Exception innerException) + : base(message, innerException) { + } + + protected OrchardCommandHostRetryException(SerializationInfo info, StreamingContext context) + : base(info, context) { + } + } + public class CommandHostEnvironment : IHostEnvironment { public CommandHostEnvironment() { T = NullLocalizer.Instance; @@ -29,7 +44,7 @@ namespace Orchard.Commands { } public void ResetSiteCompilation() { - throw new OrchardCoreException(T("A change of configuration requires the application to be restarted. Running the command again usually solves this problem.")); + throw new OrchardCommandHostRetryException(T("A change of configuration requires the host to be restarted.")); } } } \ No newline at end of file