@ -8,17 +8,21 @@ using Ordering.BackgroundTasks.IntegrationEvents;
using System ;
using System ;
using System.Collections.Generic ;
using System.Collections.Generic ;
using System.Data.SqlClient ;
using System.Data.SqlClient ;
using System.Net ;
using System.Net.Http ;
using System.Threading ;
using System.Threading ;
using System.Threading.Tasks ;
using System.Threading.Tasks ;
using System.Web ;
namespace Ordering.BackgroundTasks.Tasks
namespace Ordering.BackgroundTasks.Tasks
{
{
public class GracePeriodManagerService
public class GracePeriodManagerService
: BackgroundService
: BackgroundService
{
{
private readonly ILogger < GracePeriodManagerService > _logger ;
private readonly ILogger < GracePeriodManagerService > _logger ;
private readonly BackgroundTaskSettings _settings ;
private readonly BackgroundTaskSettings _settings ;
private readonly IEventBus _eventBus ;
private readonly IEventBus _eventBus ;
private static readonly String identityUrl = @"http://identity.api/" ;
public GracePeriodManagerService (
public GracePeriodManagerService (
IOptions < BackgroundTaskSettings > settings ,
IOptions < BackgroundTaskSettings > settings ,
@ -28,12 +32,11 @@ namespace Ordering.BackgroundTasks.Tasks
_settings = settings ? . Value ? ? throw new ArgumentNullException ( nameof ( settings ) ) ;
_settings = settings ? . Value ? ? throw new ArgumentNullException ( nameof ( settings ) ) ;
_eventBus = eventBus ? ? throw new ArgumentNullException ( nameof ( eventBus ) ) ;
_eventBus = eventBus ? ? throw new ArgumentNullException ( nameof ( eventBus ) ) ;
_logger = logger ? ? throw new ArgumentNullException ( nameof ( logger ) ) ;
_logger = logger ? ? throw new ArgumentNullException ( nameof ( logger ) ) ;
}
}
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
protected override async Task ExecuteAsync ( CancellationToken stoppingToken )
{
{
_logger . LogDebug ( "GracePeriodManagerService is starting." ) ;
_logger . LogInformation ( "GracePeriodManagerService is starting." ) ;
stoppingToken . Register ( ( ) = > _logger . LogDebug ( "#1 GracePeriodManagerService background task is stopping." ) ) ;
stoppingToken . Register ( ( ) = > _logger . LogDebug ( "#1 GracePeriodManagerService background task is stopping." ) ) ;
@ -46,27 +49,82 @@ namespace Ordering.BackgroundTasks.Tasks
await Task . Delay ( _settings . CheckUpdateTime , stoppingToken ) ;
await Task . Delay ( _settings . CheckUpdateTime , stoppingToken ) ;
}
}
_logger . LogDebug ( "GracePeriodManagerService background task is stopping." ) ;
_logger . LogInformation ( "GracePeriodManagerService background task is stopping." ) ;
await Task . CompletedTask ;
await Task . CompletedTask ;
}
}
private void CheckConfirmedGracePeriodOrders ( )
private void CheckConfirmedGracePeriodOrders ( )
{
{
_logger . LogDebug ( "Checking confirmed grace period orders" ) ;
_logger . LogInformation ( "Checking confirmed grace period orders" ) ;
var orderIds = GetConfirmedGracePeriodOrders ( ) ;
var orderIds = GetConfirmedGracePeriodOrders ( ) ;
foreach ( var orderId in orderIds )
foreach ( var orderId in orderIds )
{
{
var confirmGracePeriodEvent = new GracePeriodConfirmedIntegrationEvent ( orderId ) ;
var confirmGracePeriodEvent = new GracePeriodConfirmedIntegrationEvent ( orderId ) ;
String userName = GetUserName ( orderId ) ;
int tenantId = GetTenantId ( userName ) . Result ;
confirmGracePeriodEvent . TenantId = tenantId ;
_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 , Program . AppName , confirmGracePeriodEvent ) ;
_eventBus . Publish ( confirmGracePeriodEvent ) ;
_eventBus . Publish ( confirmGracePeriodEvent ) ;
}
}
}
}
private async Task < int > GetTenantId ( String userName )
{
var builder = new UriBuilder ( identityUrl + "api/userid" ) ;
builder . Port = - 1 ;
var query = HttpUtility . ParseQueryString ( builder . Query ) ;
query [ "userName" ] = userName ;
builder . Query = query . ToString ( ) ;
string url = builder . ToString ( ) ;
using ( var client = new HttpClient ( ) )
{
try
{
var response = await client . GetAsync ( url ) ;
string result = response . Content . ReadAsStringAsync ( ) . Result ;
return Int32 . Parse ( result ) ;
}
catch ( Exception e )
{
return 0 ;
}
}
}
private String GetUserName ( int orderId )
{
String username = "" ;
using ( var conn = new SqlConnection ( _settings . ConnectionString ) )
{
try
{
conn . Open ( ) ;
username = conn . QueryFirst < String > (
@ "SELECT Name FROM [ordering].[orders]
LEFT JOIN [ ordering ] . buyers
ON [ ordering ] . orders . BuyerId = [ ordering ] . buyers . Id
WHERE [ ordering ] . orders . Id = @OrderId ",
new { OrderId = orderId } ) ;
}
catch ( SqlException exception )
{
_logger . LogCritical ( exception , "FATAL ERROR: Database connections could not be opened: {Message}" ,
exception . Message ) ;
}
}
return username ;
}
private IEnumerable < int > GetConfirmedGracePeriodOrders ( )
private IEnumerable < int > GetConfirmedGracePeriodOrders ( )
{
{
IEnumerable < int > orderIds = new List < int > ( ) ;
IEnumerable < int > orderIds = new List < int > ( ) ;
@ -80,16 +138,16 @@ namespace Ordering.BackgroundTasks.Tasks
@ "SELECT Id FROM [ordering].[orders]
@ "SELECT Id FROM [ordering].[orders]
WHERE DATEDIFF ( minute , [ OrderDate ] , GETDATE ( ) ) > = @GracePeriodTime
WHERE DATEDIFF ( minute , [ OrderDate ] , GETDATE ( ) ) > = @GracePeriodTime
AND [ OrderStatusId ] = 1 ",
AND [ OrderStatusId ] = 1 ",
new { GracePeriodTime = _settings . GracePeriodTime } ) ;
new { GracePeriodTime = _settings . GracePeriodTime } ) ;
}
}
catch ( SqlException exception )
catch ( SqlException exception )
{
{
_logger . LogCritical ( exception , "FATAL ERROR: Database connections could not be opened: {Message}" , exception . Message ) ;
_logger . LogCritical ( exception , "FATAL ERROR: Database connections could not be opened: {Message}" ,
exception . Message ) ;
}
}
}
}
return orderIds ;
return orderIds ;
}
}
}
}
}
}