-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #193 from sebader/dev
Update data fetchers to net8
- Loading branch information
Showing
9 changed files
with
316 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 19 additions & 14 deletions
33
FetchDataFunctions/CleanupFreeBedNotificationSubscriptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data; | ||
using System.Linq; | ||
using FetchDataFunctions.Models; | ||
using Microsoft.Azure.WebJobs; | ||
using Microsoft.Azure.WebJobs.Host; | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Azure.Functions.Worker.Extensions.Sql; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace FetchDataFunctions.WebsiteFunctions | ||
namespace FetchDataFunctions | ||
{ | ||
public class CleanupFreeBedNotificationSubscriptions | ||
{ | ||
private readonly ILogger<CleanupFreeBedNotificationSubscriptions> _logger; | ||
|
||
public CleanupFreeBedNotificationSubscriptions(ILogger<CleanupFreeBedNotificationSubscriptions> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
/// <summary> | ||
/// On execution, it runs the cleanup stored procedure in the database which removes | ||
/// any free bed notification subscriptions which are now in the past | ||
/// </summary> | ||
/// <param name="myTimer"></param> | ||
/// <param name="result"></param> | ||
/// <param name="log"></param> | ||
[FunctionName(nameof(CleanupFreeBedNotificationSubscriptions))] | ||
public void Run([TimerTrigger("0 0 1 * * * ")]TimerInfo myTimer, | ||
[Sql("[dbo].[DeleteOldFreeBedSubscriptions]", | ||
"DatabaseConnectionString", | ||
CommandType.StoredProcedure)] IEnumerable<FreeBedUpdateSubscription> result, | ||
ILogger log) | ||
[Function(nameof(CleanupFreeBedNotificationSubscriptions))] | ||
public void Run([TimerTrigger("0 0 1 * * * ")] TimerInfo myTimer, | ||
[SqlInput("[dbo].[DeleteOldFreeBedSubscriptions]", | ||
"DatabaseConnectionString", | ||
CommandType.StoredProcedure, "")] | ||
IEnumerable<FreeBedUpdateSubscription> result) | ||
{ | ||
log.LogInformation($"Free bed subscription cleanup timer trigger function executed at: {DateTime.Now}"); | ||
log.LogInformation($"Now {result.Count()} subscriptions are left in the database"); | ||
_logger.LogInformation("Free bed subscription cleanup timer trigger function executed"); | ||
_logger.LogInformation($"Now {result.Count()} subscriptions are left in the database"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using Microsoft.Azure.Functions.Worker; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Polly; | ||
using Polly.Extensions.Http; | ||
using Polly.Timeout; | ||
|
||
namespace FetchDataFunctions; | ||
|
||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var host = new HostBuilder() | ||
.ConfigureFunctionsWebApplication() | ||
.ConfigureServices(services => | ||
{ | ||
services.AddApplicationInsightsTelemetryWorkerService(); | ||
services.ConfigureFunctionsApplicationInsights(); | ||
|
||
services | ||
.AddHttpClient<HttpClient>("HttpClient", client => | ||
{ | ||
var productValue = new ProductInfoHeaderValue("HutInfoScraperBot", "1.0"); | ||
client.DefaultRequestHeaders.UserAgent.Add(productValue); | ||
|
||
client.Timeout = | ||
TimeSpan.FromSeconds( | ||
120); // default overall request request timeout (includes all polly retries) | ||
}) | ||
.ConfigurePrimaryHttpMessageHandler(() => | ||
{ | ||
return new HttpClientHandler() | ||
{ | ||
UseCookies = | ||
false, // Prevent cookie sharing in multi thread env https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-5.0#cookies | ||
}; | ||
}) | ||
.AddPolicyHandler(GetRetryWithTimeoutPolicy()); | ||
}) | ||
.Build(); | ||
|
||
host.Run(); | ||
} | ||
|
||
static IAsyncPolicy<HttpResponseMessage> GetRetryWithTimeoutPolicy() | ||
{ | ||
var retryPolicy = HttpPolicyExtensions | ||
.HandleTransientHttpError() | ||
.OrResult(msg => | ||
msg.StatusCode == | ||
System.Net.HttpStatusCode.Forbidden) // Retry 403 as it seems to be some rate limiting error | ||
.Or<TimeoutRejectedException>() | ||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))) | ||
.WrapAsync(Policy.TimeoutAsync(30)); // per request timeout | ||
|
||
return retryPolicy; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.