Browse Source

Create a SagaManager events

pull/809/head
Christian Arenas 7 years ago
parent
commit
cde4c3a258
20 changed files with 364 additions and 14 deletions
  1. +6
    -1
      docker-compose.override.yml
  2. +13
    -0
      docker-compose.vs.debug.yml
  3. +10
    -0
      docker-compose.vs.release.yml
  4. +11
    -1
      docker-compose.yml
  5. +55
    -1
      eShopOnContainers-ServicesAndWebApps.sln
  6. +3
    -6
      src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommandMsg.cs
  7. +8
    -3
      src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs
  8. +7
    -1
      src/Services/Ordering/Ordering.API/Ordering.API.csproj
  9. +19
    -1
      src/Services/Ordering/Ordering.API/Startup.cs
  10. +3
    -0
      src/Services/SagaManager/SagaManager/.dockerignore
  11. +5
    -0
      src/Services/SagaManager/SagaManager/Dockerfile
  12. +16
    -0
      src/Services/SagaManager/SagaManager/IntegrationEvents/ConfirmGracePeriodEvent.cs
  13. +14
    -0
      src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodIntegrationEvent.cs
  14. +9
    -0
      src/Services/SagaManager/SagaManager/IntegrationEvents/IConfirmGracePeriodEvent.cs
  15. +65
    -0
      src/Services/SagaManager/SagaManager/Program.cs
  16. +32
    -0
      src/Services/SagaManager/SagaManager/SagaManager.csproj
  17. +11
    -0
      src/Services/SagaManager/SagaManager/SagaManagerSettings.cs
  18. +7
    -0
      src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs
  19. +59
    -0
      src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs
  20. +11
    -0
      src/Services/SagaManager/SagaManager/appsettings.json

+ 6
- 1
docker-compose.override.yml View File

@ -7,7 +7,12 @@ version: '2'
# An external IP or DNS name has to be used (instead localhost and the 10.0.75.1 IP) when testing the Web apps and the Xamarin apps from remote machines/devices using the same WiFi, for instance. # An external IP or DNS name has to be used (instead localhost and the 10.0.75.1 IP) when testing the Web apps and the Xamarin apps from remote machines/devices using the same WiFi, for instance.
services: services:
sagamanager:
environment:
- ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word
- EventBusConnection=rabbitmq
- GracePeriod=15 #In minutes
basket.api: basket.api:
environment: environment:
- ASPNETCORE_ENVIRONMENT=Development - ASPNETCORE_ENVIRONMENT=Development


+ 13
- 0
docker-compose.vs.debug.yml View File

@ -120,3 +120,16 @@ services:
entrypoint: tail -f /dev/null entrypoint: tail -f /dev/null
labels: labels:
- "com.microsoft.visualstudio.targetoperatingsystem=linux" - "com.microsoft.visualstudio.targetoperatingsystem=linux"
sagamanager:
image: eshop/sagamanager:dev
build:
args:
source: ${DOCKER_BUILD_SOURCE}
volumes:
- ./src/Services/SagaManager/SagaManager:/app
- ~/.nuget/packages:/root/.nuget/packages:ro
- ~/clrdbg:/clrdbg:ro
entrypoint: tail -f /dev/null
labels:
- "com.microsoft.visualstudio.targetoperatingsystem=linux"

+ 10
- 0
docker-compose.vs.release.yml View File

@ -80,3 +80,13 @@ services:
entrypoint: tail -f /dev/null entrypoint: tail -f /dev/null
labels: labels:
- "com.microsoft.visualstudio.targetoperatingsystem=linux" - "com.microsoft.visualstudio.targetoperatingsystem=linux"
sagamanager:
build:
args:
source: ${DOCKER_BUILD_SOURCE}
volumes:
- ~/clrdbg:/clrdbg:ro
entrypoint: tail -f /dev/null
labels:
- "com.microsoft.visualstudio.targetoperatingsystem=linux"

