aboutsummaryrefslogtreecommitdiffstats
path: root/src/BackgroundServices
diff options
context:
space:
mode:
Diffstat (limited to 'src/BackgroundServices')
-rw-r--r--src/BackgroundServices/AssetsCleanupBackgroundService.cs61
-rw-r--r--src/BackgroundServices/StartupTasksBackgroundService.cs35
-rw-r--r--src/BackgroundServices/VippsOrderStatusCheckerBackgroundService.cs59
3 files changed, 155 insertions, 0 deletions
diff --git a/src/BackgroundServices/AssetsCleanupBackgroundService.cs b/src/BackgroundServices/AssetsCleanupBackgroundService.cs
new file mode 100644
index 0000000..effb5d7
--- /dev/null
+++ b/src/BackgroundServices/AssetsCleanupBackgroundService.cs
@@ -0,0 +1,61 @@
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using VSH.Services;
+
+namespace VSH.BackgroundServices;
+
+/// <summary>
+/// This service is not really necessary, but it is nice to have for future reference.
+/// The job that this service is doing, could maybe become an http endpoint?
+/// </summary>
+public class AssetsCleanupBackgroundService : BackgroundService
+{
+ private readonly IServiceProvider _serviceProvider;
+ private readonly ILogger<AssetsCleanupBackgroundService> _logger;
+
+ private static TimeSpan CleanupInterval => TimeSpan.FromSeconds(10);
+
+ public AssetsCleanupBackgroundService(
+ IServiceProvider serviceProvider,
+ ILogger<AssetsCleanupBackgroundService> logger
+ ) {
+ _serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
+ _logger = logger;
+ }
+
+ protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
+ await StartInternalAsync(stoppingToken);
+ }
+
+ private async Task StartInternalAsync(CancellationToken cancellationToken) {
+ while (true) {
+ if (cancellationToken.IsCancellationRequested) {
+ _logger.LogDebug("CancellationRequested... Exiting");
+ break;
+ }
+
+ try {
+ await Task.Delay(CleanupInterval, cancellationToken);
+ } catch (TaskCanceledException) {
+ _logger.LogDebug("TaskCanceledException... Exiting");
+ break;
+ } catch (Exception ex) {
+ _logger.LogError($"Task.Delay exception: {ex.Message}. Exiting.");
+ break;
+ }
+
+ if (cancellationToken.IsCancellationRequested) {
+ _logger.LogDebug("CancellationRequested... Exiting");
+ break;
+ }
+
+ using var serviceProvider = _serviceProvider.CreateScope();
+ var assetsService = serviceProvider.ServiceProvider.GetRequiredService<AssetsService>();
+ await assetsService.RemoveUnusedProductImages();
+ }
+ }
+}
diff --git a/src/BackgroundServices/StartupTasksBackgroundService.cs b/src/BackgroundServices/StartupTasksBackgroundService.cs
new file mode 100644
index 0000000..8aebee9
--- /dev/null
+++ b/src/BackgroundServices/StartupTasksBackgroundService.cs
@@ -0,0 +1,35 @@
+using System.Collections.Generic;
+using System.IO;
+using System.Threading;
+using System.Threading.Tasks;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using VSH.Data.Static;
+
+namespace VSH.BackgroundServices;
+
+public class StartupTasksBackgroundService : BackgroundService
+{
+ private readonly ILogger<StartupTasksBackgroundService> _logger;
+
+ private static IEnumerable<string> PathsToEnsureCreated => new List<string> {
+ AppPaths.ProductImages.HostPath,
+ AppPaths.DocumentImages.HostPath,
+ };
+
+ public StartupTasksBackgroundService(ILogger<StartupTasksBackgroundService> logger) {
+ _logger = logger;
+ }
+
+ protected override Task ExecuteAsync(CancellationToken stoppingToken) {
+ EnsureCreated();
+ return Task.CompletedTask;
+ }
+
+ private void EnsureCreated() {
+ foreach (var path in PathsToEnsureCreated) {
+ Directory.CreateDirectory(path);
+ _logger.LogInformation("EnsuredCreated: " + path);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/BackgroundServices/VippsOrderStatusCheckerBackgroundService.cs b/src/BackgroundServices/VippsOrderStatusCheckerBackgroundService.cs
new file mode 100644
index 0000000..73804d2
--- /dev/null
+++ b/src/BackgroundServices/VippsOrderStatusCheckerBackgroundService.cs
@@ -0,0 +1,59 @@
+using System;
+using System.Linq;
+using System.Threading;
+using System.Threading.Tasks;
+using IOL.VippsEcommerce;
+using IOL.VippsEcommerce.Models.Api;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Hosting;
+using Microsoft.Extensions.Logging;
+using VSH.Data;
+using VSH.Data.Enums;
+
+namespace VSH.BackgroundServices;
+
+public class VippsOrderStatusCheckerBackgroundService : BackgroundService
+{
+ private readonly ILogger<VippsOrderStatusCheckerBackgroundService> _logger;
+
+ public VippsOrderStatusCheckerBackgroundService(
+ IServiceProvider services,
+ ILogger<VippsOrderStatusCheckerBackgroundService> logger
+ ) {
+ Services = services;
+ _logger = logger;
+ }
+
+ public IServiceProvider Services { get; }
+
+ protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
+ using var scope = Services.CreateScope();
+ var context =
+ scope.ServiceProvider
+ .GetRequiredService<MainDbContext>();
+ var vipps =
+ scope.ServiceProvider
+ .GetService<VippsEcommerceService>();
+
+ if (vipps == default) {
+ return;
+ }
+
+ foreach (var order in context.Orders.Where(c => c.Created < DateTime.UtcNow.AddDays(1)
+ && c.Status == OrderStatus.AWAITING_VIPPS)) {
+ try {
+ var vippsResponse = await vipps.GetPaymentDetailsAsync(order.OrderReference, stoppingToken);
+ order.VippsTransactionId = vippsResponse.TransactionLogHistory.LastOrDefault()?.TransactionId;
+ } catch (Exception e) {
+ if (e is VippsRequestException vippsRequestException) {
+ Console.WriteLine(vippsRequestException);
+ }
+
+ Console.WriteLine(e);
+ }
+
+ await context.SaveChangesAsync(stoppingToken);
+ _logger.LogInformation("Got payment details from vipps for order with id: " + order.Id);
+ }
+ }
+} \ No newline at end of file