Skip to content

Commit 460fc99

Browse files
committed
Create Generic Host prototype for background task in order to be reviewed
1 parent 094ba7c commit 460fc99

9 files changed

Lines changed: 493 additions & 1 deletion

File tree

eShopOnContainers-ServicesAndWebApps.sln

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ordering.BackgroundTasks",
128128
EndProject
129129
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ordering.SignalrHub", "src\Services\Ordering\Ordering.SignalrHub\Ordering.SignalrHub.csproj", "{E1D2B260-4E7F-4A88-BC13-9910F7C44623}"
130130
EndProject
131+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ordering.BackgroundTasksHost", "src\Services\Ordering\Ordering.BackgroundTasksHost\Ordering.BackgroundTasksHost.csproj", "{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}"
132+
EndProject
131133
Global
132134
GlobalSection(SolutionConfigurationPlatforms) = preSolution
133135
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
@@ -1534,6 +1536,54 @@ Global
15341536
{E1D2B260-4E7F-4A88-BC13-9910F7C44623}.Release|x64.Build.0 = Release|Any CPU
15351537
{E1D2B260-4E7F-4A88-BC13-9910F7C44623}.Release|x86.ActiveCfg = Release|Any CPU
15361538
{E1D2B260-4E7F-4A88-BC13-9910F7C44623}.Release|x86.Build.0 = Release|Any CPU
1539+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
1540+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
1541+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
1542+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
1543+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
1544+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
1545+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1546+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
1547+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
1548+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
1549+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
1550+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
1551+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
1552+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|Any CPU.Build.0 = Debug|Any CPU
1553+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|ARM.ActiveCfg = Debug|Any CPU
1554+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|ARM.Build.0 = Debug|Any CPU
1555+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
1556+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|iPhone.Build.0 = Debug|Any CPU
1557+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1558+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
1559+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|x64.ActiveCfg = Debug|Any CPU
1560+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|x64.Build.0 = Debug|Any CPU
1561+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|x86.ActiveCfg = Debug|Any CPU
1562+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.AppStore|x86.Build.0 = Debug|Any CPU
1563+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1564+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|Any CPU.Build.0 = Debug|Any CPU
1565+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|ARM.ActiveCfg = Debug|Any CPU
1566+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|ARM.Build.0 = Debug|Any CPU
1567+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|iPhone.ActiveCfg = Debug|Any CPU
1568+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|iPhone.Build.0 = Debug|Any CPU
1569+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1570+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
1571+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|x64.ActiveCfg = Debug|Any CPU
1572+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|x64.Build.0 = Debug|Any CPU
1573+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|x86.ActiveCfg = Debug|Any CPU
1574+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Debug|x86.Build.0 = Debug|Any CPU
1575+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|Any CPU.ActiveCfg = Release|Any CPU
1576+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|Any CPU.Build.0 = Release|Any CPU
1577+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|ARM.ActiveCfg = Release|Any CPU
1578+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|ARM.Build.0 = Release|Any CPU
1579+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|iPhone.ActiveCfg = Release|Any CPU
1580+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|iPhone.Build.0 = Release|Any CPU
1581+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
1582+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
1583+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|x64.ActiveCfg = Release|Any CPU
1584+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|x64.Build.0 = Release|Any CPU
1585+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|x86.ActiveCfg = Release|Any CPU
1586+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E}.Release|x86.Build.0 = Release|Any CPU
15371587
EndGlobalSection
15381588
GlobalSection(SolutionProperties) = preSolution
15391589
HideSolutionNode = FALSE
@@ -1589,6 +1639,7 @@ Global
15891639
{AF0828DB-8BDD-411A-AEEF-B780FBB8D8C1} = {28C0F5C8-4849-4035-80AB-45639424E73F}
15901640
{7D63ED4A-3EDA-4BBA-8BBA-F46BD6430931} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B}
15911641
{E1D2B260-4E7F-4A88-BC13-9910F7C44623} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B}
1642+
{CA566CD5-A49A-47F7-BDC9-592E36DAF74E} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B}
15921643
EndGlobalSection
15931644
GlobalSection(ExtensibilityGlobals) = postSolution
15941645
SolutionGuid = {25728519-5F0F-4973-8A64-0A81EB4EA8D9}

src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<Folder Include="wwwroot\" />
10+
<Compile Remove="wwwroot\**" />
11+
<Content Remove="wwwroot\**" />
12+
<EmbeddedResource Remove="wwwroot\**" />
13+
<None Remove="wwwroot\**" />
1114
</ItemGroup>
1215