+ 11
- 1
docker-compose.yml View File

@ -1,6 +1,16 @@
version: '2' version: '2'
services: services:
sagamanager:
image: eshop/sagamanager
build:
context: ./src/Services/SagaManager/SagaManager
dockerfile: Dockerfile
depends_on:
- sql.data
- rabbitmq
basket.api: basket.api:
image: eshop/basket.api image: eshop/basket.api
build: build:
@ -9,7 +19,6 @@ services:
depends_on: depends_on:
- basket.data - basket.data
- identity.api - identity.api
- rabbitmq
catalog.api: catalog.api:
image: eshop/catalog.api image: eshop/catalog.api
@ -35,6 +44,7 @@ services:
dockerfile: Dockerfile dockerfile: Dockerfile
depends_on: depends_on:
- sql.data - sql.data
- rabbitmq
webspa: webspa:
image: eshop/webspa image: eshop/webspa


+ 55
- 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.26403.3
VisualStudioVersion = 15.0.26403.7
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{932D8224-11F6-4D07-B109-DA28AD288A63}" Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{932D8224-11F6-4D07-B109-DA28AD288A63}"
EndProject EndProject
@ -78,6 +78,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Payment", "Payment", "{022E
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Payment.API", "src\Services\Payment\Payment.API\Payment.API.csproj", "{1A01AF82-6FCB-464C-B39C-F127AEBD315D}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Payment.API", "src\Services\Payment\Payment.API\Payment.API.csproj", "{1A01AF82-6FCB-464C-B39C-F127AEBD315D}"
EndProject EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SagaManager", "SagaManager", "{F38B4FF0-0B49-405A-B1B4-F7A5E3BC4C4E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SagaManager", "src\Services\SagaManager\SagaManager\SagaManager.csproj", "{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}"
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
@ -1004,6 +1008,54 @@ Global
{1A01AF82-6FCB-464C-B39C-F127AEBD315D}.Release|x64.Build.0 = Release|Any CPU {1A01AF82-6FCB-464C-B39C-F127AEBD315D}.Release|x64.Build.0 = Release|Any CPU
{1A01AF82-6FCB-464C-B39C-F127AEBD315D}.Release|x86.ActiveCfg = Release|Any CPU {1A01AF82-6FCB-464C-B39C-F127AEBD315D}.Release|x86.ActiveCfg = Release|Any CPU
{1A01AF82-6FCB-464C-B39C-F127AEBD315D}.Release|x86.Build.0 = Release|Any CPU {1A01AF82-6FCB-464C-B39C-F127AEBD315D}.Release|x86.Build.0 = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|ARM.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|ARM.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|iPhone.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|x64.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|x64.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|x86.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.AppStore|x86.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|ARM.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|ARM.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|iPhone.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|x64.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|x64.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|x86.ActiveCfg = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Debug|x86.Build.0 = Debug|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|Any CPU.Build.0 = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|ARM.ActiveCfg = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|ARM.Build.0 = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|iPhone.ActiveCfg = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|iPhone.Build.0 = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|x64.ActiveCfg = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|x64.Build.0 = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|x86.ActiveCfg = Release|Any CPU
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -1041,5 +1093,7 @@ Global
{D1C47FF1-91F1-4CAF-9ABB-AD642B821502} = {FBF43D93-F2E7-4FF8-B4AB-186895949B88} {D1C47FF1-91F1-4CAF-9ABB-AD642B821502} = {FBF43D93-F2E7-4FF8-B4AB-186895949B88}
{022E145D-1593-47EE-9608-8E323D3C63F5} = {91CF7717-08AB-4E65-B10E-0B426F01E2E8} {022E145D-1593-47EE-9608-8E323D3C63F5} = {91CF7717-08AB-4E65-B10E-0B426F01E2E8}
{1A01AF82-6FCB-464C-B39C-F127AEBD315D} = {022E145D-1593-47EE-9608-8E323D3C63F5} {1A01AF82-6FCB-464C-B39C-F127AEBD315D} = {022E145D-1593-47EE-9608-8E323D3C63F5}
{F38B4FF0-0B49-405A-B1B4-F7A5E3BC4C4E} = {91CF7717-08AB-4E65-B10E-0B426F01E2E8}
{F6E0F0DD-1400-43C3-B5E0-7CC325728C47} = {F38B4FF0-0B49-405A-B1B4-F7A5E3BC4C4E}
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

+ 3
- 6
src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommandMsg.cs View File

@ -1,15 +1,12 @@
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ordering.API.Application.IntegrationCommands.Commands namespace Ordering.API.Application.IntegrationCommands.Commands
{ {
public class ConfirmGracePeriodCommandMsg : IntegrationEvent public class ConfirmGracePeriodCommandMsg : IntegrationEvent
{ {
public int OrderNumber { get; private set; }
public int OrderId { get; }
//TODO: message should change to Integration command type once command bus is implemented
public ConfirmGracePeriodCommandMsg(int orderId) =>
OrderId = orderId;
} }
} }

+ 8
- 3
src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs View File

@ -10,6 +10,7 @@ using Ordering.API.Application.IntegrationCommands.Commands;
using Ordering.Domain.Exceptions; using Ordering.Domain.Exceptions;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Ordering.API.Application.IntegrationEvents.Events;
namespace Ordering.API.Application.Sagas namespace Ordering.API.Application.Sagas
{ {
@ -73,13 +74,17 @@ namespace Ordering.API.Application.Sagas
/// <returns></returns> /// <returns></returns>
public async Task Handle(ConfirmGracePeriodCommandMsg command) public async Task Handle(ConfirmGracePeriodCommandMsg command)
{ {
var orderSaga = FindSagaById(command.OrderNumber);
var orderSaga = FindSagaById(command.OrderId);
CheckValidSagaId(orderSaga); CheckValidSagaId(orderSaga);
// TODO: This handler should change to Integration command handler type once command bus is implemented // TODO: This handler should change to Integration command handler type once command bus is implemented
// TODO: If order status is not cancelled, change state to awaitingValidation and // TODO: If order status is not cancelled, change state to awaitingValidation and
// send ConfirmOrderStockCommandMsg to Inventory api
// send ConfirmOrderStockCommandMsg to Inventory api
if (orderSaga.OrderStatus == OrderStatus.Submitted)
{
}
} }


+ 7
- 1
src/Services/Ordering/Ordering.API/Ordering.API.csproj View File

@ -13,6 +13,13 @@
<DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath> <DockerComposeProjectPath>..\..\..\..\docker-compose.dcproj</DockerComposeProjectPath>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Compile Remove="Application\IntegrationEvents\EventHandling\**" />
<Content Remove="Application\IntegrationEvents\EventHandling\**" />
<EmbeddedResource Remove="Application\IntegrationEvents\EventHandling\**" />
<None Remove="Application\IntegrationEvents\EventHandling\**" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<Content Update="web.config;"> <Content Update="web.config;">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory> <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
@ -80,7 +87,6 @@
<ItemGroup> <ItemGroup>
<Folder Include="Application\IntegrationCommands\CommandHandlers\" /> <Folder Include="Application\IntegrationCommands\CommandHandlers\" />
<Folder Include="Application\IntegrationEvents\EventHandling\" />
<Folder Include="Infrastructure\IntegrationEventMigrations\" /> <Folder Include="Infrastructure\IntegrationEventMigrations\" />
</ItemGroup> </ItemGroup>


+ 19
- 1
src/Services/Ordering/Ordering.API/Startup.cs View File

@ -1,4 +1,6 @@
namespace Microsoft.eShopOnContainers.Services.Ordering.API

namespace Microsoft.eShopOnContainers.Services.Ordering.API
{ {
using AspNetCore.Http; using AspNetCore.Http;
using Autofac; using Autofac;
@ -21,6 +23,9 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.HealthChecks; using Microsoft.Extensions.HealthChecks;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Ordering.API.Application.Commands;
using Ordering.API.Application.IntegrationCommands.Commands;
using Ordering.API.Application.IntegrationEvents.Events;
using Ordering.Infrastructure; using Ordering.Infrastructure;
using RabbitMQ.Client; using RabbitMQ.Client;
using System; using System;
@ -157,6 +162,8 @@
.UseSqlServer(Configuration["ConnectionString"], b => b.MigrationsAssembly("Ordering.API")) .UseSqlServer(Configuration["ConnectionString"], b => b.MigrationsAssembly("Ordering.API"))
.Options); .Options);
integrationEventLogContext.Database.Migrate(); integrationEventLogContext.Database.Migrate();
ConfigureEventBus(app);
} }
protected virtual void ConfigureAuth(IApplicationBuilder app) protected virtual void ConfigureAuth(IApplicationBuilder app)
@ -169,5 +176,16 @@
RequireHttpsMetadata = false RequireHttpsMetadata = false
}); });
} }
protected virtual void ConfigureEventBus(IApplicationBuilder app)
{
var confirmGracePeriodHandler = app.ApplicationServices
.GetService<IIntegrationEventHandler<ConfirmGracePeriodCommandMsg>>();
var eventBus = app.ApplicationServices
.GetRequiredService<IEventBus>();
eventBus.Subscribe<ConfirmGracePeriodCommandMsg>(confirmGracePeriodHandler);
}
} }
} }

