mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-10-02 16:52:04 +02:00
signalr cleanup
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
using System;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
using Microsoft.AspNet.SignalR.Infrastructure;
|
||||
using NzbDrone.Api.SignalR;
|
||||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Common.Messaging.Events;
|
||||
using NzbDrone.Common.Messaging.Tracking;
|
||||
|
||||
namespace NzbDrone.Api.Commands
|
||||
{
|
||||
public class CommandConnection : NzbDronePersistentConnection,
|
||||
IHandleAsync<CommandStartedEvent>,
|
||||
IHandleAsync<CommandCompletedEvent>,
|
||||
IHandleAsync<CommandFailedEvent>
|
||||
{
|
||||
public override string Resource
|
||||
{
|
||||
get { return "/Command"; }
|
||||
}
|
||||
|
||||
public void HandleAsync(CommandStartedEvent message)
|
||||
{
|
||||
BroadcastMessage(message.TrackedCommand);
|
||||
}
|
||||
|
||||
public void HandleAsync(CommandCompletedEvent message)
|
||||
{
|
||||
BroadcastMessage(message.TrackedCommand);
|
||||
}
|
||||
|
||||
public void HandleAsync(CommandFailedEvent message)
|
||||
{
|
||||
BroadcastMessage(message.TrackedCommand);
|
||||
}
|
||||
|
||||
private void BroadcastMessage(TrackedCommand trackedCommand)
|
||||
{
|
||||
var context = ((ConnectionManager)GlobalHost.ConnectionManager).GetConnection(GetType());
|
||||
context.Connection.Broadcast(trackedCommand);
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,47 +1,68 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Nancy;
|
||||
using NzbDrone.Api.Extensions;
|
||||
using NzbDrone.Api.Mapping;
|
||||
using NzbDrone.Api.Validation;
|
||||
using NzbDrone.Common.Composition;
|
||||
using NzbDrone.Common.Messaging;
|
||||
using NzbDrone.Common.Messaging.Tracking;
|
||||
using NzbDrone.Core.Datastore.Events;
|
||||
using NzbDrone.Core.Messaging;
|
||||
using NzbDrone.Core.Messaging.Commands;
|
||||
using NzbDrone.Core.Messaging.Tracking;
|
||||
using NzbDrone.Core.ProgressMessaging;
|
||||
|
||||
|
||||
namespace NzbDrone.Api.Commands
|
||||
{
|
||||
public class CommandModule : NzbDroneRestModule<CommandResource>
|
||||
public class CommandModule : NzbDroneRestModuleWithSignalR<CommandResource, Command>, IHandle<CommandUpdatedEvent>
|
||||
{
|
||||
private readonly IMessageAggregator _messageAggregator;
|
||||
private readonly IContainer _container;
|
||||
private readonly ITrackCommands _trackCommands;
|
||||
|
||||
public CommandModule(IMessageAggregator messageAggregator, IContainer container, ITrackCommands trackCommands)
|
||||
: base(messageAggregator)
|
||||
{
|
||||
_messageAggregator = messageAggregator;
|
||||
_container = container;
|
||||
_trackCommands = trackCommands;
|
||||
|
||||
Post["/"] = x => RunCommand(ReadResourceFromRequest());
|
||||
Get["/"] = x => GetAllCommands();
|
||||
GetResourceById = GetCommand;
|
||||
CreateResource = StartCommand;
|
||||
GetResourceAll = GetAllCommands;
|
||||
|
||||
PostValidator.RuleFor(c => c.Name).NotBlank();
|
||||
}
|
||||
|
||||
private Response RunCommand(CommandResource resource)
|
||||
private CommandResource GetCommand(int id)
|
||||
{
|
||||
return _trackCommands.GetById(id).InjectTo<CommandResource>();
|
||||
}
|
||||
|
||||
private int StartCommand(CommandResource commandResource)
|
||||
{
|
||||
var commandType =
|
||||
_container.GetImplementations(typeof(ICommand))
|
||||
.Single(c => c.Name.Replace("Command", "")
|
||||
.Equals(resource.Command, StringComparison.InvariantCultureIgnoreCase));
|
||||
_container.GetImplementations(typeof(Command))
|
||||
.Single(c => c.Name.Replace("Command", "")
|
||||
.Equals(commandResource.Name, StringComparison.InvariantCultureIgnoreCase));
|
||||
|
||||
dynamic command = Request.Body.FromJson(commandType);
|
||||
|
||||
var response = (TrackedCommand) _messageAggregator.PublishCommandAsync(command);
|
||||
|
||||
return response.AsResponse(HttpStatusCode.Created);
|
||||
var trackedCommand = (Command)_messageAggregator.PublishCommandAsync(command);
|
||||
return trackedCommand.Id;
|
||||
}
|
||||
|
||||
private Response GetAllCommands()
|
||||
private List<CommandResource> GetAllCommands()
|
||||
{
|
||||
return _trackCommands.AllTracked().AsResponse();
|
||||
return ToListResource(_trackCommands.RunningCommands);
|
||||
}
|
||||
|
||||
public void Handle(CommandUpdatedEvent message)
|
||||
{
|
||||
if (message.Command.SendUpdatesToClient)
|
||||
{
|
||||
BroadcastResourceChange(ModelAction.Updated, message.Command.Id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,9 +1,16 @@
|
||||
using NzbDrone.Api.REST;
|
||||
using System;
|
||||
using NzbDrone.Api.REST;
|
||||
using NzbDrone.Core.Messaging.Tracking;
|
||||
|
||||
namespace NzbDrone.Api.Commands
|
||||
{
|
||||
public class CommandResource : RestResource
|
||||
{
|
||||
public string Command { get; set; }
|
||||
public String Name { get; set; }
|
||||
public String Message { get; set; }
|
||||
public DateTime StartedOn { get; set; }
|
||||
public DateTime StateChangeTime { get; set; }
|
||||
public Boolean SendUpdatesToClient { get; set; }
|
||||
public CommandStatus State { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user