1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-26 01:27:00 +00: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.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NLog;
namespace NzbDrone.Common.Eventing
@ -20,11 +21,25 @@ public void Publish<TEvent>(TEvent message) where TEvent : IEvent
{
_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);
});
}
}
}
}
}

View file

@ -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 { }
}

View file

@ -63,7 +63,7 @@ private void RefreshMetadata(ProgressNotification notification, Series series)
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.IO;
using System.Linq;
using System.Threading;
using NLog;
using NzbDrone.Common;
using NzbDrone.Common.Eventing;
@ -9,7 +10,7 @@
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 @@ public MediaCoverService(HttpProvider httpProvider, DiskProvider diskProvider, E
_coverRootFolder = environmentProvider.GetMediaCoverPath();
}
public void Handle(SeriesUpdatedEvent message)
public void HandleAsync(SeriesUpdatedEvent message)
{
EnsureCovers(message.Series);
}