1
0
mirror of https://github.com/bitwarden/server.git synced 2024-11-22 12:15:36 +01:00

signalr heartbeat

This commit is contained in:
Kyle Spearrin 2019-07-11 23:06:30 -04:00
parent 51e4c5e1b7
commit 7c41da494f
2 changed files with 63 additions and 0 deletions

View File

@ -0,0 +1,62 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Bit.Core;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Bit.Notifications
{
public class HeartbeatHostedService : IHostedService, IDisposable
{
private readonly ILogger _logger;
private readonly IHubContext<NotificationsHub> _hubContext;
private readonly GlobalSettings _globalSettings;
private Task _executingTask;
private CancellationTokenSource _cts;
public HeartbeatHostedService(
ILogger<AzureQueueHostedService> logger,
IHubContext<NotificationsHub> hubContext,
GlobalSettings globalSettings)
{
_logger = logger;
_hubContext = hubContext;
_globalSettings = globalSettings;
}
public Task StartAsync(CancellationToken cancellationToken)
{
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
_executingTask = ExecuteAsync(_cts.Token);
return _executingTask.IsCompleted ? _executingTask : Task.CompletedTask;
}
public async Task StopAsync(CancellationToken cancellationToken)
{
if(_executingTask == null)
{
return;
}
_logger.LogWarning("Stopping service.");
_cts.Cancel();
await Task.WhenAny(_executingTask, Task.Delay(-1, cancellationToken));
cancellationToken.ThrowIfCancellationRequested();
}
public void Dispose()
{ }
private async Task ExecuteAsync(CancellationToken cancellationToken)
{
while(!cancellationToken.IsCancellationRequested)
{
await _hubContext.Clients.All.SendAsync("Heartbeat");
await Task.Delay(120000);
}
_logger.LogWarning("Done with heartbeat.");
}
}
}

View File

@ -68,6 +68,7 @@ namespace Bit.Notifications
// Mvc
services.AddMvc();
services.AddHostedService<HeartbeatHostedService>();
if(!globalSettings.SelfHosted)
{
// Hosted Services