Implement retry logic when host needs to be restarted

--HG--
branch : dev
This commit is contained in:
Renaud Paquay
2010-07-24 14:37:20 -07:00
parent 9907d0c997
commit 388ea9e553
4 changed files with 36 additions and 24 deletions

View File

@@ -28,9 +28,9 @@ namespace Orchard.Host {
HostingEnvironment.UnregisterObject(this);
}
public void StartSession(TextReader input, TextWriter output) {
public int StartSession(TextReader input, TextWriter output) {
_agent = CreateAgent();
StartHost(_agent, input, output);
return StartHost(_agent, input, output);
}
public void StopSession(TextReader input, TextWriter output) {

View File

@@ -17,8 +17,8 @@ namespace OrchardCLI {
}
public int Run() {
var context = _commandHostContextProvider.CreateContext();
var context = CommandHostContext();
Console.WriteLine("Type \"help commands\" for help, \"exit\" to exit");
while (true) {
var command = ReadCommand(context);
switch (command.ToLowerInvariant()) {
@@ -41,12 +41,24 @@ namespace OrchardCLI {
return Console.ReadLine();
}
private CommandHostContext CommandHostContext() {
Console.WriteLine("Initializing Orchard session... (This might take a few seconds)");
var result = _commandHostContextProvider.CreateContext();
if (result.StartSessionResult == 240/*special return code for "Retry"*/) {
result = _commandHostContextProvider.CreateContext();
}
return result;
}
private CommandHostContext RunCommand(CommandHostContext context, string command) {
if (string.IsNullOrWhiteSpace(command))
return context;
int result = RunCommandInSession(context, command);
if(result == 240) {
if (result == 240/*special return code for "Retry"*/) {
_commandHostContextProvider.Shutdown(context);
context = _commandHostContextProvider.CreateContext();
context = CommandHostContext();
result = RunCommandInSession(context, command);
if (result != 0)
Console.WriteLine("Command returned non-zero result: {0}", result);

View File

@@ -6,6 +6,7 @@ using Orchard.Host;
namespace OrchardCLI {
public class CommandHostContext {
public int StartSessionResult { get; set; }
public OrchardParameters Arguments { get; set; }
public DirectoryInfo OrchardDirectory { get; set; }
public ApplicationManager AppManager { get; set; }

View File

@@ -9,7 +9,7 @@ using Orchard.Host;
using Orchard.Parameters;
namespace OrchardCLI {
public class CommandHostContextProvider : ICommandHostContextProvider{
public class CommandHostContextProvider : ICommandHostContextProvider {
private readonly string[] _args;
private TextWriter _output;
private TextReader _input;
@@ -27,7 +27,20 @@ namespace OrchardCLI {
}
public void Shutdown(CommandHostContext context) {
ShutdownHost(context);
try {
if (context.CommandHost != null) {
LogInfo(context, "Shutting down Orchard session...");
context.CommandHost.StopSession(_input, _output);
}
}
catch (AppDomainUnloadedException) {
LogInfo(context, " (AppDomain already unloaded)");
}
if (context.AppObject != null) {
LogInfo(context, "Shutting down ASP.NET AppDomain...");
context.AppManager.ShutdownApplication(context.AppObject.ApplicationId);
}
}
private void Initialize(CommandHostContext context) {
@@ -64,27 +77,13 @@ namespace OrchardCLI {
context.OrchardDirectory = GetOrchardDirectory(context.Arguments.WorkingDirectory);
LogInfo(context, "Orchard root directory: \"{0}\"", context.OrchardDirectory.FullName);
CreateHost(context);
context.CommandHost.StartSession(_input, _output);
}
private void CreateHost(CommandHostContext context) {
context.AppManager = ApplicationManager.GetApplicationManager();
LogInfo(context, "Creating ASP.NET AppDomain for command agent...");
context.AppManager = ApplicationManager.GetApplicationManager();
context.AppObject = CreateWorkerAppDomainWithHost(context.AppManager, context.Arguments.VirtualPath, context.OrchardDirectory.FullName, typeof(CommandHost));
context.CommandHost = (CommandHost)context.AppObject.ObjectInstance;
}
private void ShutdownHost(CommandHostContext context) {
if (context.CommandHost != null) {
context.CommandHost.StopSession(_input, _output);
}
if (context.AppObject != null) {
LogInfo(context, "Shutting down ASP.NET AppDomain...");
context.AppManager.ShutdownApplication(context.AppObject.ApplicationId);
}
LogInfo(context, "Starting Orchard session");
context.StartSessionResult = context.CommandHost.StartSession(_input, _output);
}
private int GeneralHelp() {