-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeedPoller.cs
66 lines (56 loc) · 2.23 KB
/
FeedPoller.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Net.Http.Headers;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Digdir.Oed.FeedPoller.Settings;
namespace Digdir.Oed.FeedPoller;
public class FeedPoller
{
private readonly ILogger _logger;
private readonly OedSettings _oedSettings;
private readonly IHttpClientFactory _clientFactory;
public FeedPoller(
ILoggerFactory loggerFactory,
IHttpClientFactory clientFactory,
IOptions<OedSettings> oedEventsSettings)
{
_logger = loggerFactory.CreateLogger<FeedPoller>();
_oedSettings = oedEventsSettings.Value;
_clientFactory = clientFactory;
}
[Function(nameof(FeedPoller))]
public async Task RunAsync([TimerTrigger("*/5 * * * *")] TimerInfo timerInfo)
{
_logger.LogDebug("DA feed import executed at: {Now}", DateTime.Now);
if (timerInfo.IsPastDue)
{
_logger.LogWarning("DA feed import was not run on schedule");
}
if (!Helpers.ShouldRunUpdate())
{
_logger.LogDebug("Skipping update outside of busy hours");
return;
}
if (Uri.IsWellFormedUriString(_oedSettings.OedEventsBaseUrl, UriKind.Absolute))
{
await PerformFeedPollAndUpdate();
}
else
{
_logger.LogError("Invalid configuration for OedEventsBaseUrl, should be an absolute url");
}
_logger.LogInformation("Next timer schedule at: {Next}", timerInfo.ScheduleStatus?.Next);
}
private async Task PerformFeedPollAndUpdate()
{
HttpClient httpClient = _clientFactory.CreateClient(Constants.EventsHttpClient);
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string url = _oedSettings.OedEventsBaseUrl?.TrimEnd('/') + "/process";
HttpResponseMessage result = await httpClient.PostAsync(url, null);
if (!result.IsSuccessStatusCode)
{
_logger.LogError("Failed to trigger processing of DA event feed - POST {Url}, status code: {StatusCode}. Message: {Message}",
url, result.StatusCode, await result.Content.ReadAsStringAsync());
}
}
}