|
1 | 1 | package com.gu.mobile.notifications.football.lib |
2 | 2 |
|
| 3 | + |
| 4 | +import com.gu.mobile.notifications.football.Logging |
3 | 5 | import com.gu.mobile.notifications.football.models.FootballMatchEvent |
4 | 6 | import com.gu.mobile.notifications.football.notificationbuilders.MatchStatusLiveActivityPayloadBuilder |
5 | 7 |
|
6 | 8 | import java.util.UUID |
7 | 9 | import pa.{MatchDay, MatchEvent} |
8 | 10 |
|
9 | | -import java.time.{Instant, ZoneId, ZonedDateTime} |
| 11 | +import java.time.ZonedDateTime |
| 12 | +import scala.concurrent.duration._ |
| 13 | +import scala.concurrent.Await |
| 14 | +import scala.concurrent.ExecutionContext.Implicits.global |
| 15 | +import scala.util.control.NonFatal |
10 | 16 |
|
11 | 17 | // Synthetic events create a timeline event for a match status change, that can be processed by the EventConsumer |
12 | 18 | // and transformed into a NotificationPayload and/or LiveActivityPayload for broadcasting. |
13 | 19 |
|
14 | | -class SyntheticMatchEventGenerator(getCurrentTime: () => ZonedDateTime) { |
| 20 | +class SyntheticMatchEventGenerator(getCurrentTime: () => ZonedDateTime, matchStateDiffer: DynamoMatchStateDiffer) extends Logging { |
15 | 21 |
|
16 | 22 | def generate(events: List[MatchEvent], id: String, matchDay: MatchDay): List[MatchEvent] = { |
17 | 23 | // Live activity synthetic events are appended at the end, but when they are first generated, no other timeline events exist and duplicate events are filtered so this should not matter. |
@@ -196,17 +202,33 @@ class SyntheticMatchEventGenerator(getCurrentTime: () => ZonedDateTime) { |
196 | 202 | articleId = None |
197 | 203 | ) |
198 | 204 |
|
199 | | - // todo clean up |
200 | | - println(s"footballMatchState: $footballMatchState") |
201 | | - |
202 | | - // todo this will impact a few of the test set ups |
203 | | - val isFootballMatchStateDifferent: Boolean = false// call to DataStore with last match state sent, TRUE if diff |
204 | | - |
205 | | - if (isFootballMatchStateDifferent) Some(emptyMatchEvent.copy( |
206 | | - id = Some(UUID.nameUUIDFromBytes(s"football-match/${matchDay.id}/state-change/${latestEvent.eventTime}".getBytes).toString), |
207 | | - eventType = "state-change" |
208 | | - )) |
209 | | - else None |
| 205 | + // todo block on the async DynamoDB check but revisit making this async in future; |
| 206 | + // this will impact a few of the test set ups |
| 207 | + val isFootballMatchStateIdentical: Boolean = |
| 208 | + try { |
| 209 | + Await.result(matchStateDiffer.isIdentical(matchDay.id, footballMatchState), 5.seconds) |
| 210 | + } catch { |
| 211 | + case NonFatal(exception) => |
| 212 | + logger.error(s"Error checking for identical football match state: ${exception.getMessage}") |
| 213 | + true // assume identical if we can't check |
| 214 | + } |
| 215 | + |
| 216 | + if (!isFootballMatchStateIdentical) { |
| 217 | + // Only emit the state-change event if we successfully persist the new state, otherwise try again next polling cycle. |
| 218 | + try { |
| 219 | + Await.result(matchStateDiffer.updateState(matchDay.id, footballMatchState), 5.seconds) |
| 220 | + |
| 221 | + Some(emptyMatchEvent.copy( |
| 222 | + id = Some(UUID.nameUUIDFromBytes(s"football-match/${matchDay.id}/state-change/${latestEvent.eventTime}".getBytes).toString), |
| 223 | + eventType = "state-change" |
| 224 | + )) |
| 225 | + } catch { |
| 226 | + case NonFatal(exception) => |
| 227 | + logger.error(s"Error updating football match state, not emitting state-change event: ${exception.getMessage}") |
| 228 | + None |
| 229 | + } |
| 230 | + } |
| 231 | + else None |
210 | 232 | } |
211 | 233 |
|
212 | 234 | case Nil => None |
|
0 commit comments