Skip to content

Commit 025ab0e

Browse files
committed
Implemented HostedService in different service
1 parent e869b67 commit 025ab0e

20 files changed

Lines changed: 730 additions & 131 deletions

File tree

docker-compose.override.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,24 @@ services:
8383
ports:
8484
- "5102:80"
8585

86+
ordering.backgroundtasks:
87+
environment:
88+
- ASPNETCORE_ENVIRONMENT=Development
89+
- ASPNETCORE_URLS=http://0.0.0.0:80
90+
- ConnectionString=${ESHOP_AZURE_ORDERING_DB:-Server=sql.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word}
91+
- EventBusConnection=${ESHOP_AZURE_SERVICE_BUS:-rabbitmq}
92+
- EventBusUserName=${ESHOP_SERVICE_BUS_USERNAME}
93+
- EventBusPassword=${ESHOP_SERVICE_BUS_PASSWORD}
94+
- UseCustomizationData=True
95+
- AzureServiceBusEnabled=False
96+
- CheckUpdateTime=30000
97+
- GracePeriodTime=1
98+
- ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY}
99+
- OrchestratorType=${ORCHESTRATOR_TYPE}
100+
- UseLoadTest=${USE_LOADTEST:-False}
101+
ports:
102+
- "5111:80"
103+
86104
marketing.api:
87105
environment:
88106
- ASPNETCORE_ENVIRONMENT=Development
@@ -219,4 +237,5 @@ services:
219237
rabbitmq:
220238
ports:
221239
- "15672:15672"
222-
- "5672:5672"
240+
- "5672:5672"
241+

docker-compose.prod.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ services:
9090
ports:
9191
- "5102:80"
9292

93+
ordering.backgroundtasks:
94+
environment:
95+
- ASPNETCORE_ENVIRONMENT=Development
96+
- ASPNETCORE_URLS=http://0.0.0.0:80
97+
- ConnectionString=${ESHOP_AZURE_ORDERING_DB:-Server=sql.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word}
98+
- EventBusConnection=${ESHOP_AZURE_SERVICE_BUS:-rabbitmq}
99+
- EventBusUserName=${ESHOP_SERVICE_BUS_USERNAME}
100+
- EventBusPassword=${ESHOP_SERVICE_BUS_PASSWORD}
101+
- UseCustomizationData=True
102+
- AzureServiceBusEnabled=False
103+
- CheckUpdateTime=30000
104+
- GracePeriodTime=1
105+
- ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY}
106+
- OrchestratorType=${ORCHESTRATOR_TYPE}
107+
- UseLoadTest=${USE_LOADTEST:-False}
108+
ports:
109+
- "5111:80"
110+
93111
marketing.api:
94112
environment:
95113
- ASPNETCORE_ENVIRONMENT=Development

docker-compose.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ services:
3838
- sql.data
3939
- rabbitmq
4040

41+
ordering.backgroundtasks:
42+
image: eshop/ordering.backgroundtasks:${TAG:-latest}
43+
build:
44+
context: .
45+
dockerfile: src/Services/Ordering/Ordering.BackgroundTasks/Dockerfile
46+
depends_on:
47+
- sql.data
48+
- rabbitmq
49+
4150
marketing.api:
4251
image: eshop/marketing.api:${TAG:-latest}
4352
build:
@@ -106,4 +115,4 @@ services:
106115
image: redis:alpine
107116

108117
rabbitmq:
109-
image: rabbitmq:3-management-alpine
118+
image: rabbitmq:3-management-alpine

