Skip to content

Commit

Permalink
feat: creates KafkaFlow.Extensions.Hosting to run as Hosted Service
Browse files Browse the repository at this point in the history
  • Loading branch information
filipeesch committed Mar 30, 2021
1 parent 2fc7985 commit 4051104
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>latest</LangVersion>
<RootNamespace>KafkaFlow</RootNamespace>
<PackageId>KafkaFlow.Extensions.Hosting</PackageId>
<Description>Helper to run KafkaFlow as a Hosted Service</Description>
<PackageIconUrl>https://raw.githubusercontent.com/Farfetch/.github/master/images/fuse-logo-128.png</PackageIconUrl>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\KafkaFlow.Microsoft.DependencyInjection\KafkaFlow.Microsoft.DependencyInjection.csproj" />
<ProjectReference Include="..\KafkaFlow\KafkaFlow.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="2.2.0" />
</ItemGroup>

</Project>
18 changes: 18 additions & 0 deletions src/KafkaFlow.Extensions.Hosting/KafkaFlowHostedService.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
26 changes: 26 additions & 0 deletions src/KafkaFlow.Extensions.Hosting/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace KafkaFlow
{
using System;
using global::Microsoft.Extensions.DependencyInjection;
using KafkaFlow.Configuration;

/// <summary>
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Configures KafkaFlow to run as a Hosted Service
/// </summary>
/// <param name="services">Instance of <see cref="IServiceCollection"/></param>
/// <param name="kafka">A handler to configure KafkaFlow</param>
/// <returns></returns>
public static IServiceCollection AddKafkaFlowHostedService(
this IServiceCollection services,
Action<IKafkaConfigurationBuilder> kafka)
{
return services
.AddHostedService<KafkaFlowHostedService>()
.AddKafka(kafka);
}
}
}
7 changes: 7 additions & 0 deletions src/KafkaFlow.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 4051104

Please sign in to comment.