+ 3
- 0
src/Services/SagaManager/SagaManager/.dockerignore View File

@ -0,0 +1,3 @@
*
!obj/Docker/publish/*
!obj/Docker/empty/

+ 5
- 0
src/Services/SagaManager/SagaManager/Dockerfile View File

@ -0,0 +1,5 @@
FROM microsoft/dotnet:1.1-runtime
ARG source
WORKDIR /app
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT ["dotnet", "SagaManager.dll"]

+ 16
- 0
src/Services/SagaManager/SagaManager/IntegrationEvents/ConfirmGracePeriodEvent.cs View File

@ -0,0 +1,16 @@
namespace SagaManager.IntegrationEvents
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
using System;
public class ConfirmGracePeriodEvent : IConfirmGracePeriodEvent
{
private readonly IEventBus _eventBus;
public ConfirmGracePeriodEvent(IEventBus eventBus) =>
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
public void PublishThroughEventBus(IntegrationEvent evt) => _eventBus.Publish(evt);
}
}

+ 14
- 0
src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodIntegrationEvent.cs View File

@ -0,0 +1,14 @@
namespace SagaManager.IntegrationEvents.Events
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
// Integration Events notes:
// An Event is “something that has happened in the past”, therefore its name has to be
// An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems.
public class ConfirmGracePeriodIntegrationEvent : IntegrationEvent
{
public int OrderId { get;}
public ConfirmGracePeriodIntegrationEvent(int orderId) => OrderId = orderId;
}
}

+ 9
- 0
src/Services/SagaManager/SagaManager/IntegrationEvents/IConfirmGracePeriodEvent.cs View File

@ -0,0 +1,9 @@
namespace SagaManager.IntegrationEvents
{
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
public interface IConfirmGracePeriodEvent
{
void PublishThroughEventBus(IntegrationEvent evt);
}
}

+ 65
- 0
src/Services/SagaManager/SagaManager/Program.cs View File

@ -0,0 +1,65 @@
using SagaManager.IntegrationEvents;
namespace SagaManager
{
using System.IO;
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using RabbitMQ.Client;
using Services;
public class Program
{
public static IConfigurationRoot Configuration { get; set; }
public static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
var serviceProvider = new ServiceCollection()
.AddLogging()
.AddOptions()
.Configure<SagaManagerSettings>(Configuration)
.AddSingleton<ISagaManagerService, SagaManagerService>()
.AddSingleton<IConfirmGracePeriodEvent, ConfirmGracePeriodEvent>()
.AddSingleton<IRabbitMQPersisterConnection>(sp =>
{
var settings = sp.GetRequiredService<IOptions<SagaManagerSettings>>().Value;
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersisterConnection>>();
var factory = new ConnectionFactory()
{
HostName = settings.EventBusConnection
};
return new DefaultRabbitMQPersisterConnection(factory, logger);
})
.AddSingleton<IEventBus, EventBusRabbitMQ>()
.BuildServiceProvider();
//configure console logging
serviceProvider
.GetService<ILoggerFactory>()
.AddConsole(Configuration.GetSection("Logging"))
.AddConsole(LogLevel.Debug);
var sagaManagerService = serviceProvider
.GetRequiredService<ISagaManagerService>();
while (true)
{
sagaManagerService.CheckFinishedGracePeriodOrders();
System.Threading.Thread.Sleep(30000);
}
}
}
}

+ 32
- 0
src/Services/SagaManager/SagaManager/SagaManager.csproj View File

@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="1.50.2" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Options" Version="1.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="1.1.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" />
</ItemGroup>
<ItemGroup>
<None Update=".dockerignore">
<DependentUpon>Dockerfile</DependentUpon>
</None>
</ItemGroup>
</Project>

+ 11
- 0
src/Services/SagaManager/SagaManager/SagaManagerSettings.cs View File

@ -0,0 +1,11 @@
namespace SagaManager
{
public class SagaManagerSettings
{
public string ConnectionString { get; set; }
public string EventBusConnection { get; set; }
public int GracePeriod { get; set; }
}
}

+ 7
- 0
src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs View File

@ -0,0 +1,7 @@
namespace SagaManager.Services
{
public interface ISagaManagerService
{
void CheckFinishedGracePeriodOrders();
}
}

+ 59
- 0
src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs View File

@ -0,0 +1,59 @@
namespace SagaManager.Services
{
using System.Collections.Generic;
using System.Data.SqlClient;
using Microsoft.Extensions.Options;
using Dapper;
using IntegrationEvents;
using IntegrationEvents.Events;
public class SagaManagerService : ISagaManagerService
{
private readonly SagaManagerSettings _settings;
private readonly IConfirmGracePeriodEvent _confirmGracePeriodEvent;
public SagaManagerService(IOptions<SagaManagerSettings> settings,
IConfirmGracePeriodEvent confirmGracePeriodEvent)
{
_settings = settings.Value;
_confirmGracePeriodEvent = confirmGracePeriodEvent;
}
public void CheckFinishedGracePeriodOrders()
{
var orderIds = GetFinishedGracePeriodOrders();
foreach (var orderId in orderIds)
{
Publish(orderId);
}
}
private IEnumerable<int> GetFinishedGracePeriodOrders()
{
IEnumerable<int> orderIds = new List<int>();
using (var conn = new SqlConnection(_settings.ConnectionString))
{
if (conn != null)
{
conn.Open();
orderIds = conn.Query<int>(
@"SELECT Id FROM [Microsoft.eShopOnContainers.Services.OrderingDb].[ordering].[orders]
WHERE DATEDIFF(hour, [OrderDate], GETDATE()) >= @GracePeriod
AND [OrderStatusId] = 1",
new { GracePeriod = _settings.GracePeriod });
}
}
return orderIds;
}
private void Publish(int orderId)
{
var confirmGracePeriodEvent = new ConfirmGracePeriodIntegrationEvent(orderId);
// Publish through the Event Bus and mark the saved event as published
_confirmGracePeriodEvent.PublishThroughEventBus(confirmGracePeriodEvent);
}
}
}

+ 11
- 0
src/Services/SagaManager/SagaManager/appsettings.json View File

@ -0,0 +1,11 @@
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"ConnectionString": "Server=sql.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word"
}

Loading…
Cancel
Save