Implementing retry logic in command host

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-07-22 11:13:55 -07:00
parent 261d42d4ee
commit 23dd223c87
2 changed files with 35 additions and 7 deletions

View File

@@ -42,14 +42,19 @@ namespace Orchard.Commands {
}
public int StartHost(TextReader input, TextWriter output) {
try {
_hostContainer = OrchardStarter.CreateHostContainer(ContainerRegistrations);
_tenants = new Dictionary<string, IStandaloneEnvironment>();
var host = _hostContainer.Resolve<IOrchardHost>();
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<string, IStandaloneEnvironment>();
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<IOrchardHost>();
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);
}
}

View File

@@ -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."));
}
}
}