Fixes build, updated BackgroundTaskRunner to get an ILogger injected

This commit is contained in:
Shannon
2015-04-09 17:12:52 +10:00
parent a1c7352da1
commit 19715ef45f
7 changed files with 56 additions and 87 deletions

View File

@@ -18,14 +18,7 @@ namespace Umbraco.Tests.Routing
{
protected override void FreezeResolution()
{
SiteDomainHelperResolver.Current = new SiteDomainHelperResolver(new SiteDomainHelper());
base.FreezeResolution();
}
{
SiteDomainHelperResolver.Current = new SiteDomainHelperResolver(new SiteDomainHelper());
SiteDomainHelperResolver.Current = new SiteDomainHelperResolver(new SiteDomainHelper());
base.FreezeResolution();
}
@@ -333,32 +326,7 @@ namespace Umbraco.Tests.Routing
Assert.AreEqual(pcr.PublishedContent.Id, expectedNode);
}
#region Cases
[TestCase(10011, "http://domain1.com/", "en-US")]
[TestCase(100111, "http://domain1.com/", "en-US")]
[TestCase(10011, "http://domain1.fr/", "fr-FR")]
[TestCase(100111, "http://domain1.fr/", "fr-FR")]
[TestCase(1001121, "http://domain1.fr/", "de-DE")]
#endregion
public void GetCulture(int nodeId, string currentUrl, string expectedCulture)
{
var langEn = Language.GetByCultureCode("en-US");
var langFr = Language.GetByCultureCode("fr-FR");
var langDe = Language.GetByCultureCode("de-DE");
Domain.MakeNew("domain1.com/", 1001, langEn.id);
Domain.MakeNew("domain1.fr/", 1001, langFr.id);
Domain.MakeNew("*100112", 100112, langDe.id);
var routingContext = GetRoutingContext("http://anything/");
var umbracoContext = routingContext.UmbracoContext;
var content = umbracoContext.ContentCache.GetById(nodeId);
Assert.IsNotNull(content);
var culture = Web.Models.ContentExtensions.GetCulture(umbracoContext, null, null, content.Id, content.Path, new Uri(currentUrl));
Assert.AreEqual(expectedCulture, culture.Name);
}
#region Cases
[TestCase(10011, "http://domain1.com/", "en-US")]

View File