1316
<ItemGroup>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Ordering.BackgroundTasksHost.Configuration
6+
{
7+
public class BackgroundTaskSettings
8+
{
9+
public string ConnectionString { get; set; }
10+
11+
public string EventBusConnection { get; set; }
12+
13+
public int GracePeriodTime { get; set; }
14+
15+
public int CheckUpdateTime { get; set; }
16+
}
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
2+
3+
namespace Ordering.BackgroundTasksHost.IntegrationEvents
4+
{
5+
public class GracePeriodConfirmedIntegrationEvent : IntegrationEvent
6+
{
7+
public int OrderId { get; }
8+
9+
public GracePeriodConfirmedIntegrationEvent(int orderId) =>
10+
OrderId = orderId;
11+
}
12+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp2.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<None Remove="appsettings.json" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<Content Include="appsettings.json">
14+
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
15+
</Content>
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.2.2" />
20+
<PackageReference Include="Dapper" Version="1.50.4" />
21+
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0-rc1-final" />
22+
</ItemGroup>
23+
<ItemGroup>
24+
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
25+
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />
26+
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" />
27+
</ItemGroup>
28+
<ItemGroup>
29+
<Folder Include="Tasks\" />
30+
</ItemGroup>
31+
32+
</Project>
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
using Autofac.Extensions.DependencyInjection;
2+
using Autofac;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using System;
6+
using Ordering.BackgroundTasksHost.Configuration;
7+
using Microsoft.Extensions.Configuration;
8+
using Microsoft.Extensions.Logging;
9+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
10+
using Microsoft.Azure.ServiceBus;
11+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
12+
using RabbitMQ.Client;
13+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
14+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
15+
using Ordering.BackgroundTasksHost.Tasks;
16+
17+
namespace Ordering.BackgroundTasksHost
18+
{
19+
class Program
20+
{
21+
static void Main(string[] args)
22+
{
23+
using (var host = CreateHost(args))
24+
{
25+
host.Start();
26+
27+
host.WaitForShutdown();
28+
}
29+
}
30+
31+
static IHost CreateHost(string[] args)
32+
{
33+
var host = new HostBuilder()
34+
.ConfigureAppConfiguration((hostContext, configApp) =>
35+
{
36+
configApp.AddEnvironmentVariables();
37+
configApp.AddJsonFile("appsettings.json", optional: true);
38+
configApp.AddJsonFile(
39+
$"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
40+
optional: true);
41+
configApp.AddCommandLine(args);
42+
43+
})
44+
.ConfigureServices(services =>
45+
{
46+
var configuration = services.BuildServiceProvider()
47+
.GetRequiredService<IConfiguration>();
48+
49+
services.AddOptions()
50+
.Configure<BackgroundTaskSettings>(configuration)
51+
.RegisterBus(configuration)
52+
.RegisterHostedServices();
53+
54+
})
55+
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
56+
.Build();
57+
58+
return host;
59+
}
60+
61+
62+
}
63+
64+
class AutofacServiceProviderFactory
65+
: IServiceProviderFactory<ContainerBuilder>
66+
{
67+
public ContainerBuilder CreateBuilder(IServiceCollection services)
68+
{
69+
var containerBuilder = new ContainerBuilder();
70+
containerBuilder.Populate(services);
71+
72+
return containerBuilder;
73+
}
74+
75+
public IServiceProvider CreateServiceProvider(ContainerBuilder containerBuilder)
76+
{
77+
return new AutofacServiceProvider(containerBuilder.Build());
78+
}
79+
}
80+
81+
static class ServiceCollectionExtensions
82+
{
83+
public static IServiceCollection RegisterBus(this IServiceCollection services, IConfiguration configuration)
84+
{
85+
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
86+
{
87+
services.AddSingleton<IServiceBusPersisterConnection>(sp =>
88+
{
89+
var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
90+
91+
var serviceBusConnectionString = configuration["EventBusConnection"];
92+
var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
93+
94+
return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
95+
});
96+
}
97+
else
98+
{
99+
services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
100+
{
101+
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
102+
103+
104+
var factory = new ConnectionFactory()
105+
{
106+
HostName = configuration["EventBusConnection"]
107+
};
108+
109+
if (!string.IsNullOrEmpty(configuration["EventBusUserName"]))
110+
{
111+
factory.UserName = configuration["EventBusUserName"];
112+
}
113+
114+
if (!string.IsNullOrEmpty(configuration["EventBusPassword"]))
115+
{
116+
factory.Password = configuration["EventBusPassword"];
117+
}
118+
119+
var retryCount = 5;
120+
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))
121+
{
122+
retryCount = int.Parse(configuration["EventBusRetryCount"]);
123+
}
124+
125+
return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
126+
});
127+
}
128+
129+
130+
var subscriptionClientName = configuration["SubscriptionClientName"];
131+
132+
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
133+
{
134+
services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
135+
{
136+
var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
137+
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
138+
var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
139+
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
140+
141+
return new EventBusServiceBus(serviceBusPersisterConnection, logger,
142+
eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
143+
});
144+
}
145+
else
146+
{
147+
services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
148+
{
149+
var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
150+
var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
151+
var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
152+
var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
153+
154+
var retryCount = 5;
155+
if (!string.IsNullOrEmpty(configuration["EventBusRetryCount"]))
156+
{
157+
retryCount = int.Parse(configuration["EventBusRetryCount"]);
158+
}
159+
160+
return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount);
161+
});
162+
}
163+
164+
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
165+
166+
return services;
167+
}
168+
169+
public static IServiceCollection RegisterHostedServices(this IServiceCollection services)
170+
{
171+
services.AddSingleton<IHostedService, GracePeriodManagerService>();
172+
173+
return services;
174+
}
175+
}
176+
177+
}

0 commit comments

Comments
 (0)