Skip to content

Commit 56f3a6b

Browse files
committed
move background task
1 parent 2891044 commit 56f3a6b

File tree

21 files changed

+407
-29
lines changed

21 files changed

+407
-29
lines changed

eShopOnContainers-ServicesAndWebApps.sln

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{3ABEEE
161161
src\ApiGateways\Envoy\config\envoy.yaml = src\ApiGateways\Envoy\config\envoy.yaml
162162
EndProjectSection
163163
EndProject
164+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ordering.BackgroundTasks", "src\Services\Ordering\Ordering.BackgroundTasks\Ordering.BackgroundTasks.csproj", "{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}"
165+
EndProject
164166
Global
165167
GlobalSection(SolutionConfigurationPlatforms) = preSolution
166168
Ad-Hoc|Any CPU = Ad-Hoc|Any CPU
@@ -1759,6 +1761,54 @@ Global
17591761
{56C2EF0B-6BF2-41D9-BE07-6E6D08D06B35}.Release|x64.Build.0 = Release|Any CPU
17601762
{56C2EF0B-6BF2-41D9-BE07-6E6D08D06B35}.Release|x86.ActiveCfg = Release|Any CPU
17611763
{56C2EF0B-6BF2-41D9-BE07-6E6D08D06B35}.Release|x86.Build.0 = Release|Any CPU
1764+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
1765+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
1766+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
1767+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|ARM.Build.0 = Debug|Any CPU
1768+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
1769+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
1770+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1771+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
1772+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|x64.ActiveCfg = Debug|Any CPU
1773+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|x64.Build.0 = Debug|Any CPU
1774+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|x86.ActiveCfg = Debug|Any CPU
1775+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Ad-Hoc|x86.Build.0 = Debug|Any CPU
1776+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
1777+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|Any CPU.Build.0 = Debug|Any CPU
1778+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|ARM.ActiveCfg = Debug|Any CPU
1779+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|ARM.Build.0 = Debug|Any CPU
1780+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
1781+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|iPhone.Build.0 = Debug|Any CPU
1782+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1783+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
1784+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|x64.ActiveCfg = Debug|Any CPU
1785+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|x64.Build.0 = Debug|Any CPU
1786+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|x86.ActiveCfg = Debug|Any CPU
1787+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.AppStore|x86.Build.0 = Debug|Any CPU
1788+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1789+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|Any CPU.Build.0 = Debug|Any CPU
1790+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|ARM.ActiveCfg = Debug|Any CPU
1791+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|ARM.Build.0 = Debug|Any CPU
1792+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|iPhone.ActiveCfg = Debug|Any CPU
1793+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|iPhone.Build.0 = Debug|Any CPU
1794+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
1795+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
1796+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|x64.ActiveCfg = Debug|Any CPU
1797+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|x64.Build.0 = Debug|Any CPU
1798+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|x86.ActiveCfg = Debug|Any CPU
1799+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Debug|x86.Build.0 = Debug|Any CPU
1800+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|Any CPU.ActiveCfg = Release|Any CPU
1801+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|Any CPU.Build.0 = Release|Any CPU
1802+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|ARM.ActiveCfg = Release|Any CPU
1803+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|ARM.Build.0 = Release|Any CPU
1804+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|iPhone.ActiveCfg = Release|Any CPU
1805+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|iPhone.Build.0 = Release|Any CPU
1806+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
1807+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
1808+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|x64.ActiveCfg = Release|Any CPU
1809+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|x64.Build.0 = Release|Any CPU
1810+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|x86.ActiveCfg = Release|Any CPU
1811+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191}.Release|x86.Build.0 = Release|Any CPU
17621812
EndGlobalSection
17631813
GlobalSection(SolutionProperties) = preSolution
17641814
HideSolutionNode = FALSE
@@ -1826,6 +1876,7 @@ Global
18261876
{56C2EF0B-6BF2-41D9-BE07-6E6D08D06B35} = {68F5041D-51F2-4630-94B6-B49789F5E51A}
18271877
{882A8F3A-C61F-4C44-86DD-A5A258714BF2} = {77849D35-37D4-4802-81DC-9477B2775A40}
18281878
{3ABEEE8C-35E0-4185-9825-C44326151F5B} = {882A8F3A-C61F-4C44-86DD-A5A258714BF2}
1879+
{D4DBA4A3-E4A5-4D9D-8ACF-F38F7D506191} = {0BD0DB92-2D98-44D9-9AC0-C59186D59B0B}
18291880
EndGlobalSection
18301881
GlobalSection(ExtensibilityGlobals) = postSolution
18311882
SolutionGuid = {25728519-5F0F-4973-8A64-0A81EB4EA8D9}

