Fixes merge and re-code's the XmlCacheFilePersister to use ILogger

This commit is contained in:
Shannon
2015-05-05 12:44:19 +10:00
parent 2f9753cc12
commit 8fa838e8fb
2 changed files with 44 additions and 33 deletions

View File

@@ -21,6 +21,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
{
private readonly IBackgroundTaskRunner<XmlCacheFilePersister> _runner;
private readonly content _content;
private readonly ProfilingLogger _logger;
private readonly ManualResetEventSlim _latch = new ManualResetEventSlim(false);
private readonly object _locko = new object();
private bool _released;
@@ -41,14 +42,15 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
public bool RunsOnShutdown { get { return true; } }
// initialize the first instance, which is inactive (not touched yet)
public XmlCacheFilePersister(IBackgroundTaskRunner<XmlCacheFilePersister> runner, content content)
: this(runner, content, false)
public XmlCacheFilePersister(IBackgroundTaskRunner<XmlCacheFilePersister> runner, content content, ProfilingLogger logger)
: this(runner, content, logger, false)
{ }
private XmlCacheFilePersister(IBackgroundTaskRunner<XmlCacheFilePersister> runner, content content, bool touched)
private XmlCacheFilePersister(IBackgroundTaskRunner<XmlCacheFilePersister> runner, content content, ProfilingLogger logger, bool touched)
{
_runner = runner;
_content = content;
_logger = logger;
if (runner.TryAdd(this) == false)
{
@@ -61,7 +63,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
if (touched == false) return;
LogHelper.Debug<XmlCacheFilePersister>("Created, save in {0}ms.", () => WaitMilliseconds);
_logger.Logger.Debug<XmlCacheFilePersister>("Created, save in {0}ms.", () => WaitMilliseconds);
_initialTouch = DateTime.Now;
_timer = new Timer(_ => TimerRelease());
_timer.Change(WaitMilliseconds, 0);
@@ -83,22 +85,22 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
{
if (_released) // our timer has triggered OR the runner is shutting down
{
LogHelper.Debug<XmlCacheFilePersister>("Touched, was released...");
_logger.Logger.Debug<XmlCacheFilePersister>("Touched, was released...");
// release: has run or is running, too late, return a new task (adds itself to runner)
if (_runner == null)
{
LogHelper.Debug<XmlCacheFilePersister>("Runner is down, run now.");
_logger.Logger.Debug<XmlCacheFilePersister>("Runner is down, run now.");
runNow = true;
}
else
{
LogHelper.Debug<XmlCacheFilePersister>("Create new...");
ret = new XmlCacheFilePersister(_runner, _content, true);
_logger.Logger.Debug<XmlCacheFilePersister>("Create new...");
ret = new XmlCacheFilePersister(_runner, _content, _logger, true);
if (ret._runner == null)
{
// could not enlist with the runner, runner is completed, must run now
LogHelper.Debug<XmlCacheFilePersister>("Runner is down, run now.");
_logger.Logger.Debug<XmlCacheFilePersister>("Runner is down, run now.");
runNow = true;
}
}
@@ -106,7 +108,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
else if (_timer == null) // we don't have a timer yet
{
LogHelper.Debug<XmlCacheFilePersister>("Touched, was idle, start and save in {0}ms.");
_logger.Logger.Debug<XmlCacheFilePersister>("Touched, was idle, start and save in {0}ms.", () => WaitMilliseconds);
_initialTouch = DateTime.Now;
_timer = new Timer(_ => TimerRelease());
_timer.Change(WaitMilliseconds, 0);
@@ -119,12 +121,12 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
if (DateTime.Now - _initialTouch < TimeSpan.FromMilliseconds(MaxWaitMilliseconds))
{
LogHelper.Debug<XmlCacheFilePersister>("Touched, was waiting, can delay, save in {0}ms.", () => WaitMilliseconds);
_logger.Logger.Debug<XmlCacheFilePersister>("Touched, was waiting, can delay, save in {0}ms.", () => WaitMilliseconds);
_timer.Change(WaitMilliseconds, 0);
}
else
{
LogHelper.Debug<XmlCacheFilePersister>("Touched, was waiting, cannot delay.");
_logger.Logger.Debug<XmlCacheFilePersister>("Touched, was waiting, cannot delay.");
}
}
}
@@ -139,7 +141,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
{
lock (_locko)
{
LogHelper.Debug<XmlCacheFilePersister>("Timer: release.");
_logger.Logger.Debug<XmlCacheFilePersister>("Timer: release.");
if (_timer != null)
_timer.Dispose();
_timer = null;
@@ -165,7 +167,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
{
lock (_locko)
{
LogHelper.Debug<XmlCacheFilePersister>("Run now (async).");
_logger.Logger.Debug<XmlCacheFilePersister>("Run now (async).");
// just make sure - in case the runner is running the task on shutdown
_released = true;
}
@@ -173,6 +175,12 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
// http://stackoverflow.com/questions/13489065/best-practice-to-call-configureawait-for-all-server-side-code
// http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
// do we really need that ConfigureAwait here?
// - In theory, no, because we are already executing on a background thread because we know it is there and
// there won't be any SynchronizationContext to resume to, however this is 'library' code and
// who are we to say that this will never be executed in a sync context... this is best practice to be sure
// it won't cause problems.
// .... so yes we want it.
using (await _runLock.LockAsync())
{
@@ -192,7 +200,7 @@ namespace Umbraco.Web.PublishedCache.XmlPublishedCache
{
lock (_locko)
{
LogHelper.Debug<XmlCacheFilePersister>("Run now (sync).");
_logger.Logger.Debug<XmlCacheFilePersister>("Run now (sync).");
// not really needed but safer (it's only us invoking Run, but the method is public...)
_released = true;
}