From 40511043bc5de3525b83961c78a22e9312a5f8bc Mon Sep 17 00:00:00 2001 From: Filipe Esch Date: Tue, 30 Mar 2021 16:41:25 +0100 Subject: [PATCH] feat: creates KafkaFlow.Extensions.Hosting to run as Hosted Service --- .../KafkaFlow.Extensions.Hosting.csproj | 21 +++++++++++++++ .../KafkaFlowHostedService.cs | 18 +++++++++++++ .../ServiceCollectionExtensions.cs | 26 +++++++++++++++++++ src/KafkaFlow.sln | 7 +++++ 4 files changed, 72 insertions(+) create mode 100644 src/KafkaFlow.Extensions.Hosting/KafkaFlow.Extensions.Hosting.csproj create mode 100644 src/KafkaFlow.Extensions.Hosting/KafkaFlowHostedService.cs create mode 100644 src/KafkaFlow.Extensions.Hosting/ServiceCollectionExtensions.cs diff --git a/src/KafkaFlow.Extensions.Hosting/KafkaFlow.Extensions.Hosting.csproj b/src/KafkaFlow.Extensions.Hosting/KafkaFlow.Extensions.Hosting.csproj new file mode 100644 index 000000000..c1d0baa11 --- /dev/null +++ b/src/KafkaFlow.Extensions.Hosting/KafkaFlow.Extensions.Hosting.csproj @@ -0,0 +1,21 @@ + + + + netstandard2.0 + latest + KafkaFlow + KafkaFlow.Extensions.Hosting + Helper to run KafkaFlow as a Hosted Service + https://raw.githubusercontent.com/Farfetch/.github/master/images/fuse-logo-128.png + + + + + + + + + + + + diff --git a/src/KafkaFlow.Extensions.Hosting/KafkaFlowHostedService.cs b/src/KafkaFlow.Extensions.Hosting/KafkaFlowHostedService.cs new file mode 100644 index 000000000..7a08e6b68 --- /dev/null +++ b/src/KafkaFlow.Extensions.Hosting/KafkaFlowHostedService.cs @@ -0,0 +1,18 @@ +namespace KafkaFlow +{ + using System; + using System.Threading; + using System.Threading.Tasks; + using global::Microsoft.Extensions.Hosting; + + internal class KafkaFlowHostedService : IHostedService + { + private readonly IKafkaBus kafkaBus; + + public KafkaFlowHostedService(IServiceProvider serviceProvider) => this.kafkaBus = serviceProvider.CreateKafkaBus(); + + public Task StartAsync(CancellationToken cancellationToken) => this.kafkaBus.StartAsync(cancellationToken); + + public Task StopAsync(CancellationToken cancellationToken) => this.kafkaBus.StopAsync(); + } +} diff --git a/src/KafkaFlow.Extensions.Hosting/ServiceCollectionExtensions.cs b/src/KafkaFlow.Extensions.Hosting/ServiceCollectionExtensions.cs new file mode 100644 index 000000000..920fb2471 --- /dev/null +++ b/src/KafkaFlow.Extensions.Hosting/ServiceCollectionExtensions.cs @@ -0,0 +1,26 @@ +namespace KafkaFlow +{ + using System; + using global::Microsoft.Extensions.DependencyInjection; + using KafkaFlow.Configuration; + + /// + /// + public static class ServiceCollectionExtensions + { + /// + /// Configures KafkaFlow to run as a Hosted Service + /// + /// Instance of + /// A handler to configure KafkaFlow + /// + public static IServiceCollection AddKafkaFlowHostedService( + this IServiceCollection services, + Action kafka) + { + return services + .AddHostedService() + .AddKafka(kafka); + } + } +} diff --git a/src/KafkaFlow.sln b/src/KafkaFlow.sln index 409a02fa1..26e2277ba 100644 --- a/src/KafkaFlow.sln +++ b/src/KafkaFlow.sln @@ -65,6 +65,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KafkaFlow.BatchConsume", "K EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KafkaFlow.Sample.BatchConsume", "..\samples\KafkaFlow.Sample.BatchConsume\KafkaFlow.Sample.BatchConsume.csproj", "{DE8A8871-B19E-489D-8292-386A06A4CDFA}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KafkaFlow.Extensions.Hosting", "KafkaFlow.Extensions.Hosting\KafkaFlow.Extensions.Hosting.csproj", "{7913342E-80FD-4094-B892-18DAA2E6948F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -159,6 +161,10 @@ Global {DE8A8871-B19E-489D-8292-386A06A4CDFA}.Debug|Any CPU.Build.0 = Debug|Any CPU {DE8A8871-B19E-489D-8292-386A06A4CDFA}.Release|Any CPU.ActiveCfg = Release|Any CPU {DE8A8871-B19E-489D-8292-386A06A4CDFA}.Release|Any CPU.Build.0 = Release|Any CPU + {7913342E-80FD-4094-B892-18DAA2E6948F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7913342E-80FD-4094-B892-18DAA2E6948F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7913342E-80FD-4094-B892-18DAA2E6948F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7913342E-80FD-4094-B892-18DAA2E6948F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -188,6 +194,7 @@ Global {0A782A83-B66D-4B99-9BE2-2B18AAD2E03C} = {ED24B548-6F37-4283-A35B-F6015BFB7A34} {C891D0DB-BE19-4D20-9E2F-61D413210F8D} = {ED24B548-6F37-4283-A35B-F6015BFB7A34} {DE8A8871-B19E-489D-8292-386A06A4CDFA} = {303AE78F-6C96-4DF4-AC89-5C4FD53AFF0B} + {7913342E-80FD-4094-B892-18DAA2E6948F} = {068CB250-2804-4C7E-9490-17F432B9CE21} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {6AE955B5-16B0-41CF-9F12-66D15B3DD1AB}