move background task
This commit is contained in:
parent
2891044c9c
commit
56f3a6b82e
@ -161,6 +161,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{3ABEEE
|
||||
src\ApiGateways\Envoy\config\envoy.yaml = src\ApiGateways\Envoy\config\envoy.yaml
|
||||
EndProjectSection
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ordering.BackgroundTasks", "src\Services\Ordering\Ordering.BackgroundTasks\Ordering.BackgroundTasks.csproj", "{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
|
||||
@ -1759,6 +1761,54 @@ Global
|
||||
{56C2EF0B-6BF2-41D9-BE07-6E6D08D06B35}.Release|x64.Build.0 = Release|Any CPU
|
||||
{56C2EF0B-6BF2-41D9-BE07-6E6D08D06B35}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{56C2EF0B-6BF2-41D9-BE07-6E6D08D06B35}.Release|x86.Build.0 = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|ARM.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|ARM.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|iPhone.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|x64.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|x64.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|x86.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|x86.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|ARM.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|ARM.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|iPhone.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|iPhone.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|x64.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|x86.Build.0 = Debug|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|ARM.ActiveCfg = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|ARM.Build.0 = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|iPhone.ActiveCfg = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|iPhone.Build.0 = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|x64.ActiveCfg = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|x64.Build.0 = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|x86.ActiveCfg = Release|Any CPU
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|x86.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@ -1826,6 +1876,7 @@ Global
|
||||
{56C2EF0B-6BF2-41D9-BE07-6E6D08D06B35} = {68F5041D-51F2-4630-94B6-B49789F5E51A}
|
||||
{882A8F3A-C61F-4C44-86DD-A5A258714BF2} = {77849D35-37D4-4802-81DC-9477B2775A40}
|
||||
{3ABEEE8C-35E0-4185-9825-C44326151F5B} = {882A8F3A-C61F-4C44-86DD-A5A258714BF2}
|
||||
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {25728519-5F0F-4973-8A64-0A81EB4EA8D9}
|
||||
|
@ -117,18 +117,27 @@ namespace Microsoft.eShopOnContainers.Web.Shopping.HttpAggregator.Controllers
|
||||
|
||||
// Step 2: Get current basket status
|
||||
var currentBasket = (await _basket.GetByIdAsync(data.BasketId)) ?? new BasketData(data.BasketId);
|
||||
// Step 3: Merge current status with new product
|
||||
currentBasket.Items.Add(new BasketDataItem()
|
||||
{
|
||||
UnitPrice = item.Price,
|
||||
PictureUrl = item.PictureUri,
|
||||
ProductId = item.Id.ToString(),
|
||||
ProductName = item.Name,
|
||||
Quantity = data.Quantity,
|
||||
Id = Guid.NewGuid().ToString()
|
||||
});
|
||||
|
||||
// Step 4: Update basket
|
||||
// Step 3: Search if exist product into basket
|
||||
var product = currentBasket.Items.SingleOrDefault(i => i.ProductId == item.Id.ToString());
|
||||
|
||||
if(product != null){
|
||||
// Step 4: Update quantity for product
|
||||
product.Quantity += data.Quantity;
|
||||
}
|
||||
else{
|
||||
// Step 4: Merge current status with new product
|
||||
currentBasket.Items.Add(new BasketDataItem()
|
||||
{
|
||||
UnitPrice = item.Price,
|
||||
PictureUrl = item.PictureUri,
|
||||
ProductId = item.Id.ToString(),
|
||||
ProductName = item.Name,
|
||||
Quantity = data.Quantity,
|
||||
Id = Guid.NewGuid().ToString()
|
||||
});
|
||||
}
|
||||
|
||||
// Step 5: Update basket
|
||||
await _basket.UpdateAsync(currentBasket);
|
||||
|
||||
return Ok();
|
||||
|
@ -6,13 +6,15 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
using System.Linq;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
|
||||
|
||||
namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF
|
||||
{
|
||||
public class IntegrationEventLogEntry
|
||||
{
|
||||
private IntegrationEventLogEntry() { }
|
||||
public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId)
|
||||
public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId, ILogger<IntegrationEventLogService> logger)
|
||||
{
|
||||
EventId = @event.Id;
|
||||
CreationTime = @event.CreationDate;
|
||||
@ -21,8 +23,8 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF
|
||||
State = EventStateEnum.NotPublished;
|
||||
TimesSent = 0;
|
||||
TransactionId = transactionId.ToString();
|
||||
}
|
||||
|
||||
}
|
||||
public Guid EventId { get; private set; }
|
||||
public string EventTypeName { get; private set; }
|
||||
[NotMapped]
|
||||
@ -35,8 +37,10 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF
|
||||
public string Content { get; private set; }
|
||||
public string TransactionId { get; private set; }
|
||||
|
||||
public IntegrationEventLogEntry DeserializeJsonContent(Type type)
|
||||
public IntegrationEventLogEntry DeserializeJsonContent(Type type, ILogger<IntegrationEventLogService> logger)
|
||||
{
|
||||
logger.LogInformation("----- DeserializeJsonContent {Content} {Type}", Content, type);
|
||||
|
||||
IntegrationEvent = JsonConvert.DeserializeObject(Content, type) as IntegrationEvent;
|
||||
return this;
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using Microsoft.EntityFrameworkCore.Storage;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections;
|
||||
@ -17,11 +18,13 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi
|
||||
public class IntegrationEventLogService : IIntegrationEventLogService
|
||||
{
|
||||
private readonly IntegrationEventLogContext _integrationEventLogContext;
|
||||
private readonly ILogger<IntegrationEventLogService> _logger;
|
||||
private readonly DbConnection _dbConnection;
|
||||
private readonly List<Type> _eventTypes;
|
||||
|
||||
public IntegrationEventLogService(DbConnection dbConnection)
|
||||
public IntegrationEventLogService(DbConnection dbConnection, ILogger<IntegrationEventLogService> logger)
|
||||
{
|
||||
_logger =logger;
|
||||
_dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection));
|
||||
_integrationEventLogContext = new IntegrationEventLogContext(
|
||||
new DbContextOptionsBuilder<IntegrationEventLogContext>()
|
||||
@ -37,12 +40,14 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi
|
||||
|
||||
public async Task<IEnumerable<IntegrationEventLogEntry>> RetrieveEventLogsPendingToPublishAsync(Guid transactionId)
|
||||
{
|
||||
_logger.LogInformation("----- RetrieveEventLogsPendingToPublishAsync {TransactionId}", transactionId);
|
||||
|
||||
var tid = transactionId.ToString();
|
||||
|
||||
return await _integrationEventLogContext.IntegrationEventLogs
|
||||
.Where(e => e.TransactionId == tid && e.State == EventStateEnum.NotPublished)
|
||||
.OrderBy(o => o.CreationTime)
|
||||
.Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName)))
|
||||
.Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName), _logger))
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
@ -50,7 +55,7 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi
|
||||
{
|
||||
if (transaction == null) throw new ArgumentNullException(nameof(transaction));
|
||||
|
||||
var eventLogEntry = new IntegrationEventLogEntry(@event, transaction.TransactionId);
|
||||
var eventLogEntry = new IntegrationEventLogEntry(@event, transaction.TransactionId, _logger);
|
||||
|
||||
_integrationEventLogContext.Database.UseTransaction(transaction.GetDbTransaction());
|
||||
_integrationEventLogContext.IntegrationEventLogs.Add(eventLogEntry);
|
||||
|
@ -277,7 +277,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API
|
||||
public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
||||
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
||||
sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService<ILogger<IntegrationEventLogService>>()));
|
||||
|
||||
services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>();
|
||||
|
||||
|
@ -56,6 +56,8 @@ namespace Ordering.API.Application.Behaviors
|
||||
await _dbContext.CommitTransactionAsync(transaction);
|
||||
|
||||
transactionId = transaction.TransactionId;
|
||||
|
||||
_logger.LogInformation("----- End transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName);
|
||||
}
|
||||
|
||||
await _orderingIntegrationEventService.PublishEventsThroughEventBusAsync(transactionId);
|
||||
|
@ -7,7 +7,6 @@ using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Ordering.API.Infrastructure.Tasks;
|
||||
using Serilog;
|
||||
using System;
|
||||
using System.IO;
|
||||
@ -63,7 +62,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API
|
||||
WebHost.CreateDefaultBuilder(args)
|
||||
.CaptureStartupErrors(false)
|
||||
.UseStartup<Startup>()
|
||||
.ConfigureServices(service => service.AddHostedService<GracePeriodManagerService>())
|
||||
.UseApplicationInsights()
|
||||
.UseContentRoot(Directory.GetCurrentDirectory())
|
||||
.UseConfiguration(configuration)
|
||||
|
@ -273,7 +273,7 @@
|
||||
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
||||
services.AddTransient<IIdentityService, IdentityService>();
|
||||
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
||||
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
||||
sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService<ILogger<IntegrationEventLogService>>()));
|
||||
|
||||
services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
|
||||
|
||||
|
@ -0,0 +1,15 @@
|
||||
namespace Ordering.BackgroundTasks
|
||||
{
|
||||
public class BackgroundTaskSettings
|
||||
{
|
||||
public string ConnectionString { get; set; }
|
||||
|
||||
public string EventBusConnection { get; set; }
|
||||
|
||||
public int GracePeriodTime { get; set; }
|
||||
|
||||
public int CheckUpdateTime { get; set; }
|
||||
|
||||
public string SubscriptionClientName { get; set; }
|
||||
}
|
||||
}
|
18
src/Services/Ordering/Ordering.BackgroundTasks/Dockerfile
Normal file
18
src/Services/Ordering/Ordering.BackgroundTasks/Dockerfile
Normal file
@ -0,0 +1,18 @@
|
||||
FROM mcr.microsoft.com/dotnet/core/runtime:3.0-buster-slim AS base
|
||||
WORKDIR /app
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
|
||||
WORKDIR /src
|
||||
COPY ["src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj", "src/Services/Ordering/Ordering.BackgroundTasks/"]
|
||||
RUN dotnet restore "src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/src/Services/Ordering/Ordering.BackgroundTasks"
|
||||
RUN dotnet build "Ordering.BackgroundTasks.csproj" -c Release -o /app
|
||||
|
||||
FROM build AS publish
|
||||
RUN dotnet publish "Ordering.BackgroundTasks.csproj" -c Release -o /app
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app .
|
||||
ENTRYPOINT ["dotnet", "Ordering.BackgroundTasks.dll"]
|
@ -0,0 +1,12 @@
|
||||
namespace Ordering.BackgroundTasks.Events
|
||||
{
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||
|
||||
public class GracePeriodConfirmedIntegrationEvent : IntegrationEvent
|
||||
{
|
||||
public int OrderId { get; }
|
||||
|
||||
public GracePeriodConfirmedIntegrationEvent(int orderId) =>
|
||||
OrderId = orderId;
|
||||
}
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
using Autofac;
|
||||
using Microsoft.Azure.ServiceBus;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBusServiceBus;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Diagnostics.HealthChecks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using RabbitMQ.Client;
|
||||
using Serilog;
|
||||
|
||||
namespace Ordering.BackgroundTasks.Extensions
|
||||
{
|
||||
public static class CustomExtensionMethods
|
||||
{
|
||||
public static IServiceCollection AddCustomHealthCheck(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var hcBuilder = services.AddHealthChecks();
|
||||
|
||||
hcBuilder.AddCheck("self", () => HealthCheckResult.Healthy());
|
||||
|
||||
hcBuilder.AddSqlServer(
|
||||
configuration["ConnectionString"],
|
||||
name: "OrderingTaskDB-check",
|
||||
tags: new string[] { "orderingtaskdb" });
|
||||
|
||||
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
||||
{
|
||||
hcBuilder.AddAzureServiceBusTopic(
|
||||
configuration["EventBusConnection"],
|
||||
topicName: "eshop_event_bus",
|
||||
name: "orderingtask-servicebus-check",
|
||||
tags: new string[] { "servicebus" });
|
||||
}
|
||||
else
|
||||
{
|
||||
hcBuilder.AddRabbitMQ(
|
||||
$"amqp://{configuration["EventBusConnection"]}",
|
||||
name: "orderingtask-rabbitmqbus-check",
|
||||
tags: new string[] { "rabbitmqbus" });
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddEventBus(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
var subscriptionClientName = configuration["SubscriptionClientName"];
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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<IRabbitMQPersistentConnection>(sp =>
|
||||
{
|
||||
var logger = sp.GetRequiredService<ILogger<DefaultRabbitMQPersistentConnection>>();
|
||||
|
||||
var factory = new ConnectionFactory()
|
||||
{
|
||||
HostName = configuration["EventBusConnection"],
|
||||
DispatchConsumersAsync = true
|
||||
};
|
||||
|
||||
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);
|
||||
});
|
||||
|
||||
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>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static ILoggingBuilder UseSerilog(this ILoggingBuilder builder, IConfiguration configuration)
|
||||
{
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.MinimumLevel.Verbose()
|
||||
.Enrich.WithProperty("ApplicationContext", "BackgroundTasks")
|
||||
.Enrich.FromLogContext()
|
||||
.WriteTo.Console()
|
||||
.ReadFrom.Configuration(configuration)
|
||||
.CreateLogger();
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Worker">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>$(NetCoreTargetVersion)</TargetFramework>
|
||||
<UserSecretsId>dotnet-Ordering.BackgroundTasks-9D3E1DD6-405B-447F-8AAB-1708B36D260E</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<LangVersion>$(LangVersion)</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="$(Microsoft_Extensions_Hosting)" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="$(Microsoft_VisualStudio_Azure_Containers_Tools_Targets)" />
|
||||
<PackageReference Include="Dapper" Version="$(Dapper)" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="$(Serilog_AspNetCore)" />
|
||||
<PackageReference Include="Serilog.Enrichers.Environment" Version="$(Serilog_Enrichers_Environment)" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="$(Serilog_Settings_Configuration)" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="$(Serilog_Sinks_Console)" />
|
||||
<PackageReference Include="Serilog.Sinks.Seq" Version="$(Serilog_Sinks_Seq)" />
|
||||
<PackageReference Include="Serilog.Sinks.Http" Version="$(Serilog_Sinks_Http)" />
|
||||
<PackageReference Include="Autofac" Version="$(Autofac)" />
|
||||
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="$(Autofac_Extensions_DependencyInjection)" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.HealthChecks" Version="$(Microsoft_AspNetCore_Diagnostics_HealthChecks)" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.AzureServiceBus" Version="$(AspNetCore_HealthChecks_AzureServiceBus)" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.Rabbitmq" Version="$(AspNetCore_HealthChecks_Rabbitmq)" />
|
||||
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" Version="$(AspNetCore_HealthChecks_SqlServer)" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusRabbitMQ\EventBusRabbitMQ.csproj" />
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBusServiceBus\EventBusServiceBus.csproj" />
|
||||
<ProjectReference Include="..\..\..\BuildingBlocks\EventBus\EventBus\EventBus.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
40
src/Services/Ordering/Ordering.BackgroundTasks/Program.cs
Normal file
40
src/Services/Ordering/Ordering.BackgroundTasks/Program.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using Autofac.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Ordering.BackgroundTasks.Extensions;
|
||||
using Ordering.BackgroundTasks.Tasks;
|
||||
using Serilog;
|
||||
using System.IO;
|
||||
|
||||
namespace Ordering.BackgroundTasks
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureAppConfiguration((hostContext, builder) =>
|
||||
{
|
||||
builder.SetBasePath(Directory.GetCurrentDirectory());
|
||||
builder.AddJsonFile("appsettings.json", optional: true);
|
||||
builder.AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true);
|
||||
builder.AddEnvironmentVariables();
|
||||
builder.AddCommandLine(args);
|
||||
})
|
||||
.ConfigureLogging((host, builder) => builder.UseSerilog(host.Configuration).AddSerilog())
|
||||
.ConfigureServices((host, services) =>
|
||||
{
|
||||
services.AddCustomHealthCheck(host.Configuration);
|
||||
services.Configure<BackgroundTaskSettings>(host.Configuration);
|
||||
services.AddOptions();
|
||||
services.AddHostedService<GracePeriodManagerService>();
|
||||
services.AddEventBus(host.Configuration);
|
||||
services.AddAutofac(container => container.Populate(services));
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Ordering.BackgroundTasks": {
|
||||
"commandName": "Project",
|
||||
"environmentVariables": {
|
||||
"DOTNET_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Docker": {
|
||||
"commandName": "Docker"
|
||||
}
|
||||
}
|
||||
}
|
@ -1,25 +1,24 @@
|
||||
using Dapper;
|
||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||
using Microsoft.eShopOnContainers.Services.Ordering.API;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Ordering.API.Application.IntegrationEvents.Events;
|
||||
using Ordering.BackgroundTasks.Events;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Ordering.API.Infrastructure.Tasks
|
||||
namespace Ordering.BackgroundTasks.Tasks
|
||||
{
|
||||
public class GracePeriodManagerService : BackgroundService
|
||||
{
|
||||
private readonly ILogger<GracePeriodManagerService> _logger;
|
||||
private readonly OrderingSettings _settings;
|
||||
private readonly BackgroundTaskSettings _settings;
|
||||
private readonly IEventBus _eventBus;
|
||||
|
||||
public GracePeriodManagerService(IOptions<OrderingSettings> settings, IEventBus eventBus, ILogger<GracePeriodManagerService> logger)
|
||||
public GracePeriodManagerService(IOptions<BackgroundTaskSettings> settings, IEventBus eventBus, ILogger<GracePeriodManagerService> logger)
|
||||
{
|
||||
_settings = settings?.Value ?? throw new ArgumentNullException(nameof(settings));
|
||||
_eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
|
||||
@ -56,7 +55,7 @@ namespace Ordering.API.Infrastructure.Tasks
|
||||
{
|
||||
var confirmGracePeriodEvent = new GracePeriodConfirmedIntegrationEvent(orderId);
|
||||
|
||||
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", confirmGracePeriodEvent.Id, Program.AppName, confirmGracePeriodEvent);
|
||||
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} from {AppName} - ({@IntegrationEvent})", confirmGracePeriodEvent.Id, _settings.SubscriptionClientName, confirmGracePeriodEvent);
|
||||
|
||||
_eventBus.Publish(confirmGracePeriodEvent);
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"System": "Information",
|
||||
"Microsoft": "Information"
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
{
|
||||
"ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;",
|
||||
"Serilog": {
|
||||
"SeqServerUrl": null,
|
||||
"LogstashgUrl": null,
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.eShopOnContainers": "Information",
|
||||
"System": "Warning"
|
||||
}
|
||||
}
|
||||
},
|
||||
"SubscriptionClientName": "BackgroundTasks",
|
||||
"GracePeriodTime": "1",
|
||||
"CheckUpdateTime": "1000",
|
||||
"ApplicationInsights": {
|
||||
"InstrumentationKey": ""
|
||||
},
|
||||
"AzureServiceBusEnabled": false,
|
||||
"EventBusRetryCount": 5,
|
||||
"EventBusConnection": "",
|
||||
"EventBusUserName": "",
|
||||
"EventBusPassword": ""
|
||||
}
|
@ -291,7 +291,7 @@ namespace Webhooks.API
|
||||
public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
|
||||
sp => (DbConnection c) => new IntegrationEventLogService(c));
|
||||
sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService<ILogger<IntegrationEventLogService>>()));
|
||||
|
||||
if (configuration.GetValue<bool>("AzureServiceBusEnabled"))
|
||||
{
|
||||
|
@ -118,6 +118,7 @@ namespace eShopConContainers.WebSPA
|
||||
app.UseRouting();
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapDefaultControllerRoute();
|
||||
endpoints.MapControllers();
|
||||
endpoints.MapHealthChecks("/liveness", new HealthCheckOptions
|
||||
{
|
||||
|
@ -20,6 +20,8 @@
|
||||
<Google_Protobuf>3.9.0-rc1</Google_Protobuf>
|
||||
<Grpc_Tools>1.22.0</Grpc_Tools>
|
||||
|
||||
<Microsoft_Extensions_Hosting>3.0.0-preview7.19362.4</Microsoft_Extensions_Hosting>
|
||||
<Microsoft_VisualStudio_Azure_Containers_Tools_Targets>1.7.9</Microsoft_VisualStudio_Azure_Containers_Tools_Targets>
|
||||
<AspNetCore_HealthChecks_AzureServiceBus>2.2.0</AspNetCore_HealthChecks_AzureServiceBus>
|
||||
<AspNetCore_HealthChecks_AzureStorage>2.2.0</AspNetCore_HealthChecks_AzureStorage>
|
||||
<AspNetCore_HealthChecks_MongoDb>2.2.0</AspNetCore_HealthChecks_MongoDb>
|
||||
|
Loading…
x
Reference in New Issue
Block a user