-
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.
- Loading branch information
Farshad DASHTI
authored and
Farshad DASHTI
committed
Oct 2, 2024
1 parent
3d1d0a5
commit e18bd4b
Showing
9 changed files
with
157 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: .NET Build and Test for DfE.CoreLibs.BackgroundService | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'src/DfE.CoreLibs.BackgroundService/**' | ||
|
||
jobs: | ||
build-and-test: | ||
uses: ./.github/workflows/build-test-template.yml | ||
with: | ||
project_name: DfE.CoreLibs.BackgroundService | ||
project_path: src/DfE.CoreLibs.BackgroundService | ||
sonar_project_key: DFE-Digital_corelibs-backgroundService | ||
secrets: | ||
GITHUB__TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
SONAR__TOKEN: ${{ secrets.SONAR_TOKEN }} |
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,15 @@ | ||
name: Build and Push NuGet Package for DfE.CoreLibs.BackgroundService | ||
|
||
on: | ||
workflow_run: | ||
workflows: [".NET Build and Test for DfE.CoreLibs.BackgroundService"] | ||
types: | ||
- completed | ||
|
||
jobs: | ||
build-and-package: | ||
uses: ./.github/workflows/nuget-package-template.yml | ||
with: | ||
project_name: DfE.CoreLibs.BackgroundService | ||
project_path: src/DfE.CoreLibs.BackgroundService | ||
nuget_package_name: DfE.CoreLibs.BackgroundService |
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
16 changes: 16 additions & 0 deletions
16
src/DfE.CoreLibs.BackgroundService/DfE.CoreLibs.BackgroundService.csproj
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,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="MediatR" Version="12.4.1" /> | ||
<PackageReference Include="MediatR.Contracts" Version="2.0.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
src/DfE.CoreLibs.BackgroundService/Interfaces/IBackgroundServiceEvent.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using MediatR; | ||
|
||
namespace DfE.CoreLibs.BackgroundService.Interfaces | ||
{ | ||
public interface IBackgroundServiceEvent : INotification | ||
{ | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/DfE.CoreLibs.BackgroundService/Interfaces/IBackgroundServiceEventHandler.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using MediatR; | ||
|
||
namespace DfE.CoreLibs.BackgroundService.Interfaces | ||
{ | ||
public interface IBackgroundServiceEventHandler<in TEvent> : INotificationHandler<TEvent> where TEvent : IBackgroundServiceEvent | ||
{ | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/DfE.CoreLibs.BackgroundService/Interfaces/IBackgroundServiceFactory.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace DfE.CoreLibs.BackgroundService.Interfaces | ||
{ | ||
public interface IBackgroundServiceFactory | ||
{ | ||
void EnqueueTask<TResult, TEvent>(Func<Task<TResult>> taskFunc, Func<TResult, TEvent>? eventFactory = null) | ||
where TEvent : IBackgroundServiceEvent; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/DfE.CoreLibs.BackgroundService/ServiceCollectionExtensions.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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using DfE.CoreLibs.BackgroundService.Interfaces; | ||
using DfE.CoreLibs.BackgroundService.Services; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class ServiceCollectionExtensions | ||
{ | ||
public static IServiceCollection AddBackgroundService(this IServiceCollection services) | ||
{ | ||
services.AddSingleton<IBackgroundServiceFactory, BackgroundServiceFactory>(); | ||
services.AddHostedService<BackgroundServiceFactory>(); | ||
|
||
return services; | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/DfE.CoreLibs.BackgroundService/Services/BackgroundServiceFactory.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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Collections.Concurrent; | ||
using DfE.CoreLibs.BackgroundService.Interfaces; | ||
using MediatR; | ||
|
||
namespace DfE.CoreLibs.BackgroundService.Services | ||
{ | ||
public class BackgroundServiceFactory(IMediator mediator) : Microsoft.Extensions.Hosting.BackgroundService, IBackgroundServiceFactory | ||
{ | ||
private readonly ConcurrentDictionary<Type, ConcurrentQueue<Func<Task>>> _taskQueues = new(); | ||
private readonly ConcurrentDictionary<Type, SemaphoreSlim> _semaphores = new(); | ||
|
||
public void EnqueueTask<TResult, TEvent>(Func<Task<TResult>> taskFunc, Func<TResult, TEvent>? eventFactory = null) | ||
where TEvent : IBackgroundServiceEvent | ||
{ | ||
var taskType = taskFunc.GetType(); | ||
var queue = _taskQueues.GetOrAdd(taskType, new ConcurrentQueue<Func<Task>>()); | ||
_semaphores.GetOrAdd(taskType, new SemaphoreSlim(1, 1)); | ||
|
||
queue.Enqueue(async () => | ||
{ | ||
var result = await taskFunc(); | ||
|
||
if (eventFactory != null) | ||
{ | ||
var taskCompletedEvent = eventFactory.Invoke(result); | ||
await mediator.Publish(taskCompletedEvent); | ||
} | ||
}); | ||
|
||
_ = StartProcessingQueue(taskType); | ||
} | ||
|
||
private async Task StartProcessingQueue(Type taskType) | ||
{ | ||
var queue = _taskQueues[taskType]; | ||
var semaphore = _semaphores[taskType]; | ||
|
||
await semaphore.WaitAsync(); | ||
|
||
try | ||
{ | ||
while (queue.TryDequeue(out var taskToProcess)) | ||
{ | ||
await taskToProcess(); | ||
} | ||
} | ||
finally | ||
{ | ||
semaphore.Release(); | ||
} | ||
} | ||
|
||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
while (!stoppingToken.IsCancellationRequested) | ||
{ | ||
await Task.Delay(1000, stoppingToken); | ||
} | ||
} | ||
} | ||
} |