1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

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

This commit is contained in:
kay.one 2013-03-04 22:25:05 -08:00
parent 333a88ebd1
commit d1ba892e45
4 changed files with 40 additions and 6 deletions

View File

@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using NLog; using NLog;
namespace NzbDrone.Common.Eventing namespace NzbDrone.Common.Eventing
@ -20,11 +21,25 @@ public void Publish<TEvent>(TEvent message) where TEvent : IEvent
{ {
_logger.Trace("Publishing {0}", message.GetType().Name); _logger.Trace("Publishing {0}", message.GetType().Name);
//call synchronous handlers first.
foreach (var handler in _handlers().OfType<IHandle<TEvent>>()) 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); 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);
});
} }
} }
} }
} }

View File

@ -9,14 +9,32 @@ namespace NzbDrone.Common.Eventing
public interface IHandle<TEvent> : IHandle where TEvent : IEvent public interface IHandle<TEvent> : IHandle where TEvent : IEvent
{ {
/// <summary> /// <summary>
/// Handles the message. /// Handles the message synchronously.
/// </summary> /// </summary>
/// <param name = "message">The message.</param> /// <param name = "message">The message.</param>
void Handle(TEvent message); 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> /// <summary>
/// A marker interface for classes that subscribe to messages. /// A marker interface for classes that subscribe to messages.
/// </summary> /// </summary>
public interface IHandle { } public interface IHandle { }
/// <summary>
/// A marker interface for classes that subscribe to messages.
/// </summary>
public interface IHandleAsync : IHandle { }
} }

View File

@ -63,7 +63,7 @@ private void RefreshMetadata(ProgressNotification notification, Series series)
return; 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);
} }
} }
} }

View File

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