polling for task status.

This commit is contained in:
2020-03-25 11:39:26 +01:00
parent e6561308e5
commit 2a8f4cbb8d
3 changed files with 77 additions and 37 deletions

View File

@@ -1,4 +1,7 @@
using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using FarmmapsApi.HttpMessageHandlers;
using FarmmapsApi.Models;
using FarmmapsApi.Services;
@@ -9,7 +12,8 @@ namespace FarmmapsApi
{
public static class Extensions
{
public static IServiceCollection AddFarmmapsServices(this IServiceCollection serviceCollection, Configuration configuration)
public static IServiceCollection AddFarmmapsServices(this IServiceCollection serviceCollection,
Configuration configuration)
{
return serviceCollection
.AddSingleton(configuration)
@@ -23,7 +27,26 @@ namespace FarmmapsApi
.AddTransient<FarmmapsAuthenticationHandler>()
.AddHttpClient<FarmmapsApiService>()
.AddHttpMessageHandler<FarmmapsAuthenticationHandler>()
.Services;;
.Services;
;
}
public static async Task PollTask(TimeSpan retryTime, Func<CancellationTokenSource, Task> callback)
{
var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
do
{
try
{
await callback(tokenSource);
await Task.Delay(retryTime, token);
}
catch
{
// ignored
}
} while (!token.IsCancellationRequested);
}
}
}

View File

@@ -11,8 +11,9 @@ using System.Threading.Tasks;
using System.Web;
using FarmmapsApi.Models;
using IdentityModel;
using Microsoft.AspNetCore.Http.Connections;
using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@@ -20,6 +21,7 @@ namespace FarmmapsApi.Services
{
public class FarmmapsApiService
{
private readonly ILogger<FarmmapsApiService> _logger;
private readonly HttpClient _httpClient;
private readonly OpenIdConnectService _openIdConnectService;
private readonly Configuration _configuration;
@@ -28,9 +30,10 @@ namespace FarmmapsApi.Services
public event Action<EventMessage> EventCallback;
public FarmmapsApiService(HttpClient httpClient,
public FarmmapsApiService(HttpClient httpClient, ILogger<FarmmapsApiService> logger,
OpenIdConnectService openIdConnectService, Configuration configuration)
{
_logger = logger;
_httpClient = httpClient;
_openIdConnectService = openIdConnectService;
_configuration = configuration;
@@ -65,13 +68,21 @@ namespace FarmmapsApi.Services
var uri = new Uri(_configuration.Endpoint);
var eventEndpoint = $"{uri.Scheme}://{uri.Host}:{uri.Port}/EventHub";
_hubConnection = new HubConnectionBuilder()
.WithUrl(eventEndpoint)
.WithUrl(eventEndpoint, HttpTransportType.WebSockets,
options => options.SkipNegotiation = true)
.ConfigureLogging(log => log.AddConsole())
.WithAutomaticReconnect()
.AddJsonProtocol()
.Build();
_hubConnection.On("Event", (EventMessage @event) => EventCallback?.Invoke(@event));
await _hubConnection.StartAsync();
await AuthenticateEventHub(accessToken);
_hubConnection.Reconnected += async s => await AuthenticateEventHub(accessToken);
_hubConnection.On("event", (Object @event) => _logger.LogInformation(@event.ToString()));
}
private async Task AuthenticateEventHub(string accessToken)
{
await _hubConnection.SendAsync("authenticate", accessToken);
}