@@ -6,6 +6,7 @@ using System.Threading;
using System.Threading.Tasks;
using NUnit.Framework;
using Umbraco.Core;
using Umbraco.Core.Logging;
using Umbraco.Tests.TestHelpers;
using Umbraco.Web.Scheduling;
@@ -15,16 +16,18 @@ namespace Umbraco.Tests.Scheduling
[Timeout(30000)]
public class BackgroundTaskRunnerTests
{
ILogger _logger;
[TestFixtureSetUp]
public void InitializeFixture()
{
TestHelper.SetupLog4NetForTests();
_logger = new DebugDiagnosticsLogger();
}
[Test]
public async void ShutdownWaitWhenRunning()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { AutoStart = true, KeepAlive = true }))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { AutoStart = true, KeepAlive = true }, _logger))
{
Assert.IsTrue(runner.IsRunning);
Thread.Sleep(800); // for long
@@ -38,7 +41,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void ShutdownWhenRunning()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
Console.WriteLine("Begin {0}", DateTime.Now);
@@ -66,7 +69,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void ShutdownFlushesTheQueue()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
Assert.IsFalse(runner.IsRunning);
runner.Add(new MyTask(5000));
@@ -83,7 +86,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void ShutdownForceTruncatesTheQueue()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
Assert.IsFalse(runner.IsRunning);
runner.Add(new MyTask(5000));
@@ -100,7 +103,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void ShutdownThenForce()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
Assert.IsFalse(runner.IsRunning);
runner.Add(new MyTask(5000));
@@ -120,7 +123,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public void Create_IsRunning()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
Assert.IsFalse(runner.IsRunning);
}
@@ -129,7 +132,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void Create_AutoStart_IsRunning()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { AutoStart = true }))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { AutoStart = true }, _logger))
{
Assert.IsTrue(runner.IsRunning);
await runner; // wait for the entire runner operation to complete
@@ -139,7 +142,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public void Create_AutoStartAndKeepAlive_IsRunning()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { AutoStart = true, KeepAlive = true }))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { AutoStart = true, KeepAlive = true }, _logger))
{
Assert.IsTrue(runner.IsRunning);
Thread.Sleep(800); // for long
@@ -152,7 +155,7 @@ namespace Umbraco.Tests.Scheduling
public async void Dispose_IsRunning()
{
BackgroundTaskRunner<IBackgroundTask> runner;
using (runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { AutoStart = true, KeepAlive = true }))
using (runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { AutoStart = true, KeepAlive = true }, _logger))
{
Assert.IsTrue(runner.IsRunning);
// dispose will stop it
@@ -165,7 +168,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public void Startup_KeepAlive_IsRunning()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { KeepAlive = true }))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions { KeepAlive = true }, _logger))
{
Assert.IsFalse(runner.IsRunning);
runner.StartUp();
@@ -177,7 +180,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void Create_AddTask_IsRunning()
{
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions(), _logger))
{
var waitHandle = new ManualResetEvent(false);
runner.TaskCompleted += (sender, args) =>
@@ -195,7 +198,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public void Create_KeepAliveAndAddTask_IsRunning()
{
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions { KeepAlive = true }))
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions { KeepAlive = true }, _logger))
{
var waitHandle = new ManualResetEvent(false);
runner.TaskCompleted += (sender, args) =>
@@ -213,7 +216,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void WaitOnRunner_OneTask()
{
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions(), _logger))
{
var task = new MyTask();
Assert.IsTrue(task.Ended == default(DateTime));
@@ -231,7 +234,7 @@ namespace Umbraco.Tests.Scheduling
for (var i = 0; i < 10; i++)
tasks.Add(new MyTask());
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions { KeepAlive = false, LongRunning = true, PreserveRunningTask = true }))
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions { KeepAlive = false, LongRunning = true, PreserveRunningTask = true }, _logger))
{
tasks.ForEach(runner.Add);
@@ -249,7 +252,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void WaitOnTask()
{
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions(), _logger))
{
var task = new MyTask();
var waitHandle = new ManualResetEvent(false);
@@ -269,7 +272,7 @@ namespace Umbraco.Tests.Scheduling
for (var i = 0; i < 10; i++)
tasks.Add(new MyTask(), new ManualResetEvent(false));
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions(), _logger))
{
runner.TaskCompleted += (sender, task) => tasks[task.Task].Set();
foreach (var t in tasks) runner.Add(t.Key);
@@ -298,7 +301,7 @@ namespace Umbraco.Tests.Scheduling
IDictionary<BaseTask, ManualResetEvent> tasks = getTasks();
BackgroundTaskRunner<BaseTask> tManager;
using (tManager = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions { LongRunning = true, KeepAlive = true }))
using (tManager = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions { LongRunning = true, KeepAlive = true }, _logger))
{
tManager.TaskCompleted += (sender, task) => tasks[task.Task].Set();
@@ -344,7 +347,7 @@ namespace Umbraco.Tests.Scheduling
List<BaseTask> tasks = getTasks();
using (var tManager = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions { LongRunning = true, PreserveRunningTask = true }))
using (var tManager = new BackgroundTaskRunner<BaseTask>(new BackgroundTaskRunnerOptions { LongRunning = true, PreserveRunningTask = true }, _logger))
{
tasks.ForEach(tManager.Add);
@@ -385,7 +388,7 @@ namespace Umbraco.Tests.Scheduling
{
var runCount = 0;
var waitHandle = new ManualResetEvent(false);
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
runner.TaskCompleted += (sender, args) => runCount++;
runner.TaskStarting += async (sender, args) =>
@@ -417,7 +420,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void DelayedTaskRuns()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
var task = new MyDelayedTask(200);
runner.Add(task);
@@ -435,7 +438,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void DelayedTaskStops()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
var task = new MyDelayedTask(200);
runner.Add(task);
@@ -454,7 +457,7 @@ namespace Umbraco.Tests.Scheduling
{
var runCount = 0;
var waitHandle = new ManualResetEvent(false);
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
runner.TaskCompleted += (sender, args) => runCount++;
runner.TaskStarting += async (sender, args) =>
@@ -483,7 +486,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void FailingTaskSync()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
var exceptions = new ConcurrentQueue<Exception>();
runner.TaskError += (sender, args) => exceptions.Enqueue(args.Exception);
@@ -500,7 +503,7 @@ namespace Umbraco.Tests.Scheduling
[Test]
public async void FailingTaskAsync()
{
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions()))
using (var runner = new BackgroundTaskRunner<IBackgroundTask>(new BackgroundTaskRunnerOptions(), _logger))
{
var exceptions = new ConcurrentQueue<Exception>();
runner.TaskError += (sender, args) => exceptions.Enqueue(args.Exception);

View File

@@ -174,7 +174,6 @@
<Compile Include="AngularIntegration\ServerVariablesParserTests.cs" />
<Compile Include="AttemptTests.cs" />
<Compile Include="Cache\CacheRefresherTests.cs" />
<Compile Include="Integration\GetCultureTests.cs" />
<Compile Include="Persistence\Repositories\AuditRepositoryTest.cs" />
<Compile Include="Persistence\Repositories\DomainRepositoryTest.cs" />
<Compile Include="Persistence\Repositories\PublicAccessRepositoryTest.cs" />

View File

@@ -95,10 +95,5 @@ namespace Umbraco.Web.Models
return defaultLanguage == null ? CultureInfo.CurrentUICulture : new CultureInfo(defaultLanguage.IsoCode);
}
private static CultureInfo GetDefaultCulture(ILocalizationService localizationService)
{
var defaultLanguage = localizationService.GetAllLanguages().FirstOrDefault();
return defaultLanguage == null ? CultureInfo.CurrentUICulture : new CultureInfo(defaultLanguage.IsoCode);
}
}
}

