Skip to content

Commit

Permalink
Added message broker common package
Browse files Browse the repository at this point in the history
  • Loading branch information
jarmatys committed Aug 13, 2024
1 parent 8899066 commit e3c52ed
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/release-messagebroker-rabbit-package.yml
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
6 changes: 6 additions & 0 deletions API/API.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.Common.HealthCheck"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.Common.Correlation", "SOFTURE.Common.Correlation\SOFTURE.Common.Correlation.csproj", "{8D236EE2-807F-4491-ADE3-B414A39AE8D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SOFTURE.MessageBroker.Rabbit", "SOFTURE.MessageBroker.Rabbit\SOFTURE.MessageBroker.Rabbit.csproj", "{D64C8209-20BA-4294-89B3-A67BB3B9F64C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -18,5 +20,9 @@ Global
{8D236EE2-807F-4491-ADE3-B414A39AE8D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D236EE2-807F-4491-ADE3-B414A39AE8D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D236EE2-807F-4491-ADE3-B414A39AE8D8}.Release|Any CPU.Build.0 = Release|Any CPU
{D64C8209-20BA-4294-89B3-A67BB3B9F64C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D64C8209-20BA-4294-89B3-A67BB3B9F64C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D64C8209-20BA-4294-89B3-A67BB3B9F64C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D64C8209-20BA-4294-89B3-A67BB3B9F64C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
100 changes: 100 additions & 0 deletions API/SOFTURE.MessageBroker.Rabbit/DependencyInjection.cs
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;
}
}
}
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();
}
}
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);
}
}
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;
}
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>
6 changes: 6 additions & 0 deletions API/SOFTURE.MessageBroker.Rabbit/Settings/IRabbitSettings.cs
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 API/SOFTURE.MessageBroker.Rabbit/Settings/RabbitSettings.cs
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
}

0 comments on commit e3c52ed

Please sign in to comment.