Skip to content

Commit

Permalink
SignalR crash fix (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
hidden4003 committed Sep 5, 2019
1 parent 895c9bd commit ebf5084
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
34 changes: 34 additions & 0 deletions Shoko.Server/API/SignalR/EventEmitter.cs
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Shoko.Server.Commands;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace Shoko.Server.API.SignalR
{
public class EventEmitter
{
public EventEmitter(IHubContext<EventsHub> hub)
{
Hub = hub;
}

private IHubContext<EventsHub> Hub
{
get;
set;
}

public async Task QueueStateChanged(string queue, QueueStateEventArgs e)
{
await Hub.Clients.All.SendAsync("QueueStateChanged", queue, e.QueueState.formatMessage());
}

public async Task QueueCountChanged(string queue, QueueCountEventArgs e)
{
await Hub.Clients.All.SendAsync("QueueCountChanged", queue, e.QueueCount);
}
}
}
9 changes: 6 additions & 3 deletions Shoko.Server/API/SignalR/EventsHub.cs
Expand Up @@ -10,8 +10,11 @@ namespace Shoko.Server.API.SignalR
{
public class EventsHub : Hub
{
public EventsHub()
private readonly EventEmitter _eventEmitter;

public EventsHub(EventEmitter eventEmitter)
{
_eventEmitter = eventEmitter;
ShokoService.CmdProcessorGeneral.OnQueueCountChangedEvent += (e) => OnQueueCountChangedEvent("general", e);
ShokoService.CmdProcessorHasher.OnQueueCountChangedEvent += (e) => OnQueueCountChangedEvent("hasher", e);
ShokoService.CmdProcessorImages.OnQueueCountChangedEvent += (e) => OnQueueCountChangedEvent("images", e);
Expand All @@ -23,12 +26,12 @@ public EventsHub()

private async void OnQueueStateChangedEvent(string queue, QueueStateEventArgs e)
{
await Clients.All.SendAsync("QueueStateChanged", queue, e.QueueState.formatMessage());
await _eventEmitter.QueueStateChanged(queue, e);
}

private async void OnQueueCountChangedEvent(string queue, Commands.QueueCountEventArgs ev)
{
await Clients.All.SendAsync("QueueCountChanged", queue, ev.QueueCount);
await _eventEmitter.QueueCountChanged(queue, ev);
}

public void ChangeQueueProcessingState(string queue, bool paused)
Expand Down
7 changes: 6 additions & 1 deletion Shoko.Server/API/Startup.cs
Expand Up @@ -85,7 +85,12 @@ public void ConfigureServices(IServiceCollection services)

services.ConfigureSwaggerGen(options => { options.CustomSchemaIds(x => x.FullName); });

services.AddSignalR();
services.AddSignalR(o =>
{
o.EnableDetailedErrors = true;
});

services.AddSingleton<EventEmitter>();

// this caused issues with auth. https://stackoverflow.com/questions/43574552
services.AddMvc(options =>
Expand Down
1 change: 1 addition & 0 deletions Shoko.Server/Shoko.Server.csproj
Expand Up @@ -1000,6 +1000,7 @@
<Compile Include="API\Resolvers\EmitEmptyEnumerableInsteadOfNullResolver.cs" />
<Compile Include="API\Resolvers\OmitEmptyEnumerableResolver.cs" />
<Compile Include="API\RestController.cs" />
<Compile Include="API\SignalR\EventEmitter.cs" />
<Compile Include="API\SignalR\EventsHub.cs" />
<Compile Include="API\Startup.cs" />
<Compile Include="API\UserHandler.cs" />
Expand Down

0 comments on commit ebf5084

Please sign in to comment.