-
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
Showing
9 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
.github/workflows/release-messagebroker-rabbit-package.yml
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,27 @@ | ||
name: SOFTURE MESSAGE BROKER - RABBIT - RELEASE NEW VERSION TO NUGET.ORG | ||
|
||
on: | ||
release: | ||
types: [released] | ||
|
||
jobs: | ||
publishing: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: ⚙️ Install dotnet | ||
uses: actions/setup-dotnet@v1 | ||
with: | ||
dotnet-version: 8.0.100 | ||
|
||
- name: 🔗 Restore dependencies | ||
run: dotnet restore ./API | ||
|
||
- name: 📂 Create new nuget package | ||
run: dotnet pack --no-restore -c Release -o ./artifacts /p:PackageVersion=${{ github.ref_name }} /p:Version=${{ github.ref_name }} ./API/SOFTURE.MessageBroker.Rabbit/SOFTURE..MessageBroker.Rabbit.csproj | ||
continue-on-error: false | ||
|
||
- name: 🚀 Push new nuget package | ||
run: dotnet nuget push ./artifacts/SOFTURE.MessageBroker.Rabbit.${{ github.ref_name }}.nupkg -k ${{ secrets.NUGET_API_KEY }} -s https://api.nuget.org/v3/index.json |
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
100 changes: 100 additions & 0 deletions
100
API/SOFTURE.MessageBroker.Rabbit/DependencyInjection.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,100 @@ | ||
using System.Reflection; | ||
using MassTransit; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SOFTURE.Common.HealthCheck; | ||
using SOFTURE.MessageBroker.Rabbit.Filters; | ||
using SOFTURE.MessageBroker.Rabbit.HealthChecks; | ||
using SOFTURE.MessageBroker.Rabbit.Settings; | ||
|
||
namespace SOFTURE.MessageBroker.Rabbit | ||
{ | ||
public static class DependencyInjection | ||
{ | ||
public static IServiceCollection AddCommonPublisher<TSettings>(this IServiceCollection services) | ||
where TSettings : IRabbitSettings | ||
{ | ||
services.AddMassTransit(config => | ||
{ | ||
config.UsingRabbitMq((ctx, cfg) => | ||
{ | ||
var publisherSettings = ctx.GetRequiredService<TSettings>().Rabbit; | ||
|
||
cfg.UseInMemoryOutbox(ctx); | ||
|
||
cfg.UsePublishFilter(typeof(ContextPublishLoggingFilter<>), ctx); | ||
|
||
cfg.Host(publisherSettings.Url, h => | ||
{ | ||
h.ConfigureBatchPublish(bcfg => | ||
{ | ||
bcfg.Enabled = true; | ||
bcfg.MessageLimit = 100; | ||
bcfg.SizeLimit = 10000; | ||
bcfg.Timeout = TimeSpan.FromMilliseconds(30); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
return services; | ||
} | ||
|
||
public static IServiceCollection AddCommonConsumers<TSettings>(this IServiceCollection services, Assembly assembly) | ||
where TSettings : IRabbitSettings | ||
{ | ||
var consumerTypes = GetConsumers(assembly); | ||
|
||
services.AddMassTransit(config => | ||
{ | ||
foreach (var type in consumerTypes) | ||
{ | ||
config.AddConsumer(type); | ||
} | ||
|
||
config.UsingRabbitMq((ctx, cfg) => | ||
{ | ||
var consumerSettings = ctx.GetRequiredService<TSettings>().Rabbit; | ||
|
||
cfg.UseInMemoryOutbox(ctx); | ||
|
||
cfg.UseConsumeFilter(typeof(ContextConsumeLoggingFilter<>), ctx); | ||
|
||
cfg.Host(consumerSettings.Url, h => | ||
{ | ||
h.ConfigureBatchPublish(bcfg => | ||
{ | ||
bcfg.Enabled = true; | ||
bcfg.MessageLimit = 100; | ||
bcfg.SizeLimit = 10000; | ||
bcfg.Timeout = TimeSpan.FromMilliseconds(30); | ||
}); | ||
}); | ||
|
||
cfg.ReceiveEndpoint(consumerSettings.Name, c => | ||
{ | ||
foreach (var type in consumerTypes) | ||
{ | ||
c.ConfigureConsumer(ctx, type); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
services.AddCommonHealthCheck<MessageBrokerHealthCheck>(); | ||
|
||
return services; | ||
} | ||
|
||
private static List<Type> GetConsumers(Assembly assembly) | ||
{ | ||
var consumerTypes = assembly.GetTypes() | ||
.Where(t => | ||
t.GetInterfaces().Any(i => | ||
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IConsumer<>)) && | ||
!t.IsAbstract) | ||
.ToList(); | ||
|
||
return consumerTypes; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
API/SOFTURE.MessageBroker.Rabbit/Filters/ContextConsumeLoggingFilter.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,36 @@ | ||
using MassTransit; | ||
using SOFTURE.Common.Correlation.Consts; | ||
using SOFTURE.Common.Correlation.Generators; | ||
using SOFTURE.Common.Correlation.Providers; | ||
using SOFTURE.Common.Correlation.ValueObjects; | ||
using LogContext = Serilog.Context.LogContext; | ||
|
||
namespace SOFTURE.MessageBroker.Rabbit.Filters; | ||
|
||
public class ContextConsumeLoggingFilter<T>(ICorrelationProvider correlationProvider) : IFilter<ConsumeContext<T>> | ||
where T : class | ||
{ | ||
public void Probe(ProbeContext context) | ||
{ | ||
context.CreateFilterScope(nameof(ContextConsumeLoggingFilter<T>)); | ||
} | ||
|
||
public async Task Send(ConsumeContext<T> context, IPipe<ConsumeContext<T>> next) | ||
{ | ||
var correlationId = GetCorrelationId(context) ?? CorrelationGenerator.Generate(); | ||
|
||
correlationProvider.Set(CorrelationId.Parse(correlationId)); | ||
|
||
using (LogContext.PushProperty("CorrelationId", correlationId)) | ||
{ | ||
await next.Send(context); | ||
} | ||
} | ||
|
||
private static string? GetCorrelationId(ConsumeContext context) | ||
{ | ||
context.Headers.TryGetHeader(CorrelationConsts.CorrelationHeader, out var correlationId); | ||
|
||
return correlationId?.ToString(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
API/SOFTURE.MessageBroker.Rabbit/Filters/ContextPublishLoggingFilter.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,25 @@ | ||
using MassTransit; | ||
using SOFTURE.Common.Correlation.Consts; | ||
using SOFTURE.Common.Correlation.Providers; | ||
|
||
namespace SOFTURE.MessageBroker.Rabbit.Filters; | ||
|
||
public class ContextPublishLoggingFilter<T>(ICorrelationProvider correlationProvider) : IFilter<PublishContext<T>> | ||
where T : class | ||
{ | ||
public void Probe(ProbeContext context) | ||
{ | ||
context.CreateFilterScope(nameof(ContextPublishLoggingFilter<T>)); | ||
} | ||
|
||
public async Task Send(PublishContext<T> context, IPipe<PublishContext<T>> next) | ||
{ | ||
var correlationId = correlationProvider.Get(); | ||
if (correlationId is not null) | ||
{ | ||
context.Headers.Set(CorrelationConsts.CorrelationHeader, correlationId); | ||
} | ||
|
||
await next.Send(context); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
API/SOFTURE.MessageBroker.Rabbit/HealthChecks/MessageBrokerHealthCheck.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,17 @@ | ||
using CSharpFunctionalExtensions; | ||
using MassTransit; | ||
using SOFTURE.Common.HealthCheck.Core; | ||
|
||
namespace SOFTURE.MessageBroker.Rabbit.HealthChecks; | ||
|
||
internal class MessageBrokerHealthCheck(IPublishEndpoint publishEndpoint) : CheckBase | ||
{ | ||
protected override async Task<Result> Check() | ||
{ | ||
await publishEndpoint.Publish(new HealthCheckMessage(), Cts.Token); | ||
|
||
return Result.Success(); | ||
} | ||
|
||
private record HealthCheckMessage; | ||
} |
54 changes: 54 additions & 0 deletions
54
API/SOFTURE.MessageBroker.Rabbit/SOFTURE.MessageBroker.Rabbit.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,54 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net8.0</TargetFrameworks> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' "> | ||
<PackageReference Include="MassTransit" Version="8.2.4"/> | ||
<PackageReference Include="MassTransit.RabbitMQ" Version="8.2.4"/> | ||
<PackageReference Include="Serilog" Version="4.0.1" /> | ||
<PackageReference Include="SOFTURE.Common.Correlation" Version="0.0.5" /> | ||
<PackageReference Include="SOFTURE.Common.HealthCheck" Version="0.0.5" /> | ||
<PackageReference Include="SOFTURE.Results" Version="0.0.9" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' "> | ||
<PackageReference Include="MassTransit" Version="8.2.4"/> | ||
<PackageReference Include="MassTransit.RabbitMQ" Version="8.2.4"/> | ||
<PackageReference Include="Serilog" Version="4.0.1" /> | ||
<PackageReference Include="SOFTURE.Common.Correlation" Version="0.0.5" /> | ||
<PackageReference Include="SOFTURE.Common.HealthCheck" Version="0.0.5" /> | ||
<PackageReference Include="SOFTURE.Results" Version="0.0.9" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<AssemblyName>SOFTURE.MessageBroker.Rabbit</AssemblyName> | ||
<AssemblyTitle>$(AssemblyName)</AssemblyTitle> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<LangVersion>latest</LangVersion> | ||
<Title>$(AssemblyName)</Title> | ||
<Authors>SOFTURE</Authors> | ||
<Copyright>Copyright (c) 2024 $(Authors)</Copyright> | ||
<Description>SOFTURE - MessageBroker - Rabbit</Description> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<PackageId>$(AssemblyName)</PackageId> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageProjectUrl>https://github.com/SOFTURE/API</PackageProjectUrl> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<PackageReleaseNotes>See $(PackageProjectUrl)/blob/master/CHANGELOG.md for release notes.</PackageReleaseNotes> | ||
<PackageTags>SOFTURE</PackageTags> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<RepositoryType>Git</RepositoryType> | ||
<RepositoryUrl>$(PackageProjectUrl)</RepositoryUrl> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\README.md" Pack="true" PackagePath="\"/> | ||
<None Include="..\..\LICENSE" Pack="true" PackagePath="\"/> | ||
</ItemGroup> | ||
|
||
</Project> |
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,6 @@ | ||
namespace SOFTURE.MessageBroker.Rabbit.Settings; | ||
|
||
public interface IRabbitSettings | ||
{ | ||
RabbitSettings Rabbit { get; init; } | ||
} |
14 changes: 14 additions & 0 deletions
14
API/SOFTURE.MessageBroker.Rabbit/Settings/RabbitSettings.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,14 @@ | ||
namespace SOFTURE.MessageBroker.Rabbit.Settings; | ||
|
||
public sealed class RabbitSettings | ||
{ | ||
#if NET8_0 | ||
public required string Name { get; init; } | ||
public required string Url { get; init; } | ||
#endif | ||
|
||
#if NET6_0 | ||
public string Name { get; init; } = null!; | ||
public string Url { get; init; } = null!; | ||
#endif | ||
} |