src/ApiGateways/Web.Bff.Shopping/aggregator/Controllers/BasketController.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,27 @@ public async Task<ActionResult> AddBasketItemAsync([FromBody] AddBasketItemReque
117117

118118
// Step 2: Get current basket status
119119
var currentBasket = (await _basket.GetByIdAsync(data.BasketId)) ?? new BasketData(data.BasketId);
120-
// Step 3: Merge current status with new product
121-
currentBasket.Items.Add(new BasketDataItem()
122-
{
123-
UnitPrice = item.Price,
124-
PictureUrl = item.PictureUri,
125-
ProductId = item.Id.ToString(),
126-
ProductName = item.Name,
127-
Quantity = data.Quantity,
128-
Id = Guid.NewGuid().ToString()
129-
});
130-
131-
// Step 4: Update basket
120+
// Step 3: Search if exist product into basket
121+
var product = currentBasket.Items.SingleOrDefault(i => i.ProductId == item.Id.ToString());
122+
123+
if(product != null){
124+
// Step 4: Update quantity for product
125+
product.Quantity += data.Quantity;
126+
}
127+
else{
128+
// Step 4: Merge current status with new product
129+
currentBasket.Items.Add(new BasketDataItem()
130+
{
131+
UnitPrice = item.Price,
132+
PictureUrl = item.PictureUri,
133+
ProductId = item.Id.ToString(),
134+
ProductName = item.Name,
135+
Quantity = data.Quantity,
136+
Id = Guid.NewGuid().ToString()
137+
});
138+
}
139+
140+
// Step 5: Update basket
132141
await _basket.UpdateAsync(currentBasket);
133142

134143
return Ok();

src/BuildingBlocks/EventBus/IntegrationEventLogEF/IntegrationEventLogEntry.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
using System.Linq;
77
using System.ComponentModel.DataAnnotations.Schema;
88
using System.Reflection;
9+
using Microsoft.Extensions.Logging;
10+
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
911

1012
namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF
1113
{
1214
public class IntegrationEventLogEntry
1315
{
1416
private IntegrationEventLogEntry() { }
15-
public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId)
17+
public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId, ILogger<IntegrationEventLogService> logger)
1618
{
1719
EventId = @event.Id;
1820
CreationTime = @event.CreationDate;
@@ -21,8 +23,8 @@ public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId)
2123
State = EventStateEnum.NotPublished;
2224
TimesSent = 0;
2325
TransactionId = transactionId.ToString();
24-
}
2526

27+
}
2628
public Guid EventId { get; private set; }
2729
public string EventTypeName { get; private set; }
2830
[NotMapped]
@@ -35,8 +37,10 @@ public IntegrationEventLogEntry(IntegrationEvent @event, Guid transactionId)
3537
public string Content { get; private set; }
3638
public string TransactionId { get; private set; }
3739

38-
public IntegrationEventLogEntry DeserializeJsonContent(Type type)
40+
public IntegrationEventLogEntry DeserializeJsonContent(Type type, ILogger<IntegrationEventLogService> logger)
3941
{
42+
logger.LogInformation("----- DeserializeJsonContent {Content} {Type}", Content, type);
43+
4044
IntegrationEvent = JsonConvert.DeserializeObject(Content, type) as IntegrationEvent;
4145
return this;
4246
}

src/BuildingBlocks/EventBus/IntegrationEventLogEF/Services/IntegrationEventLogService.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.EntityFrameworkCore.Storage;
44
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
55
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
6+
using Microsoft.Extensions.Logging;
67
using Newtonsoft.Json;
78
using System;
89
using System.Collections;
@@ -17,11 +18,13 @@ namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Servi
1718
public class IntegrationEventLogService : IIntegrationEventLogService
1819
{
1920
private readonly IntegrationEventLogContext _integrationEventLogContext;
21+
private readonly ILogger<IntegrationEventLogService> _logger;
2022
private readonly DbConnection _dbConnection;
2123
private readonly List<Type> _eventTypes;
2224

23-
public IntegrationEventLogService(DbConnection dbConnection)
25+
public IntegrationEventLogService(DbConnection dbConnection, ILogger<IntegrationEventLogService> logger)
2426
{
27+
_logger =logger;
2528
_dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection));
2629
_integrationEventLogContext = new IntegrationEventLogContext(
2730
new DbContextOptionsBuilder<IntegrationEventLogContext>()
@@ -37,20 +40,22 @@ public IntegrationEventLogService(DbConnection dbConnection)
3740

3841
public async Task<IEnumerable<IntegrationEventLogEntry>> RetrieveEventLogsPendingToPublishAsync(Guid transactionId)
3942
{
43+
_logger.LogInformation("----- RetrieveEventLogsPendingToPublishAsync {TransactionId}", transactionId);
44+
4045
var tid = transactionId.ToString();
4146

4247
return await _integrationEventLogContext.IntegrationEventLogs
4348
.Where(e => e.TransactionId == tid && e.State == EventStateEnum.NotPublished)
4449
.OrderBy(o => o.CreationTime)
45-
.Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName)))
50+
.Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName), _logger))
4651
.ToListAsync();
4752
}
4853

