Skip to content

Commit

Permalink
Merge pull request #509 from LykkeBusiness/LT-5298-looping-error-in-m…
Browse files Browse the repository at this point in the history
…t-core-requesting-to-stop-the-trading

fix(LT-5298): increase the criticality of the message when snapshot w…
  • Loading branch information
tarurar authored Mar 21, 2024
2 parents 01c66d4 + 2abed95 commit 3dc9b2c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public static bool Initialized(this IDraftSnapshotKeeper keeper)
public static bool IsPlatformClosureEvent(this MarketStateChangedEvent evt) =>
evt.Id == LykkeConstants.PlatformMarketIdentifier && !evt.IsEnabled;

public static bool IsNotPlatformClosureEvent(this MarketStateChangedEvent evt) =>
!evt.IsPlatformClosureEvent();

private static List<T> GetOrders<T>(this TradingEngineSnapshot snapshot)
{
return string.IsNullOrWhiteSpace(snapshot.OrdersJson)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,67 @@ public PlatformClosureProjection(ISnapshotService snapshotService,
[UsedImplicitly]
public async Task Handle(MarketStateChangedEvent e)
{
if (!e.IsPlatformClosureEvent())
if (e.IsNotPlatformClosureEvent())
return;

var tradingDay = DateOnly.FromDateTime(e.EventTimestamp);

string result;
try
{
result = await _snapshotService.MakeTradingDataSnapshot(e.EventTimestamp.Date,
_identityGenerator.GenerateGuid(),
SnapshotStatus.Draft);
var successMessage = await CreateDraftSnapshot(e.EventTimestamp.Date);
await LogIfSucceeded(successMessage, e);
}
catch (Exception ex)
{
await _log.WriteWarningAsync(nameof(PlatformClosureProjection),
nameof(Handle),
e.ToJson(),
$"Failed to make trading data draft snapshot for [{tradingDay}]", ex);

if (tradingDay < _dateService.NowDateOnly())
var exceptionExpected = await IsExceptionExpected(ex, e);
if (!exceptionExpected)
{
await _log.WriteWarningAsync(nameof(PlatformClosureProjection),
nameof(Handle),
e.ToJson(),
"The event is for the past date, so the snapshot draft will not be created.", ex);
return;
throw;
}

throw;
}
}

private async Task LogIfSucceeded(string successMessage, MarketStateChangedEvent evt)
{
var failed = string.IsNullOrWhiteSpace(successMessage);
if (failed) return;

await _log.WriteInfoAsync(nameof(PlatformClosureProjection),
nameof(LogIfSucceeded),
evt.ToJson(),
successMessage);
}

private async Task<string> CreateDraftSnapshot(DateTime tradingDay)
{
var result = await _snapshotService.MakeTradingDataSnapshot(tradingDay,
_identityGenerator.GenerateGuid(),
SnapshotStatus.Draft);
return result;
}

if (!string.IsNullOrWhiteSpace(result))
private async Task<bool> IsExceptionExpected(Exception ex, MarketStateChangedEvent evt)
{
if (IsEventForPastDate(evt))
{
await _log.WriteInfoAsync(nameof(PlatformClosureProjection),
nameof(Handle),
e.ToJson(),
result);
await _log.WriteWarningAsync(nameof(PlatformClosureProjection),
nameof(IsExceptionExpected),
evt.ToJson(),
"The event is for the past date, so the snapshot draft will not be created.", ex);
return true;
}

var tradingDayFromEvent = DateOnly.FromDateTime(evt.EventTimestamp);
await _log.WriteErrorAsync(nameof(PlatformClosureProjection),
nameof(IsExceptionExpected),
new {eventJson = evt.ToJson(), tradingDay = tradingDayFromEvent}.ToJson(),
ex);

return false;
}

private bool IsEventForPastDate(MarketStateChangedEvent evt)
{
var tradingDayFromEvent = DateOnly.FromDateTime(evt.EventTimestamp);
return tradingDayFromEvent < _dateService.NowDateOnly();
}
}
}

0 comments on commit 3dc9b2c

Please sign in to comment.