eShopOnContainers-ServicesAndWebApps.sln

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.27004.2009
4+
VisualStudioVersion = 15.0.27130.2024
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{FEA0C318-FFED-4D39-8781-265718CA43DD}"
77
EndProject
@@ -97,6 +97,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WebHost", "WebHost", "{1815
9797
EndProject
9898
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebHost.Customization", "src\BuildingBlocks\WebHostCustomization\WebHost.Customization\WebHost.Customization.csproj", "{15F4B3AA-89B6-4A0D-9051-414305974781}"
9999
EndProject
100+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ordering.BackgroundTasks", "src\Services\Ordering\Ordering.BackgroundTasks\Ordering.BackgroundTasks.csproj", "{2FF56999-0266-48B2-ACC1-FEBC482A5105}"
101+
EndProject
100102
Global
101103
GlobalSection(SolutionConfigurationPlatforms) = preSolution
102104
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
@@ -1311,6 +1313,54 @@ Global
13111313
{15F4B3AA-89B6-4A0D-9051-414305974781}.Release|x64.Build.0 = Release|Any CPU
13121314
{15F4B3AA-89B6-4A0D-9051-414305974781}.Release|x86.ActiveCfg = Release|Any CPU
13131315
{15F4B3AA-89B6-4A0D-9051-414305974781}.Release|x86.Build.0 = Release|Any CPU
1316+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
1317+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
1318+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
1319+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
1320+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
1321+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
1322+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1323+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
1324+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
1325+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
1326+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
1327+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
1328+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
1329+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|Any CPU.Build.0 = Debug|Any CPU
1330+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|ARM.ActiveCfg = Debug|Any CPU
1331+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|ARM.Build.0 = Debug|Any CPU
1332+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
1333+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|iPhone.Build.0 = Debug|Any CPU
1334+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1335+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
1336+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|x64.ActiveCfg = Debug|Any CPU
1337+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|x64.Build.0 = Debug|Any CPU
1338+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|x86.ActiveCfg = Debug|Any CPU
1339+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.AppStore|x86.Build.0 = Debug|Any CPU
1340+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1341+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|Any CPU.Build.0 = Debug|Any CPU
1342+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|ARM.ActiveCfg = Debug|Any CPU
1343+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|ARM.Build.0 = Debug|Any CPU
1344+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|iPhone.ActiveCfg = Debug|Any CPU
1345+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|iPhone.Build.0 = Debug|Any CPU
1346+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1347+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
1348+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|x64.ActiveCfg = Debug|Any CPU
1349+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|x64.Build.0 = Debug|Any CPU
1350+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|x86.ActiveCfg = Debug|Any CPU
1351+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Debug|x86.Build.0 = Debug|Any CPU
1352+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|Any CPU.ActiveCfg = Release|Any CPU
1353+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|Any CPU.Build.0 = Release|Any CPU
1354+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|ARM.ActiveCfg = Release|Any CPU
1355+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|ARM.Build.0 = Release|Any CPU
1356+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|iPhone.ActiveCfg = Release|Any CPU
1357+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|iPhone.Build.0 = Release|Any CPU
1358+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
1359+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
1360+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|x64.ActiveCfg = Release|Any CPU
1361+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|x64.Build.0 = Release|Any CPU
1362+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|x86.ActiveCfg = Release|Any CPU
1363+
{2FF56999-0266-48B2-ACC1-FEBC482A5105}.Release|x86.Build.0 = Release|Any CPU
13141364
EndGlobalSection
13151365
GlobalSection(SolutionProperties) = preSolution
13161366
HideSolutionNode = FALSE
@@ -1357,6 +1407,7 @@ Global
13571407
{969E793C-C413-490E-9C9D-B2B46DA5AF32} = {EF0337F2-ED00-4643-89FD-EE10863F1870}
13581408
{1815B651-941C-466B-AE33-D1D7EEB8F77F} = {DB0EFB20-B024-4E5E-A75C-52143C131D25}
13591409
{15F4B3AA-89B6-4A0D-9051-414305974781} = {1815B651-941C-466B-AE33-D1D7EEB8F77F}
1410+
{2FF56999-0266-48B2-ACC1-FEBC482A5105} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B}
13601411
EndGlobalSection
13611412
GlobalSection(ExtensibilityGlobals) = postSolution
13621413
SolutionGuid = {25728519-5F0F-4973-8A64-0A81EB4EA8D9}

src/BuildingBlocks/EventBus/EventBusRabbitMQ/EventBusRabbitMQ.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,13 @@ public void Publish(IntegrationEvent @event)
9292

9393
policy.Execute(() =>
9494
{
95+
var properties = channel.CreateBasicProperties();
96+
properties.DeliveryMode = 2; // persistent
97+
9598
channel.BasicPublish(exchange: BROKER_NAME,
9699
routingKey: eventName,
97-
basicProperties: null,
100+
mandatory:true,
101+
basicProperties: properties,
98102
body: body);
99103
});
100104
}
@@ -184,6 +188,8 @@ private IModel CreateConsumerChannel()
184188
var message = Encoding.UTF8.GetString(ea.Body);
185189

186190
await ProcessEvent(eventName, message);
191+
192+
channel.BasicAck(ea.DeliveryTag,multiple:false);
187193
};
188194

