2017-03-24 12:37:44 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2018-07-03 16:17:52 +09:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
2019-05-07 21:29:37 +01:00
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage;
|
2018-10-11 17:16:31 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus;
|
2017-03-24 12:37:44 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
2019-08-06 16:10:39 +02:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-10-11 17:16:31 +02:00
|
|
|
|
using Newtonsoft.Json;
|
2018-07-03 16:17:52 +09:00
|
|
|
|
using System;
|
2018-10-11 17:16:31 +02:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
2017-03-24 12:37:44 +01:00
|
|
|
|
using System.Data.Common;
|
2017-03-23 13:24:17 +01:00
|
|
|
|
using System.Linq;
|
2018-10-12 15:30:22 +02:00
|
|
|
|
using System.Reflection;
|
2017-03-23 13:24:17 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-03-24 12:37:44 +01:00
|
|
|
|
namespace Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services
|
2017-03-23 13:24:17 +01:00
|
|
|
|
{
|
2020-08-31 09:42:34 +01:00
|
|
|
|
public class IntegrationEventLogService : IIntegrationEventLogService,IDisposable
|
2017-03-23 13:24:17 +01:00
|
|
|
|
{
|
|
|
|
|
private readonly IntegrationEventLogContext _integrationEventLogContext;
|
2017-03-24 12:37:44 +01:00
|
|
|
|
private readonly DbConnection _dbConnection;
|
2018-10-12 15:30:22 +02:00
|
|
|
|
private readonly List<Type> _eventTypes;
|
2020-08-31 09:42:34 +01:00
|
|
|
|
private volatile bool disposedValue;
|
2017-03-23 13:24:17 +01:00
|
|
|
|
|
2019-08-07 17:06:20 +02:00
|
|
|
|
public IntegrationEventLogService(DbConnection dbConnection)
|
2017-03-23 13:24:17 +01:00
|
|
|
|
{
|
2018-07-03 16:17:52 +09:00
|
|
|
|
_dbConnection = dbConnection ?? throw new ArgumentNullException(nameof(dbConnection));
|
2017-03-23 13:24:17 +01:00
|
|
|
|
_integrationEventLogContext = new IntegrationEventLogContext(
|
|
|
|
|
new DbContextOptionsBuilder<IntegrationEventLogContext>()
|
2017-03-24 12:37:44 +01:00
|
|
|
|
.UseSqlServer(_dbConnection)
|
2017-03-23 13:24:17 +01:00
|
|
|
|
.Options);
|
2018-10-12 15:30:22 +02:00
|
|
|
|
|
|
|
|
|
_eventTypes = Assembly.Load(Assembly.GetEntryAssembly().FullName)
|
|
|
|
|
.GetTypes()
|
|
|
|
|
.Where(t => t.Name.EndsWith(nameof(IntegrationEvent)))
|
|
|
|
|
.ToList();
|
2017-03-23 13:24:17 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 21:29:37 +01:00
|
|
|
|
public async Task<IEnumerable<IntegrationEventLogEntry>> RetrieveEventLogsPendingToPublishAsync(Guid transactionId)
|
2018-10-11 17:16:31 +02:00
|
|
|
|
{
|
2019-05-07 21:29:37 +01:00
|
|
|
|
var tid = transactionId.ToString();
|
|
|
|
|
|
2019-08-07 17:06:20 +02:00
|
|
|
|
var result = await _integrationEventLogContext.IntegrationEventLogs
|
|
|
|
|
.Where(e => e.TransactionId == tid && e.State == EventStateEnum.NotPublished).ToListAsync();
|
|
|
|
|
|
|
|
|
|
if(result != null && result.Any()){
|
|
|
|
|
return result.OrderBy(o => o.CreationTime)
|
|
|
|
|
.Select(e => e.DeserializeJsonContent(_eventTypes.Find(t=> t.Name == e.EventTypeShortName)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new List<IntegrationEventLogEntry>();
|
2018-10-11 17:16:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-07 21:29:37 +01:00
|
|
|
|
public Task SaveEventAsync(IntegrationEvent @event, IDbContextTransaction transaction)
|
2017-03-23 13:24:17 +01:00
|
|
|
|
{
|
2019-05-07 21:29:37 +01:00
|
|
|
|
if (transaction == null) throw new ArgumentNullException(nameof(transaction));
|
2018-07-03 16:17:52 +09:00
|
|
|
|
|
2019-08-07 17:06:20 +02:00
|
|
|
|
var eventLogEntry = new IntegrationEventLogEntry(@event, transaction.TransactionId);
|
2018-07-03 16:17:52 +09:00
|
|
|
|
|
2019-05-07 21:29:37 +01:00
|
|
|
|
_integrationEventLogContext.Database.UseTransaction(transaction.GetDbTransaction());
|
2017-03-23 13:24:17 +01:00
|
|
|
|
_integrationEventLogContext.IntegrationEventLogs.Add(eventLogEntry);
|
|
|
|
|
|
|
|
|
|
return _integrationEventLogContext.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 17:16:31 +02:00
|
|
|
|
public Task MarkEventAsPublishedAsync(Guid eventId)
|
|
|
|
|
{
|
|
|
|
|
return UpdateEventStatus(eventId, EventStateEnum.Published);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task MarkEventAsInProgressAsync(Guid eventId)
|
2017-03-23 13:24:17 +01:00
|
|
|
|
{
|
2018-10-11 17:16:31 +02:00
|
|
|
|
return UpdateEventStatus(eventId, EventStateEnum.InProgress);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task MarkEventAsFailedAsync(Guid eventId)
|
|
|
|
|
{
|
|
|
|
|
return UpdateEventStatus(eventId, EventStateEnum.PublishedFailed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Task UpdateEventStatus(Guid eventId, EventStateEnum status)
|
|
|
|
|
{
|
|
|
|
|
var eventLogEntry = _integrationEventLogContext.IntegrationEventLogs.Single(ie => ie.EventId == eventId);
|
|
|
|
|
eventLogEntry.State = status;
|
|
|
|
|
|
|
|
|
|
if(status == EventStateEnum.InProgress)
|
|
|
|
|
eventLogEntry.TimesSent++;
|
2017-03-23 13:24:17 +01:00
|
|
|
|
|
|
|
|
|
_integrationEventLogContext.IntegrationEventLogs.Update(eventLogEntry);
|
|
|
|
|
|
|
|
|
|
return _integrationEventLogContext.SaveChangesAsync();
|
|
|
|
|
}
|
2020-08-31 09:42:34 +01:00
|
|
|
|
|
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!disposedValue)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
_integrationEventLogContext?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
disposedValue = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(disposing: true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
2017-03-23 13:24:17 +01:00
|
|
|
|
}
|
|
|
|
|
}
|