View File

@@ -20,6 +20,7 @@ namespace Umbraco.Web.Scheduling
where T : class, IBackgroundTask
{
private readonly BackgroundTaskRunnerOptions _options;
private readonly ILogger _logger;
private readonly BlockingCollection<T> _tasks = new BlockingCollection<T>();
private readonly object _locker = new object();
private readonly ManualResetEventSlim _completedEvent = new ManualResetEventSlim(false);
@@ -40,19 +41,22 @@ namespace Umbraco.Web.Scheduling
/// <summary>
/// Initializes a new instance of the <see cref="BackgroundTaskRunner{T}"/> class.
/// </summary>
public BackgroundTaskRunner()
: this(new BackgroundTaskRunnerOptions())
public BackgroundTaskRunner(ILogger logger)
: this(new BackgroundTaskRunnerOptions(), logger)
{ }
/// <summary>
/// Initializes a new instance of the <see cref="BackgroundTaskRunner{T}"/> class with a set of options.
/// </summary>
/// <param name="options">The set of options.</param>
public BackgroundTaskRunner(BackgroundTaskRunnerOptions options)
/// <param name="logger"></param>
public BackgroundTaskRunner(BackgroundTaskRunnerOptions options, ILogger logger)
{
if (options == null) throw new ArgumentNullException("options");
if (logger == null) throw new ArgumentNullException("logger");
_options = options;
_logger = logger;
HostingEnvironment.RegisterObject(this);
if (options.AutoStart)
@@ -133,7 +137,7 @@ namespace Umbraco.Web.Scheduling
throw new InvalidOperationException("The task runner has completed.");
// add task
LogHelper.Debug<BackgroundTaskRunner<T>>("Task added {0}", task.GetType);
_logger.Debug<BackgroundTaskRunner<T>>("Task added {0}", task.GetType);
_tasks.Add(task);
// start
@@ -154,7 +158,7 @@ namespace Umbraco.Web.Scheduling
if (_isCompleted) return false;
// add task
LogHelper.Debug<BackgroundTaskRunner<T>>("Task added {0}", task.GetType);
_logger.Debug<BackgroundTaskRunner<T>>("Task added {0}", task.GetType);
_tasks.Add(task);
// start
@@ -195,7 +199,7 @@ namespace Umbraco.Web.Scheduling
// create a new token source since this is a new process
_tokenSource = new CancellationTokenSource();
_runningTask = PumpIBackgroundTasks(Task.Factory, _tokenSource.Token);
LogHelper.Debug<BackgroundTaskRunner<T>>("Starting");
_logger.Debug<BackgroundTaskRunner<T>>("Starting");
}
/// <summary>
@@ -286,7 +290,7 @@ namespace Umbraco.Web.Scheduling
if (task != null && task.IsFaulted)
{
var exception = task.Exception;
LogHelper.Error<BackgroundTaskRunner<T>>("Task runner exception.", exception);
_logger.Error<BackgroundTaskRunner<T>>("Task runner exception.", exception);
}
// is it ok to run?
@@ -392,7 +396,7 @@ namespace Umbraco.Web.Scheduling
}
catch (Exception ex)
{
LogHelper.Error<BackgroundTaskRunner<T>>("Task has failed.", ex);
_logger.Error<BackgroundTaskRunner<T>>("Task has failed.", ex);
}
}
@@ -415,7 +419,7 @@ namespace Umbraco.Web.Scheduling
}
catch (Exception ex)
{
LogHelper.Error<BackgroundTaskRunner<T>>("TaskStarting exception occurred", ex);
_logger.Error<BackgroundTaskRunner<T>>("TaskStarting exception occurred", ex);
}
}
}
@@ -431,7 +435,7 @@ namespace Umbraco.Web.Scheduling
}
catch (Exception ex)
{
LogHelper.Error<BackgroundTaskRunner<T>>("TaskCompleted exception occurred", ex);
_logger.Error<BackgroundTaskRunner<T>>("TaskCompleted exception occurred", ex);
}
}
}
@@ -447,7 +451,7 @@ namespace Umbraco.Web.Scheduling
}
catch (Exception ex)
{
LogHelper.Error<BackgroundTaskRunner<T>>("TaskCancelled exception occurred", ex);
_logger.Error<BackgroundTaskRunner<T>>("TaskCancelled exception occurred", ex);
}
}
@@ -466,7 +470,7 @@ namespace Umbraco.Web.Scheduling
}
catch (Exception ex)
{
LogHelper.Error<BackgroundTaskRunner<T>>("OnCompleted exception occurred", ex);
_logger.Error<BackgroundTaskRunner<T>>("OnCompleted exception occurred", ex);
}
}
}
@@ -529,7 +533,7 @@ namespace Umbraco.Web.Scheduling
// processing, call the UnregisterObject method, and then return or it can return immediately and complete
// processing asynchronously before calling the UnregisterObject method.
LogHelper.Debug<BackgroundTaskRunner<T>>("Shutting down, waiting for tasks to complete.");
_logger.Debug<BackgroundTaskRunner<T>>("Shutting down, waiting for tasks to complete.");
Shutdown(false, false); // do not accept any more tasks, flush the queue, do not wait
lock (_locker)
@@ -538,12 +542,12 @@ namespace Umbraco.Web.Scheduling
_runningTask.ContinueWith(_ =>
{
HostingEnvironment.UnregisterObject(this);
LogHelper.Info<BackgroundTaskRunner<T>>("Down, tasks completed.");
_logger.Info<BackgroundTaskRunner<T>>("Down, tasks completed.");
});
else
{
HostingEnvironment.UnregisterObject(this);
LogHelper.Info<BackgroundTaskRunner<T>>("Down, tasks completed.");
_logger.Info<BackgroundTaskRunner<T>>("Down, tasks completed.");
}
}
}
@@ -554,10 +558,10 @@ namespace Umbraco.Web.Scheduling
// immediate parameter is true, the registered object must call the UnregisterObject method before returning;
// otherwise, its registration will be removed by the application manager.
LogHelper.Info<BackgroundTaskRunner<T>>("Shutting down immediately.");
_logger.Info<BackgroundTaskRunner<T>>("Shutting down immediately.");
Shutdown(true, true); // cancel all tasks, wait for the current one to end
HostingEnvironment.UnregisterObject(this);
LogHelper.Info<BackgroundTaskRunner<T>>("Down.");
_logger.Info<BackgroundTaskRunner<T>>("Down.");
}
}