189195
channel.BasicConsume(queue: _queueName,

src/Services/Ordering/Ordering.API/Infrastructure/HostedServices/BackgroundService.cs

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,68 +16,68 @@ namespace Ordering.API.Infrastructure.HostedServices
1616
/// https://github.com/aspnet/Hosting/blob/712c992ca827576c05923e6a134ca0bec87af4df/src/Microsoft.Extensions.Hosting.Abstractions/BackgroundService.cs
1717
///
1818
/// </summary>
19-
public abstract class BackgroundService : IHostedService, IDisposable
20-
{
21-
private Task _executingTask;
22-
private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource();
19+
//public abstract class BackgroundService : IHostedService, IDisposable
20+
//{
21+
// private Task _executingTask;
22+
// private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource();
2323

24-
/// <summary>
25-
/// This method is called when the <see cref="IHostedService"/> starts. The implementation should return a task that represents
26-
/// the lifetime of the long running operation(s) being performed.
27-
/// </summary>
28-
/// <param name="stoppingToken">Triggered when <see cref="IHostedService.StopAsync(CancellationToken)"/> is called.</param>
29-
/// <returns>A <see cref="Task"/> that represents the long running operations.</returns>
30-
protected abstract Task ExecuteAsync(CancellationToken stoppingToken);
24+
// /// <summary>
25+
// /// This method is called when the <see cref="IHostedService"/> starts. The implementation should return a task that represents
26+
// /// the lifetime of the long running operation(s) being performed.
27+
// /// </summary>
28+
// /// <param name="stoppingToken">Triggered when <see cref="IHostedService.StopAsync(CancellationToken)"/> is called.</param>
29+
// /// <returns>A <see cref="Task"/> that represents the long running operations.</returns>
30+
// protected abstract Task ExecuteAsync(CancellationToken stoppingToken);
3131

32-
/// <summary>
33-
/// Triggered when the application host is ready to start the service.
34-
/// </summary>
35-
/// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
36-
public virtual Task StartAsync(CancellationToken cancellationToken)
37-
{
38-
// Store the task we're executing
39-
_executingTask = ExecuteAsync(_stoppingCts.Token);
32+
// /// <summary>
33+
// /// Triggered when the application host is ready to start the service.
34+
// /// </summary>
35+
// /// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
36+
// public virtual Task StartAsync(CancellationToken cancellationToken)
37+
// {
38+
// // Store the task we're executing
39+
// _executingTask = ExecuteAsync(_stoppingCts.Token);
4040

41-
// If the task is completed then return it, this will bubble cancellation and failure to the caller
42-
if (_executingTask.IsCompleted)
43-
{
44-
return _executingTask;
45-
}
41+
// // If the task is completed then return it, this will bubble cancellation and failure to the caller
42+
// if (_executingTask.IsCompleted)
43+
// {
44+
// return _executingTask;
45+
// }
4646

47-
// Otherwise it's running
48-
return Task.CompletedTask;
49-
}
47+
// // Otherwise it's running
48+
// return Task.CompletedTask;
49+
// }
5050

51-
/// <summary>
52-
/// Triggered when the application host is performing a graceful shutdown.
53-
/// </summary>
54-
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
55-
public virtual async Task StopAsync(CancellationToken cancellationToken)
56-
{
57-
// Stop called without start
58-
if (_executingTask == null)
59-
{
60-
return;
61-
}
51+
// /// <summary>
52+
// /// Triggered when the application host is performing a graceful shutdown.
53+
// /// </summary>
54+
// /// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
55+
// public virtual async Task StopAsync(CancellationToken cancellationToken)
56+
// {
57+
// // Stop called without start
58+
// if (_executingTask == null)
59+
// {
60+
// return;
61+
// }
6262

63-
try
64-
{
65-
// Signal cancellation to the executing method
66-
_stoppingCts.Cancel();
67-
}
68-
finally
69-
{
70-
// Wait until the task completes or the stop token triggers
71-
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
72-
}
63+
// try
64+
// {
65+
// // Signal cancellation to the executing method
66+
// _stoppingCts.Cancel();
67+
// }
68+
// finally
69+
// {
70+
// // Wait until the task completes or the stop token triggers
71+
// await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
72+
// }
7373

74-
}
74+
// }
7575

76-
public virtual void Dispose()
77-
{
78-
_stoppingCts.Cancel();
79-
}
80-
}
76+
// public virtual void Dispose()
77+
// {
78+
// _stoppingCts.Cancel();
79+
// }
80+
//}
8181

8282

8383
}

0 commit comments

Comments
 (0)