mirror of
https://github.com/lidarr/lidarr.git
synced 2025-07-16 10:03:51 -07:00
Commands return immediately and signalr is used to control the UI
This commit is contained in:
parent
772ab3c921
commit
c96ba5efd3
55 changed files with 439 additions and 238 deletions
|
@ -78,61 +78,94 @@ namespace NzbDrone.Common.Messaging
|
|||
{
|
||||
Ensure.That(() => command).IsNotNull();
|
||||
|
||||
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
||||
_logger.Trace("Publishing {0}", command.GetType().Name);
|
||||
|
||||
var trackedCommand = _trackCommands.TrackIfNew(command);
|
||||
|
||||
if (trackedCommand == null)
|
||||
{
|
||||
_logger.Info("Command is already in progress: {0}", command.GetType().Name);
|
||||
return;
|
||||
}
|
||||
|
||||
ExecuteCommand<TCommand>(trackedCommand);
|
||||
}
|
||||
|
||||
public void PublishCommand(string commandTypeName)
|
||||
{
|
||||
dynamic command = GetCommand(commandTypeName);
|
||||
PublishCommand(command);
|
||||
}
|
||||
|
||||
public TrackedCommand PublishCommandAsync<TCommand>(TCommand command) where TCommand : class, ICommand
|
||||
{
|
||||
Ensure.That(() => command).IsNotNull();
|
||||
|
||||
_logger.Trace("Publishing {0}", command.GetType().Name);
|
||||
|
||||
var existingCommand = _trackCommands.TrackNewOrGet(command);
|
||||
|
||||
if (existingCommand.Existing)
|
||||
{
|
||||
_logger.Info("Command is already in progress: {0}", command.GetType().Name);
|
||||
return existingCommand.TrackedCommand;
|
||||
}
|
||||
|
||||
_taskFactory.StartNew(() => ExecuteCommand<TCommand>(existingCommand.TrackedCommand)
|
||||
, TaskCreationOptions.PreferFairness)
|
||||
.LogExceptions();
|
||||
|
||||
return existingCommand.TrackedCommand;
|
||||
}
|
||||
|
||||
public TrackedCommand PublishCommandAsync(string commandTypeName)
|
||||
{
|
||||
dynamic command = GetCommand(commandTypeName);
|
||||
return PublishCommandAsync(command);
|
||||
}
|
||||
|
||||
private dynamic GetCommand(string commandTypeName)
|
||||
{
|
||||
var commandType = _serviceFactory.GetImplementations(typeof(ICommand))
|
||||
.Single(c => c.FullName.Equals(commandTypeName, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
return Json.Deserialize("{}", commandType);
|
||||
}
|
||||
|
||||
private void ExecuteCommand<TCommand>(TrackedCommand trackedCommand) where TCommand : class, ICommand
|
||||
{
|
||||
var command = (TCommand)trackedCommand.Command;
|
||||
|
||||
var handlerContract = typeof(IExecute<>).MakeGenericType(command.GetType());
|
||||
var handler = (IExecute<TCommand>)_serviceFactory.Build(handlerContract);
|
||||
|
||||
_logger.Debug("{0} -> {1}", command.GetType().Name, handler.GetType().Name);
|
||||
|
||||
var sw = Stopwatch.StartNew();
|
||||
TrackedCommand trackedCommand = null;
|
||||
|
||||
try
|
||||
{
|
||||
trackedCommand = _trackCommands.TrackIfNew(command);
|
||||
|
||||
if (trackedCommand == null)
|
||||
{
|
||||
_logger.Info("Command is already in progress: {0}", command.GetType().Name);
|
||||
return;
|
||||
}
|
||||
|
||||
MappedDiagnosticsContext.Set("CommandId", trackedCommand.Command.CommandId);
|
||||
|
||||
PublishEvent(new CommandStartedEvent(command));
|
||||
PublishEvent(new CommandStartedEvent(trackedCommand));
|
||||
handler.Execute(command);
|
||||
sw.Stop();
|
||||
|
||||
_trackCommands.Completed(trackedCommand, sw.Elapsed);
|
||||
PublishEvent(new CommandCompletedEvent(command));
|
||||
PublishEvent(new CommandCompletedEvent(trackedCommand));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
if (trackedCommand != null)
|
||||
{
|
||||
_trackCommands.Failed(trackedCommand, e);
|
||||
}
|
||||
|
||||
PublishEvent(new CommandFailedEvent(command, e));
|
||||
_trackCommands.Failed(trackedCommand, e);
|
||||
PublishEvent(new CommandFailedEvent(trackedCommand, e));
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
PublishEvent(new CommandExecutedEvent(command));
|
||||
PublishEvent(new CommandExecutedEvent(trackedCommand));
|
||||
}
|
||||
|
||||
_logger.Debug("{0} <- {1} [{2}]", command.GetType().Name, handler.GetType().Name, sw.Elapsed.ToString(""));
|
||||
}
|
||||
|
||||
public void PublishCommand(string commandTypeName)
|
||||
{
|
||||
var commandType = _serviceFactory.GetImplementations(typeof(ICommand))
|
||||
.Single(c => c.FullName.Equals(commandTypeName, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
dynamic command = Json.Deserialize("{}", commandType);
|
||||
PublishCommand(command);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue