Added IHandleAsync, these handlers will be run async and in parallel to each other.

pull/4/head
kay.one 12 years ago
parent 333a88ebd1
commit d1ba892e45

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
namespace NzbDrone.Common.Eventing
@ -20,11 +21,25 @@ namespace NzbDrone.Common.Eventing
{
_logger.Trace("Publishing {0}", message.GetType().Name);
//call synchronous handlers first.
foreach (var handler in _handlers().OfType<IHandle<TEvent>>())
{
_logger.Trace("{0} => {1}", message.GetType().Name, handler.GetType().Name);
_logger.Debug("{0} -> {1}", message.GetType().Name, handler.GetType().Name);
handler.Handle(message);
_logger.Debug("{0} -#> {1}", message.GetType().Name, handler.GetType().Name);
}
foreach (var handler in _handlers().OfType<IHandleAsync<TEvent>>())
{
var handlerLocal = handler;
Task.Factory.StartNew(() =>
{
_logger.Debug("{0} ~> {1}", message.GetType().Name, handlerLocal.GetType().Name);
handlerLocal.HandleAsync(message);
_logger.Debug("{0} ~#> {1}", message.GetType().Name, handlerLocal.GetType().Name);
});
}
}
}
}
}

@ -9,14 +9,32 @@ namespace NzbDrone.Common.Eventing
public interface IHandle<TEvent> : IHandle where TEvent : IEvent
{
/// <summary>
/// Handles the message.
/// Handles the message synchronously.
/// </summary>
/// <param name = "message">The message.</param>
void Handle(TEvent message);
}
/// <summary>
/// Denotes a class which can handle a particular type of message.
/// </summary>
/// <typeparam name = "TEvent">The type of message to handle.</typeparam>
public interface IHandleAsync<TEvent> : IHandleAsync where TEvent : IEvent
{
/// <summary>
/// Handles the message asynchronously.
/// </summary>
/// <param name = "message">The message.</param>
void HandleAsync(TEvent message);
}
/// <summary>
/// A marker interface for classes that subscribe to messages.
/// </summary>
public interface IHandle { }
/// <summary>
/// A marker interface for classes that subscribe to messages.
/// </summary>
public interface IHandleAsync : IHandle { }
}

@ -63,7 +63,7 @@ namespace NzbDrone.Core.Jobs.Implementations
return;
}
notification.CurrentMessage = String.Format("Epsiode metadata refresh completed for {0}", series.Title);
notification.CurrentMessage = String.Format("Episode metadata refresh completed for {0}", series.Title);
}
}
}

@ -1,6 +1,7 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.Eventing;
@ -9,7 +10,7 @@ using NzbDrone.Core.Tv.Events;
namespace NzbDrone.Core.MediaCover
{
public class MediaCoverService : IHandle<SeriesUpdatedEvent>
public class MediaCoverService : IHandleAsync<SeriesUpdatedEvent>
{
private readonly HttpProvider _httpProvider;
private readonly DiskProvider _diskProvider;
@ -28,7 +29,7 @@ namespace NzbDrone.Core.MediaCover
_coverRootFolder = environmentProvider.GetMediaCoverPath();
}
public void Handle(SeriesUpdatedEvent message)
public void HandleAsync(SeriesUpdatedEvent message)
{
EnsureCovers(message.Series);
}

Loading…
Cancel
Save