View File

@@ -48,9 +48,9 @@ namespace Umbraco.Web.Scheduling
LogHelper.Debug<Scheduler>(() => "Initializing the scheduler");
// backgrounds runners are web aware, if the app domain dies, these tasks will wind down correctly
_publishingRunner = new BackgroundTaskRunner<IBackgroundTask>();
_tasksRunner = new BackgroundTaskRunner<IBackgroundTask>();
_scrubberRunner = new BackgroundTaskRunner<IBackgroundTask>();
_publishingRunner = new BackgroundTaskRunner<IBackgroundTask>(applicationContext.ProfilingLogger.Logger);
_tasksRunner = new BackgroundTaskRunner<IBackgroundTask>(applicationContext.ProfilingLogger.Logger);
_scrubberRunner = new BackgroundTaskRunner<IBackgroundTask>(applicationContext.ProfilingLogger.Logger);
var settings = UmbracoConfig.For.UmbracoSettings();

View File

@@ -33,7 +33,7 @@ namespace umbraco
public class content
{
private static readonly BackgroundTaskRunner<XmlCacheFilePersister> FilePersister
= new BackgroundTaskRunner<XmlCacheFilePersister>(new BackgroundTaskRunnerOptions { LongRunning = true });
= new BackgroundTaskRunner<XmlCacheFilePersister>(new BackgroundTaskRunnerOptions { LongRunning = true }, LoggerResolver.Current.Logger);
private XmlCacheFilePersister _persisterTask;