Retrying on tenant startup

Conflicts:
	src/Orchard.Web/Config/HostComponents.config
	src/Orchard/Environment/DefaultOrchardHost.cs
This commit is contained in:
Sebastien Ros 2016-02-23 16:36:02 -08:00
parent aae9f6270e
commit 712ff2f487
2 changed files with 39 additions and 9 deletions

View File

@ -106,5 +106,14 @@
</Properties>
</Component>
<Component Type="Orchard.Environment.DefaultOrchardHost">
<Properties>
<!-- The number of retries when a tenant can't be loaded -->
<Property Name="Retries" Value="1"/>
<Property Name="DelayRetries" Value="true"/>
</Properties>
</Component>
</Components>
</HostComponents>

View File

@ -14,6 +14,7 @@ using Orchard.Localization;
using Orchard.Logging;
using Orchard.Utility.Extensions;
using Orchard.Exceptions;
using System.Threading;
namespace Orchard.Environment {
// All the event handlers that DefaultOrchardHost implements have to be declared in OrchardStarter
@ -32,6 +33,10 @@ namespace Orchard.Environment {
private IEnumerable<ShellContext> _shellContexts;
private readonly ContextState<IList<ShellSettings>> _tenantsToRestart;
public int Retries { get; set; }
public bool DelayRetries { get; set; }
public DefaultOrchardHost(
IShellSettingsManager shellSettingsManager,
IShellContextFactory shellContextFactory,
@ -137,16 +142,32 @@ namespace Orchard.Environment {
// load all tenants, and activate their shell
if (allSettings.Any()) {
Parallel.ForEach(allSettings, settings => {
try {
var context = CreateShellContext(settings);
ActivateShell(context);
}
catch (Exception ex) {
if (ex.IsFatal()) {
throw;
}
Logger.Error(ex, "A tenant could not be started: " + settings.Name);
for (var i = 0; i <= Retries; i++) {
// Not the first attempt, wait for a while ...
if (DelayRetries && i > 0) {
// Waits for i^2 which means 1, 2, 4, 8 ... seconds
Thread.Sleep(TimeSpan.FromSeconds(Math.Pow(i, 2)));
}
bool failed = false;
try {
var context = CreateShellContext(settings);
ActivateShell(context);
}
catch (Exception ex) {
// An exception at this point is always fatal as it literally kills the
// tenant. What is more fatal than something that kills you?
Logger.Error(ex, "A tenant could not be started: " + settings.Name + " Attempt number: " + i);
failed = true;
}
if(failed && i == Retries) {
Logger.Fatal("A tenant could not be started: {0} after {1} retries.", settings.Name, Retries);
return;
}
}
while (_processingEngine.AreTasksPending()) {
Logger.Debug("Processing pending task after activate Shell");
_processingEngine.ExecuteNextTask();