Skip to content

Commit

Permalink
Added background worker hosted service
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Oct 25, 2023
1 parent 8e6f3e2 commit c280adc
Show file tree
Hide file tree
Showing 30 changed files with 1,461 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ public class FlightRecorderFactory
private readonly Lazy<IUserManager> _users = null;
private readonly Lazy<ICountryManager> _countries = null;
private readonly Lazy<IAirportManager> _airports = null;
private readonly Lazy<IJobStatusManager> _jobStatuses = null;
private readonly Lazy<IDateBasedReport<AirlineStatistics>> _airlineStatistics = null;
private readonly Lazy<IDateBasedReport<LocationStatistics>> _locationStatistics = null;
private readonly Lazy<IDateBasedReport<ManufacturerStatistics>> _manufacturerStatistics = null;
private readonly Lazy<IDateBasedReport<ModelStatistics>> _modelStatistics = null;
private readonly Lazy<IDateBasedReport<FlightsByMonth>> _flightsByMonth = null;

public FlightRecorderDbContext Context { get; private set; }

public IAirlineManager Airlines { get { return _airlines.Value; } }
public ILocationManager Locations { get { return _locations.Value; } }
public IManufacturerManager Manufacturers { get { return _manufacturers.Value; } }
Expand All @@ -35,6 +38,7 @@ public class FlightRecorderFactory
public IUserManager Users { get { return _users.Value; } }
public ICountryManager Countries { get { return _countries.Value; } }
public IAirportManager Airports { get { return _airports.Value; } }
public IJobStatusManager JobStatuses { get { return _jobStatuses.Value; } }

[ExcludeFromCodeCoverage]
public IDateBasedReport<AirlineStatistics> AirlineStatistics { get { return _airlineStatistics.Value; } }
Expand All @@ -51,10 +55,13 @@ public class FlightRecorderFactory
[ExcludeFromCodeCoverage]
public IDateBasedReport<FlightsByMonth> FlightsByMonth { get { return _flightsByMonth.Value; } }


