Browse Source

Implementing IHostedService GracePeriodManagerService with base class BackgroundService from ".NET Core 2.1" in a separate WebHost (Seggregated service for background tasks of the Ordering Business Microservice) named Ordering.BackgroundTasksWebHost

pull/811/head
Cesar De la Torre 7 years ago
parent
commit
27a72b5c71
16 changed files with 700 additions and 1 deletions
  1. +12
    -0
      .dockerignore
  2. +1
    -0
      docker-compose.dcproj
  3. +17
    -0
      docker-compose.override.yml
  4. +9
    -0
      docker-compose.yml
  5. +103
    -1
      eShopOnContainers-ServicesAndWebApps.sln
  6. +8
    -0
      src/Services/Ordering/Ordering.BackgroundTasksHost/Ordering.BackgroundTasksHost.csproj
  7. +34
    -0
      src/Services/Ordering/Ordering.BackgroundTasksHost/Program.cs
  8. +6
    -0
      src/Services/Ordering/Ordering.BackgroundTasksWebHost/Dockerfile
  9. +83
    -0
      src/Services/Ordering/Ordering.BackgroundTasksWebHost/HostedServices/BackgroundService.cs
  10. +94
    -0
      src/Services/Ordering/Ordering.BackgroundTasksWebHost/HostedServices/GracePeriodManagerService.cs
  11. +18
    -0
      src/Services/Ordering/Ordering.BackgroundTasksWebHost/Infrastructure/AutofacModules/ApplicationModule.cs
  12. +27
    -0
      src/Services/Ordering/Ordering.BackgroundTasksWebHost/Ordering.BackgroundTasksWebHost.csproj
  13. +13
    -0
      src/Services/Ordering/Ordering.BackgroundTasksWebHost/OrderingBackgroundSettings.cs
  14. +37
    -0
      src/Services/Ordering/Ordering.BackgroundTasksWebHost/Program.cs
  15. +219
    -0
      src/Services/Ordering/Ordering.BackgroundTasksWebHost/Startup.cs
  16. +19
    -0
      src/Services/Ordering/Ordering.BackgroundTasksWebHost/settings.json

+ 12
- 0
.dockerignore View File

