Skip to content

Commit 43ee528

Browse files
authored
Live activities clean up - Fix Dynamo AttemptCalloutException (#1805)
* Increase Dynamo timeout config for cleaner lambda * Update cdk lambda execution timeout. * tidy
1 parent 00ef8bd commit 43ee528

5 files changed

Lines changed: 24 additions & 12 deletions

File tree

cdk/lib/__snapshots__/liveactivities.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ exports[`The LiveActivities stack matches the snapshot for CODE 1`] = `
853853
"Value": "CODE",
854854
},
855855
],
856-
"Timeout": 180,
856+
"Timeout": 660,
857857
},
858858
"Type": "AWS::Lambda::Function",
859859
},

cdk/lib/liveactivities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class LiveActivities extends GuStack {
198198
fileName: `${app}.jar`,
199199
runtime: Runtime.JAVA_11,
200200
memorySize: 1024,
201-
timeout: Duration.minutes(3),
201+
timeout: Duration.minutes(11),
202202
onFailure: new SqsDestination(channelCleanerDlq),
203203
retryAttempts: 2,
204204
environment: {

liveactivities/src/main/scala/com/gu/liveactivities/ChannelCleanUpLambda.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ import scala.util.{Failure, Success, Try}
1212
object ChannelCleanUpLambda extends Lambda with Logging {
1313

1414
private val channelApiClient = new ChannelApiClient(authentication, config.bundleId, config.sendingToProdServer)
15-
private val cleanUpService = new ChannelCleanUpService(repository, channelApiClient)
15+
private val dynamoRepository = getRepositoryWithCustomTimeouts(10, 40)
16+
private val cleanUpService = new ChannelCleanUpService(dynamoRepository, channelApiClient)
1617

1718
// we need a scheduledEvent input here so we have some metadata to capture in the DLQ if there is a failure.
1819
def handleRequest(input: ScheduledEvent, context: Context): Unit = {
1920
logger.info("Channel clean up lambda triggered")
2021

2122
val future = for {
2223
deletedChannels <- cleanUpService.deleteChannelsForEndedBroadcasts()
24+
_ = logger.info(s"${deletedChannels.size} channels deleted: [${deletedChannels.map(_.id).mkString(", ")}]")
2325
// orphanChannels <- deleteOrphanChannelsInAPNS() // channels that exist in APNS but corresponding mapping is missing in Dynamo
2426
// staleMappings <- markInactiveStaleMappings // mapping still "LIVE" that missed an end activity update and need to be deleted - Mapi may return channel id otherwise
2527
// orphanMappings <- markInactiveOrphanMappings // mapping still "active" but channel has been deleted.
@@ -31,7 +33,7 @@ object ChannelCleanUpLambda extends Lambda with Logging {
3133
}
3234
} yield cleanupSummary
3335

34-
Try(Await.result(future, 160.seconds)) match {
36+
Try(Await.result(future, 600.seconds)) match {
3537
case Success(_) => {
3638
logger.info("Channel clean up completed successfully")
3739
}

liveactivities/src/main/scala/com/gu/liveactivities/Lambda.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,28 @@ trait Lambda extends Logging{
2424
DefaultCredentialsProvider.builder.build(),
2525
)
2626

27-
val dynamoDbClient =
27+
private def buildDbClient(attemptTimeout: Int = 2, timeout: Int = 10): DynamoDbAsyncClient = {
2828
DynamoDbAsyncClient
2929
.builder()
3030
.credentialsProvider(credentialsv2)
3131
.region(Region.of(config.region))
3232
.overrideConfiguration(
3333
ClientOverrideConfiguration.builder()
34-
.apiCallAttemptTimeout(Duration.ofSeconds(2)) // limit for one single try
35-
.apiCallTimeout(Duration.ofSeconds(10)) // limit for the entire operation
34+
.apiCallAttemptTimeout(Duration.ofSeconds(attemptTimeout)) // limit for one single try
35+
.apiCallTimeout(Duration.ofSeconds(timeout)) // limit for the entire operation
3636
.retryStrategy(DefaultRetryStrategy.standardStrategyBuilder()
3737
.maxAttempts(3)
3838
.build())
3939
.build()
4040
)
4141
.build()
42+
}
4243

43-
val repository = new LiveActivityChannelRepository(dynamoDbClient, s"mobile-notifications-liveactivities-${config.stage}")
44+
private val dynamoTableName = s"mobile-notifications-liveactivities-${config.stage}"
45+
val repository = new LiveActivityChannelRepository(buildDbClient(), dynamoTableName)
46+
def getRepositoryWithCustomTimeouts(attemptTimeout: Int, timeout: Int): LiveActivityChannelRepository = {
47+
new LiveActivityChannelRepository(buildDbClient(attemptTimeout, timeout), dynamoTableName)
48+
}
4449

4550
private val eventBusName = s"liveactivities-eventbus-${config.stage}"
4651
lazy val liveActivityPusher = new LiveActivityPusher(eventBusName, logger)

liveactivities/src/main/scala/com/gu/liveactivities/service/ChannelCleanUpService.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ChannelCleanUpService(
2323
hasLastEvent = true,
2424
)
2525
_ = logger.info(s"Channels identified for possible deletion: ${channelsForPossibleDeletion.size}")
26-
// todo these sequence failures will not currently throw
26+
// todo this sequence failures will not currently throw
2727
_ <- Future.sequence(
2828
channelsForPossibleDeletion.map { mapping =>
2929
{
@@ -36,14 +36,19 @@ class ChannelCleanUpService(
3636
.closeChannel(mapping.channelId)
3737
.flatMap { _ =>
3838
val f = repository.updateMappingActiveChannel(mapping.id, isActive = false)
39-
channelsDeleted ++= List(mapping)
40-
logger.info(s"Channel successfully marked inactive with channel ID: ${mapping.channelId} for match ID ${mapping.id}")
39+
.map { result =>
40+
channelsDeleted ++= List(mapping)
41+
logger.info(s"Channel successfully marked inactive with channel ID: ${mapping.channelId} for match ID ${mapping.id}")
42+
result
43+
}
4144
f
4245
}
4346
.recover { case exception =>
44-
// todo Handle exception better and throw when needed. Some errorw will be handled by rest of the Cleanup tasks.
47+
// todo handle exception better and throw when needed. Some errors will be handled by rest of the Cleanup tasks.
4548
logger.error(s"Failed to delete channel with ID ${mapping.channelId} for match ID ${mapping.id} - ${exception.getMessage}")
4649
}
50+
51+
4752
}
4853
}
4954
},

0 commit comments

Comments
 (0)