4954
public Task SaveEventAsync(IntegrationEvent @event, IDbContextTransaction transaction)
5055
{
5156
if (transaction == null) throw new ArgumentNullException(nameof(transaction));
5257

53-
var eventLogEntry = new IntegrationEventLogEntry(@event, transaction.TransactionId);
58+
var eventLogEntry = new IntegrationEventLogEntry(@event, transaction.TransactionId, _logger);
5459

5560
_integrationEventLogContext.Database.UseTransaction(transaction.GetDbTransaction());
5661
_integrationEventLogContext.IntegrationEventLogs.Add(eventLogEntry);

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ public static IServiceCollection AddSwagger(this IServiceCollection services, IC
277277
public static IServiceCollection AddIntegrationServices(this IServiceCollection services, IConfiguration configuration)
278278
{
279279
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
280-
sp => (DbConnection c) => new IntegrationEventLogService(c));
280+
sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService<ILogger<IntegrationEventLogService>>()));
281281

282282
services.AddTransient<ICatalogIntegrationEventService, CatalogIntegrationEventService>();
283283

src/Services/Ordering/Ordering.API/Application/Behaviors/TransactionBehaviour.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ await strategy.ExecuteAsync(async () =>
5656
await _dbContext.CommitTransactionAsync(transaction);
5757

5858
transactionId = transaction.TransactionId;
59+
60+
_logger.LogInformation("----- End transaction {TransactionId} for {CommandName}", transaction.TransactionId, typeName);
5961
}
6062

6163
await _orderingIntegrationEventService.PublishEventsThroughEventBusAsync(transactionId);

src/Services/Ordering/Ordering.API/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Logging;
99
using Microsoft.Extensions.Options;
10-
using Ordering.API.Infrastructure.Tasks;
1110
using Serilog;
1211
using System;
1312
using System.IO;
@@ -63,7 +62,6 @@ private static IWebHost BuildWebHost(IConfiguration configuration, string[] args
6362
WebHost.CreateDefaultBuilder(args)
6463
.CaptureStartupErrors(false)
6564
.UseStartup<Startup>()
66-
.ConfigureServices(service => service.AddHostedService<GracePeriodManagerService>())
6765
.UseApplicationInsights()
6866
.UseContentRoot(Directory.GetCurrentDirectory())
6967
.UseConfiguration(configuration)

src/Services/Ordering/Ordering.API/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public static IServiceCollection AddCustomIntegrations(this IServiceCollection s
273273
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
274274
services.AddTransient<IIdentityService, IdentityService>();
275275
services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
276-
sp => (DbConnection c) => new IntegrationEventLogService(c));
276+
sp => (DbConnection c) => new IntegrationEventLogService(c, sp.GetService<ILogger<IntegrationEventLogService>>()));
277277

278278
services.AddTransient<IOrderingIntegrationEventService, OrderingIntegrationEventService>();
279279

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Ordering.BackgroundTasks
2+
{
3+
public class BackgroundTaskSettings
4+
{
5+
public string ConnectionString { get; set; }
6+
7+
public string EventBusConnection { get; set; }
8+
9+
public int GracePeriodTime { get; set; }
10+
11+
public int CheckUpdateTime { get; set; }
12+
13+
public string SubscriptionClientName { get; set; }
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
FROM mcr.microsoft.com/dotnet/core/runtime:3.0-buster-slim AS base
2+
WORKDIR /app
3+
4+
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
5+
WORKDIR /src
6+
COPY ["src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj", "src/Services/Ordering/Ordering.BackgroundTasks/"]
7+
RUN dotnet restore "src/Services/Ordering/Ordering.BackgroundTasks/Ordering.BackgroundTasks.csproj"
8+
COPY . .
9+
WORKDIR "/src/src/Services/Ordering/Ordering.BackgroundTasks"
10+
RUN dotnet build "Ordering.BackgroundTasks.csproj" -c Release -o /app
11+
12+
FROM build AS publish
13+
RUN dotnet publish "Ordering.BackgroundTasks.csproj" -c Release -o /app
14+
15+
FROM base AS final
16+
WORKDIR /app
17+
COPY --from=publish /app .
18+
ENTRYPOINT ["dotnet", "Ordering.BackgroundTasks.dll"]

0 commit comments

Comments
 (0)