@ -0,0 +1,12 @@
.dockerignore
.env
.git
.gitignore
.vs
.vscode
docker-compose.yml
docker-compose.*.yml
*/bin
*/obj
!obj/Docker/publish/*
!obj/Docker/empty/

+ 1
- 0
docker-compose.dcproj View File

@ -9,6 +9,7 @@
<ProjectVersion>2.0</ProjectVersion> <ProjectVersion>2.0</ProjectVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<None Include=".dockerignore" />
<None Include="docker-compose.ci.build.yml" /> <None Include="docker-compose.ci.build.yml" />
<None Include="docker-compose.override.yml"> <None Include="docker-compose.override.yml">
<DependentUpon>docker-compose.yml</DependentUpon> <DependentUpon>docker-compose.yml</DependentUpon>


+ 17
- 0
docker-compose.override.yml View File

@ -201,6 +201,23 @@ services:
ports: ports:
- "5109:80" - "5109:80"
ordering.backgroundtaskswebhost:
environment:
- ASPNETCORE_ENVIRONMENT=Development
- ASPNETCORE_URLS=http://0.0.0.0:80
- ConnectionString=${ESHOP_AZURE_ORDERING_DB:-Server=sql.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word}
- EventBusConnection=${ESHOP_AZURE_SERVICE_BUS:-rabbitmq}
- EventBusUserName=${ESHOP_SERVICE_BUS_USERNAME}
- EventBusPassword=${ESHOP_SERVICE_BUS_PASSWORD}
- AzureServiceBusEnabled=False
- CheckUpdateTime=30000
- GracePeriodTime=1
- ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY}
- OrchestratorType=${ORCHESTRATOR_TYPE}
- UseLoadTest=${USE_LOADTEST:-False}
sql.data: sql.data:
environment: environment:
- MSSQL_SA_PASSWORD=Pass@word - MSSQL_SA_PASSWORD=Pass@word


+ 9
- 0
docker-compose.yml View File

@ -96,6 +96,12 @@ services:
- nosql.data - nosql.data
- rabbitmq - rabbitmq
ordering.backgroundtaskswebhost:
image: eshop/ordering.backgroundtaskswebhost:${TAG:-latest}
build:
context: ./src/Services/Ordering/Ordering.BackgroundTasksWebHost
dockerfile: Dockerfile
sql.data: sql.data:
image: microsoft/mssql-server-linux:2017-latest image: microsoft/mssql-server-linux:2017-latest
@ -107,3 +113,6 @@ services:
rabbitmq: rabbitmq:
image: rabbitmq:3-management image: rabbitmq:3-management

+ 103
- 1
eShopOnContainers-ServicesAndWebApps.sln View File

@ -1,7 +1,7 @@
 
Microsoft Visual Studio Solution File, Format Version 12.00 Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15 # Visual Studio 15
VisualStudioVersion = 15.0.27004.2002
VisualStudioVersion = 15.0.27102.0
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{FEA0C318-FFED-4D39-8781-265718CA43DD}" Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{FEA0C318-FFED-4D39-8781-265718CA43DD}"
EndProject EndProject
@ -101,6 +101,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "WebHost", "WebHost", "{1815
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebHost.Customization", "src\BuildingBlocks\WebHostCustomization\WebHost.Customization\WebHost.Customization.csproj", "{15F4B3AA-89B6-4A0D-9051-414305974781}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebHost.Customization", "src\BuildingBlocks\WebHostCustomization\WebHost.Customization\WebHost.Customization.csproj", "{15F4B3AA-89B6-4A0D-9051-414305974781}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ordering.BackgroundTasksHost", "src\Services\Ordering\Ordering.BackgroundTasksHost\Ordering.BackgroundTasksHost.csproj", "{0469282C-4A4D-4BC5-B93A-B65096E67895}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ordering.BackgroundTasksWebHost", "src\Services\Ordering\Ordering.BackgroundTasksWebHost\Ordering.BackgroundTasksWebHost.csproj", "{46B47A45-0EA0-4650-9A84-0A972D98E815}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
@ -1363,6 +1367,102 @@ Global
{15F4B3AA-89B6-4A0D-9051-414305974781}.Release|x64.Build.0 = Release|Any CPU {15F4B3AA-89B6-4A0D-9051-414305974781}.Release|x64.Build.0 = Release|Any CPU
{15F4B3AA-89B6-4A0D-9051-414305974781}.Release|x86.ActiveCfg = Release|Any CPU {15F4B3AA-89B6-4A0D-9051-414305974781}.Release|x86.ActiveCfg = Release|Any CPU
{15F4B3AA-89B6-4A0D-9051-414305974781}.Release|x86.Build.0 = Release|Any CPU {15F4B3AA-89B6-4A0D-9051-414305974781}.Release|x86.Build.0 = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|ARM.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|ARM.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|iPhone.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|x64.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|x64.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|x86.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.AppStore|x86.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|ARM.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|ARM.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|iPhone.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|x64.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|x64.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|x86.ActiveCfg = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Debug|x86.Build.0 = Debug|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|Any CPU.Build.0 = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|ARM.ActiveCfg = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|ARM.Build.0 = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|iPhone.ActiveCfg = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|iPhone.Build.0 = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|x64.ActiveCfg = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|x64.Build.0 = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|x86.ActiveCfg = Release|Any CPU
{0469282C-4A4D-4BC5-B93A-B65096E67895}.Release|x86.Build.0 = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|ARM.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|ARM.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|iPhone.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|x64.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|x64.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|x86.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.AppStore|x86.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|Any CPU.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|ARM.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|ARM.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|iPhone.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|x64.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|x64.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|x86.ActiveCfg = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Debug|x86.Build.0 = Debug|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|Any CPU.ActiveCfg = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|Any CPU.Build.0 = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|ARM.ActiveCfg = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|ARM.Build.0 = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|iPhone.ActiveCfg = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|iPhone.Build.0 = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|x64.ActiveCfg = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|x64.Build.0 = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|x86.ActiveCfg = Release|Any CPU
{46B47A45-0EA0-4650-9A84-0A972D98E815}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -1411,6 +1511,8 @@ Global
{969E793C-C413-490E-9C9D-B2B46DA5AF32} = {EF0337F2-ED00-4643-89FD-EE10863F1870} {969E793C-C413-490E-9C9D-B2B46DA5AF32} = {EF0337F2-ED00-4643-89FD-EE10863F1870}
{1815B651-941C-466B-AE33-D1D7EEB8F77F} = {DB0EFB20-B024-4E5E-A75C-52143C131D25} {1815B651-941C-466B-AE33-D1D7EEB8F77F} = {DB0EFB20-B024-4E5E-A75C-52143C131D25}
{15F4B3AA-89B6-4A0D-9051-414305974781} = {1815B651-941C-466B-AE33-D1D7EEB8F77F} {15F4B3AA-89B6-4A0D-9051-414305974781} = {1815B651-941C-466B-AE33-D1D7EEB8F77F}
{0469282C-4A4D-4BC5-B93A-B65096E67895} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B}
{46B47A45-0EA0-4650-9A84-0A972D98E815} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {25728519-5F0F-4973-8A64-0A81EB4EA8D9} SolutionGuid = {25728519-5F0F-4973-8A64-0A81EB4EA8D9}


+ 8
- 0
src/Services/Ordering/Ordering.BackgroundTasksHost/Ordering.BackgroundTasksHost.csproj View File

@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
</Project>

+ 34
- 0
src/Services/Ordering/Ordering.BackgroundTasksHost/Program.cs View File

@ -0,0 +1,34 @@
using System;
// NOT READY WITH Microsoft.Extensions.Hosting out of ASP.NET Core until .NET Core 2.1
//using System.Threading.Tasks;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Hosting;
namespace Ordering.BackgroundTasksHost
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
//public class ProgramHelloWorld
//{
// public static async Task Main(string[] args)
// {
// var builder = new HostBuilder()
// .ConfigureServices((hostContext, services) =>
// {
// services.AddScoped<IHostedService, MyServiceA>();
// services.AddScoped<IHostedService, MyServiceB>();
// });
// await builder.RunConsoleAsync();
// }
//}
}

+ 6
- 0
src/Services/Ordering/Ordering.BackgroundTasksWebHost/Dockerfile View File

@ -0,0 +1,6 @@
FROM microsoft/aspnetcore:2.0
ARG source
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "Ordering.BackgroundTasksWebHost.dll"]

+ 83
- 0
src/Services/Ordering/Ordering.BackgroundTasksWebHost/HostedServices/BackgroundService.cs View File

@ -0,0 +1,83 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace Ordering.BackgroundTasksWebHost.HostedServices
{
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
/// <summary>
/// Base class for implementing a long running <see cref="IHostedService"/>.
/// IMPORTANT: This base class is implemented in .NET Core 2.1 - Since this microservice is still in .NET Core 2.0, we're using the class within the project
/// When .NET Core 2.1 is released, this class should be removed and you should use the use implemented by the framework
/// https://github.com/aspnet/Hosting/blob/712c992ca827576c05923e6a134ca0bec87af4df/src/Microsoft.Extensions.Hosting.Abstractions/BackgroundService.cs
///
/// </summary>
public abstract class BackgroundService : IHostedService, IDisposable
{
private Task _executingTask;
private readonly CancellationTokenSource _stoppingCts = new CancellationTokenSource();
/// <summary>
/// This method is called when the <see cref="IHostedService"/> starts. The implementation should return a task that represents
/// the lifetime of the long running operation(s) being performed.
/// </summary>
/// <param name="stoppingToken">Triggered when <see cref="IHostedService.StopAsync(CancellationToken)"/> is called.</param>
/// <returns>A <see cref="Task"/> that represents the long running operations.</returns>
protected abstract Task ExecuteAsync(CancellationToken stoppingToken);
/// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
/// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
public virtual Task StartAsync(CancellationToken cancellationToken)
{
// Store the task we're executing
_executingTask = ExecuteAsync(_stoppingCts.Token);
// If the task is completed then return it, this will bubble cancellation and failure to the caller
if (_executingTask.IsCompleted)
{
return _executingTask;
}
// Otherwise it's running
return Task.CompletedTask;
}
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// </summary>
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
public virtual async Task StopAsync(CancellationToken cancellationToken)
{
// Stop called without start
if (_executingTask == null)
{
return;
}
try
{
// Signal cancellation to the executing method
_stoppingCts.Cancel();
}
finally
{
// Wait until the task completes or the stop token triggers
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
}
}
public virtual void Dispose()
{
_stoppingCts.Cancel();
}
}
}

+ 94
- 0
src/Services/Ordering/Ordering.BackgroundTasksWebHost/HostedServices/GracePeriodManagerService.cs View File

@ -0,0 +1,94 @@
namespace Ordering.BackgroundTasksWebHost.HostedServices
{
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Threading;
using System.Threading.Tasks;
//using Dapper;
//using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
//using Microsoft.eShopOnContainers.Services.Ordering.API;
//using Ordering.API.Application.IntegrationEvents.Events;
public class GracePeriodManagerService
: BackgroundService
{
private readonly ILogger<GracePeriodManagerService> _logger;
private readonly OrderingBackgroundSettings _settings;
//private readonly IEventBus _eventBus;
public GracePeriodManagerService(IOptions<OrderingBackgroundSettings> settings,
//IEventBus eventBus,
ILogger<GracePeriodManagerService> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
//_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
_settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings));
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogDebug($"GracePeriodManagerService is starting.");
stoppingToken.Register(() => _logger.LogDebug($"#1 GracePeriodManagerService background task is stopping."));
while (!stoppingToken.IsCancellationRequested)
{
_logger.LogDebug($"GracePeriodManagerService background task is doing background work.");
//CheckConfirmedGracePeriodOrders();
await Task.Delay(_settings.CheckUpdateTime, stoppingToken);
}
_logger.LogDebug($"GracePeriodManagerService background task is stopping.");
await Task.CompletedTask;
}
//private void CheckConfirmedGracePeriodOrders()
//{
// _logger.LogDebug($"Checking confirmed grace period orders");
// var orderIds = GetConfirmedGracePeriodOrders();
// foreach (var orderId in orderIds)
// {
// var confirmGracePeriodEvent = new GracePeriodConfirmedIntegrationEvent(orderId);
// _eventBus.Publish(confirmGracePeriodEvent);
// }
//}
//private IEnumerable<int> GetConfirmedGracePeriodOrders()
//{
// IEnumerable<int> orderIds = new List<int>();
// using (var conn = new SqlConnection(_settings.ConnectionString))
// {
// try
// {
// conn.Open();
// orderIds = conn.Query<int>(
// @"SELECT Id FROM [ordering].[orders]
// WHERE DATEDIFF(minute, [OrderDate], GETDATE()) >= @GracePeriodTime
// AND [OrderStatusId] = 1",
// new { GracePeriodTime = _settings.GracePeriodTime });
// }
// catch (SqlException exception)
// {
// _logger.LogCritical($"FATAL ERROR: Database connections could not be opened: {exception.Message}");
// }
// }
// return orderIds;
//}
}
}

+ 18
- 0
src/Services/Ordering/Ordering.BackgroundTasksWebHost/Infrastructure/AutofacModules/ApplicationModule.cs View File

@ -0,0 +1,18 @@
using Autofac;
namespace Ordering.BackgroundTasksWebHost.Infrastructure.AutofacModules
{
public class ApplicationModule
:Autofac.Module
{
public ApplicationModule(string qconstr)
{
}
protected override void Load(ContainerBuilder builder)
{
}
}
}

+ 27
- 0
src/Services/Ordering/Ordering.BackgroundTasksWebHost/Ordering.BackgroundTasksWebHost.csproj View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<None Remove="settings.json" />
</ItemGroup>
<ItemGroup>
<Content Include="settings.json">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection">
<Version>4.2.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.All">
<Version>2.0.3</Version>
</PackageReference>
</ItemGroup>
</Project>

+ 13
- 0
src/Services/Ordering/Ordering.BackgroundTasksWebHost/OrderingBackgroundSettings.cs View File

@ -0,0 +1,13 @@
namespace Ordering.BackgroundTasksWebHost
{
public class OrderingBackgroundSettings
{
public string ConnectionString { get; set; }
public string EventBusConnection { get; set; }
public int GracePeriodTime { get; set; }
public int CheckUpdateTime { get; set; }
}
}

+ 37
- 0
src/Services/Ordering/Ordering.BackgroundTasksWebHost/Program.cs View File

@ -0,0 +1,37 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.IO;
namespace Ordering.BackgroundTasksWebHost
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
//.UseHealthChecks("/hc")
.UseContentRoot(Directory.GetCurrentDirectory())
.ConfigureAppConfiguration((builderContext, config) =>
{
config.AddJsonFile("settings.json");
config.AddEnvironmentVariables();
})
.ConfigureLogging((hostingContext, builder) =>
{
builder.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
builder.AddConsole();
builder.AddDebug();
})
//.UseApplicationInsights()
.Build();
}
}

+ 219
- 0
src/Services/Ordering/Ordering.BackgroundTasksWebHost/Startup.cs View File

@ -0,0 +1,219 @@
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
//using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
//using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
//using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
//using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Data.Common;
//using Microsoft.ApplicationInsights.ServiceFabric;
//using RabbitMQ.Client;
//using Microsoft.Azure.ServiceBus;
//using Microsoft.Extensions.HealthChecks;
//using Ordering.BackgroundTasksWebHost.Application.IntegrationEvents;
//using Ordering.BackgroundTasksWebHost.Application.IntegrationEvents.Events;
using Ordering.BackgroundTasksWebHost.Infrastructure.AutofacModules;
using Ordering.BackgroundTasksWebHost.HostedServices;
//using Ordering.BackgroundTasksWebHost.HostedServices;
namespace Ordering.BackgroundTasksWebHost
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public IServiceProvider ConfigureServices(IServiceCollection services)
{
//RegisterAppInsights(services);
// Configure GracePeriodManager Hosted Service
services.AddSingleton<IHostedService, GracePeriodManagerService>();
//services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
//services.AddHealthChecks(checks =>
//{
// var minutes = 1;
// if (int.TryParse(Configuration["HealthCheck:Timeout"], out var minutesParsed))
// {
// minutes = minutesParsed;
// }
// checks.AddSqlCheck("OrderingDb", Configuration["ConnectionString"], TimeSpan.FromMinutes(minutes));
//});
services.Configure<OrderingBackgroundSettings>(Configuration);
// Add application services.
//services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
//if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
//{
// services.AddSingleton<IServiceBusPersisterConnection>(sp =>
// {
// var logger = sp.GetRequiredService<ILogger<DefaultServiceBusPersisterConnection>>();
// var serviceBusConnectionString = Configuration["EventBusConnection"];
// var serviceBusConnection = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);
// return new DefaultServiceBusPersisterConnection(serviceBusConnection, logger);
// });
//}
//else
//{
// services.AddSingleton<IRabbitMQPersistentConnection>(sp =>
// {
// var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
// var factory = new ConnectionFactory()
// {
// HostName = Configuration["EventBusConnection"]
// };
// if (!string.IsNullOrEmpty(Configuration["EventBusUserName"]))
// {
// factory.UserName = Configuration["EventBusUserName"];
// }
// if (!string.IsNullOrEmpty(Configuration["EventBusPassword"]))
// {
// factory.Password = Configuration["EventBusPassword"];
// }
// var retryCount = 5;
// if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
// {
// retryCount = int.Parse(Configuration["EventBusRetryCount"]);
// }
// return new DefaultRabbitMQPersistentConnection(factory, logger, retryCount);
// });
//}
//RegisterEventBus(services);
services.AddOptions();
//Configure autofac
var container = new ContainerBuilder();
container.Populate(services);
container.RegisterModule(new ApplicationModule(Configuration["ConnectionString"]));
return new AutofacServiceProvider(container.Build());
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
//loggerFactory.AddAzureWebAppDiagnostics();
//loggerFactory.AddApplicationInsights(app.ApplicationServices, LogLevel.Trace);
var pathBase = Configuration["PATH_BASE"];
if (!string.IsNullOrEmpty(pathBase))
{
loggerFactory.CreateLogger("init").LogDebug($"Using PATH BASE '{pathBase}'");
app.UsePathBase(pathBase);
}
//ConfigureEventBus(app);
}
//private void RegisterAppInsights(IServiceCollection services)
//{
// services.AddApplicationInsightsTelemetry(Configuration);
// var orchestratorType = Configuration.GetValue<string>("OrchestratorType");
// if (orchestratorType?.ToUpper() == "K8S")
// {
// // Enable K8s telemetry initializer
// services.EnableKubernetes();
// }
// if (orchestratorType?.ToUpper() == "SF")
// {
// // Enable SF telemetry initializer
// services.AddSingleton<ITelemetryInitializer>((serviceProvider) =>
// new FabricTelemetryInitializer());
// }
//}
//private void ConfigureEventBus(IApplicationBuilder app)
//{
// var eventBus = app.ApplicationServices.GetRequiredService<BuildingBlocks.EventBus.Abstractions.IEventBus>();
// eventBus.Subscribe<UserCheckoutAcceptedIntegrationEvent, IIntegrationEventHandler<UserCheckoutAcceptedIntegrationEvent>>();
// eventBus.Subscribe<GracePeriodConfirmedIntegrationEvent, IIntegrationEventHandler<GracePeriodConfirmedIntegrationEvent>>();
// eventBus.Subscribe<OrderStockConfirmedIntegrationEvent, IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>>();
// eventBus.Subscribe<OrderStockRejectedIntegrationEvent, IIntegrationEventHandler<OrderStockRejectedIntegrationEvent>>();
// eventBus.Subscribe<OrderPaymentFailedIntegrationEvent, IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>>();
// eventBus.Subscribe<OrderPaymentSuccededIntegrationEvent, IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>>();
//}
//private void RegisterEventBus(IServiceCollection services)
//{
// var subscriptionClientName = Configuration["SubscriptionClientName"];
// if (Configuration.GetValue<bool>("AzureServiceBusEnabled"))
// {
// services.AddSingleton<IEventBus, EventBusServiceBus>(sp =>
// {
// var serviceBusPersisterConnection = sp.GetRequiredService<IServiceBusPersisterConnection>();
// var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
// var logger = sp.GetRequiredService<ILogger<EventBusServiceBus>>();
// var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
// return new EventBusServiceBus(serviceBusPersisterConnection, logger,
// eventBusSubcriptionsManager, subscriptionClientName, iLifetimeScope);
// });
// }
// else
// {
// services.AddSingleton<IEventBus, EventBusRabbitMQ>(sp =>
// {
// var rabbitMQPersistentConnection = sp.GetRequiredService<IRabbitMQPersistentConnection>();
// var iLifetimeScope = sp.GetRequiredService<ILifetimeScope>();
// var logger = sp.GetRequiredService<ILogger<EventBusRabbitMQ>>();
// var eventBusSubcriptionsManager = sp.GetRequiredService<IEventBusSubscriptionsManager>();
// var retryCount = 5;
// if (!string.IsNullOrEmpty(Configuration["EventBusRetryCount"]))
// {
// retryCount = int.Parse(Configuration["EventBusRetryCount"]);
// }
// return new EventBusRabbitMQ(rabbitMQPersistentConnection, logger, iLifetimeScope, eventBusSubcriptionsManager, subscriptionClientName, retryCount);
// });
// }
// services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
//}
}
}

+ 19
- 0
src/Services/Ordering/Ordering.BackgroundTasksWebHost/settings.json View File

@ -0,0 +1,19 @@
{
"ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;",
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Trace",
"System": "Information",
"Microsoft": "Information"
}
},
"AzureServiceBusEnabled": false,
"SubscriptionClientName": "Ordering",
"GracePeriodTime": "1",
"CheckUpdateTime": "30000",
"ApplicationInsights": {
"InstrumentationKey": ""
},
"EventBusRetryCount": 5
}

Loading…
Cancel
Save