public FlightRecorderFactory(FlightRecorderDbContext context)
{
// Store the database context
Context = context;

// Lazily instantiate the database managers : They'll only actually be created if called by
// the application
_airlines = new Lazy<IAirlineManager>(() => new AirlineManager(context));
_locations = new Lazy<ILocationManager>(() => new LocationManager(context));
_manufacturers = new Lazy<IManufacturerManager>(() => new ManufacturerManager(context));
Expand All @@ -65,6 +72,10 @@ public FlightRecorderFactory(FlightRecorderDbContext context)
_users = new Lazy<IUserManager>(() => new UserManager(context));
_countries = new Lazy<ICountryManager>(() => new CountryManager(context));
_airports = new Lazy<IAirportManager>(() => new AirportManager(this));
_jobStatuses = new Lazy<IJobStatusManager>(() => new JobStatusManager(context));

// Lazily instantiate the reporting managers : Once again, they'll only actually be created if called by
// the application
_airlineStatistics = new Lazy<IDateBasedReport<AirlineStatistics>>(() => new DateBasedReport<AirlineStatistics>(context));
_locationStatistics = new Lazy<IDateBasedReport<LocationStatistics>>(() => new DateBasedReport<LocationStatistics>(context));
_manufacturerStatistics = new Lazy<IDateBasedReport<ManufacturerStatistics>>(() => new DateBasedReport<ManufacturerStatistics>(context));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PackageId>FlightRecorder.BusinessLogic</PackageId>
<PackageVersion>1.1.4.0</PackageVersion>
<PackageVersion>1.2.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2020, 2021, 2022, 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -16,7 +16,7 @@
<PackageProjectUrl>https://github.com/davewalker5/FlightRecorderDb</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.1.4.0</ReleaseVersion>
<ReleaseVersion>1.2.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
37 changes: 37 additions & 0 deletions src/FlightRecorder.BusinessLogic/Logic/BackgroundQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Concurrent;

namespace FlightRecorder.BusinessLogic.Logic
{
public class BackgroundQueue<T> : IBackgroundQueue<T> where T : class
{
private readonly ConcurrentQueue<T> _queue = new();

/// <summary>
/// Add a new item to the concurrent queue
/// </summary>
/// <param name="item"></param>
/// <exception cref="ArgumentNullException"></exception>
public void Enqueue(T item)
{
if (item != null)
{
_queue.Enqueue(item);
}
else
{
throw new ArgumentNullException(nameof(item));
}
}

/// <summary>
/// De-queue an item
/// </summary>
/// <returns></returns>
public T Dequeue()
{
var successful = _queue.TryDequeue(out T item);
return successful ? item : null;
}
}
}
84 changes: 84 additions & 0 deletions src/FlightRecorder.BusinessLogic/Logic/BackgroundQueueProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using FlightRecorder.BusinessLogic.Factory;
using FlightRecorder.Entities.DataExchange;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;

namespace FlightRecorder.BusinessLogic.Logic
{
[ExcludeFromCodeCoverage]
public abstract class BackgroundQueueProcessor<T> : BackgroundService where T : BackgroundWorkItem
{
private const int ProcessingLoopDelay = 500;

private readonly IBackgroundQueue<T> _queue;

protected IServiceScopeFactory ServiceScopeFactory { get; private set; }

protected BackgroundQueueProcessor(IBackgroundQueue<T> queue, IServiceScopeFactory serviceScopeFactory)
{
_queue = queue;
ServiceScopeFactory = serviceScopeFactory;
}

/// <summary>
/// Method called to process the work placed into the queue
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
protected override async Task ExecuteAsync(CancellationToken token)

Check warning on line 32 in src/FlightRecorder.BusinessLogic/Logic/BackgroundQueueProcessor.cs

View workflow job for this annotation

GitHub Actions / build

Rename parameter 'token' to 'stoppingToken' to match the base class declaration.

Check warning on line 32 in src/FlightRecorder.BusinessLogic/Logic/BackgroundQueueProcessor.cs

View workflow job for this annotation

GitHub Actions / build

Rename parameter 'token' to 'stoppingToken' to match the base class declaration.
{
while (token.IsCancellationRequested)
{
// Wait, so there's not a busy loop, then get the next work item from the queue
await Task.Delay(ProcessingLoopDelay, token);
var item = _queue.Dequeue();

// Item may be null if there's nothing in the queue or there's a de-queuing error, so
// check it's valid
if (item != null)
{
// Create a dependency resolution scope
using (var scope = ServiceScopeFactory.CreateScope())
{
// Get a scoped instance of the flight recorder factory
var factory = scope.ServiceProvider.GetService<FlightRecorderFactory>();

// Create the job status record
var status = await factory.JobStatuses.AddAsync(item.JobName, null);

try
{
// Process the work item and, if successful, complete the job status record with
// no error
await ProcessWorkItem(item, factory);
await factory.JobStatuses.UpdateAsync(status.Id, null);
}
catch (Exception ex)
{
// Got an error during processing, so complete the job status record with the
// exception details
await factory.JobStatuses.UpdateAsync(status.Id, ex.ToString());
}
}
}
}
}

/// <summary>
/// Process the specified work item from the queue
/// </summary>
/// <param name="item"></param>
#pragma warning disable CS1998
protected virtual async Task ProcessWorkItem(T item, FlightRecorderFactory factory)
{
// Ideally, this would be an abstract method with no body but that's not possible with
// async methods so it's declared with an empty body in the expectation that the child
// classes will override it
}
#pragma warning restore CS1998
}
}
98 changes: 98 additions & 0 deletions src/FlightRecorder.BusinessLogic/Logic/JobStatusManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using FlightRecorder.BusinessLogic.Extensions;
using FlightRecorder.Data;
using FlightRecorder.Entities.Db;
using FlightRecorder.Entities.Interfaces;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace FlightRecorder.BusinessLogic.Logic
{
internal class JobStatusManager : IJobStatusManager
{
private readonly FlightRecorderDbContext _context;

internal JobStatusManager(FlightRecorderDbContext context)
{
_context = context;
}

/// <summary>
/// Get the first job status matching the specified criteria
/// </summary>
/// <param name="predicate"></param>
/// <returns></returns>
public async Task<JobStatus> GetAsync(Expression<Func<JobStatus, bool>> predicate)
{
List<JobStatus> statuses = await ListAsync(predicate, 1, 1).ToListAsync();
return statuses.FirstOrDefault();
}

/// <summary>
/// Return all entities matching the specified criteria
/// </summary>
/// <param name="predicate"></param>
/// <param name="pageNumber"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public IAsyncEnumerable<JobStatus> ListAsync(Expression<Func<JobStatus, bool>> predicate, int pageNumber, int pageSize)
{
IAsyncEnumerable<JobStatus> results;
if (predicate == null)
{
results = _context.JobStatuses
.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.AsAsyncEnumerable();
}
else
{
results = _context.JobStatuses.Where(predicate).AsAsyncEnumerable();
}

return results;
}

/// <summary>
/// Create a new job status
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public async Task<JobStatus> AddAsync(string name, string parameters)
{
JobStatus status = new JobStatus
{
Name = name.CleanString(),
Parameters = parameters.CleanString(),
Start = DateTime.Now
};

await _context.JobStatuses.AddAsync(status);
await _context.SaveChangesAsync();

return status;
}

/// <summary>
/// Update a job status, setting the end timestamp and error result
/// </summary>
/// <param name="id"></param>
/// <param name="error"></param>
/// <returns></returns>
public async Task<JobStatus> UpdateAsync(long id, string error)
{
JobStatus status = await GetAsync(x => x.Id == id);
if (status != null)
{
status.End = DateTime.Now;
status.Error = error.CleanString();
await _context.SaveChangesAsync();
}

return status;
}
}
}
4 changes: 2 additions & 2 deletions src/FlightRecorder.Data/FlightRecorder.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<PackageId>FlightRecorder.Data</PackageId>
<PackageVersion>1.1.4.0</PackageVersion>
<PackageVersion>1.2.0.0</PackageVersion>
<Authors>Dave Walker</Authors>
<Copyright>Copyright (c) Dave Walker 2020, 2021, 2022, 2023</Copyright>
<Owners>Dave Walker</Owners>
Expand All @@ -16,7 +16,7 @@
<PackageProjectUrl>https://github.com/davewalker5/FlightRecorderDb</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<ReleaseVersion>1.1.4.0</ReleaseVersion>
<ReleaseVersion>1.2.0.0</ReleaseVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions src/FlightRecorder.Data/FlightRecorderDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public partial class FlightRecorderDbContext : DbContext
public virtual DbSet<Manufacturer> Manufacturers { get; set; }
public virtual DbSet<Model> Models { get; set; }
public virtual DbSet<Sighting> Sightings { get; set; }
public virtual DbSet<JobStatus> JobStatuses { get; set; }
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<AirlineStatistics> AirlineStatistics { get; set; }
public virtual DbSet<LocationStatistics> LocationStatistics { get; set; }
Expand Down Expand Up @@ -203,6 +204,20 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.Property(e => e.LocationId).HasColumnName("location_id");
});

modelBuilder.Entity<JobStatus>(entity =>
{
entity.ToTable("JOB_STATUS");
entity.Property(e => e.Id)
.HasColumnName("id")
.ValueGeneratedOnAdd();
entity.Property(e => e.Name).IsRequired().HasColumnName("name");
entity.Property(e => e.Parameters).HasColumnName("parameters");
entity.Property(e => e.Start).IsRequired().HasColumnName("start").HasColumnType("DATETIME");
entity.Property(e => e.End).HasColumnName("end").HasColumnType("DATETIME");
entity.Property(e => e.Error).HasColumnName("error");
});

modelBuilder.Entity<User>(entity =>
{
Expand Down
Loading

0 comments on commit c280adc

Please sign in to comment.