Skip to content

Commit febf94b

Browse files
committed
ship football lambda match events to event bus
1 parent 08b7509 commit febf94b

6 files changed

Lines changed: 131 additions & 235 deletions

File tree

build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ lazy val football = lambda("football", "football")
317317
"io.netty" % "netty-codec-http" % nettyVersion,
318318
"io.netty" % "netty-codec-http2" % nettyVersion,
319319
"io.netty" % "netty-common" % nettyVersion,
320+
"software.amazon.awssdk" % "eventbridge" % "2.20.0"
320321
),
321322
excludeDependencies ++= Seq(
322323
ExclusionRule("org.playframework", "play-ahc-ws_2.13"),

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,26 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for CODE 1`]
458458
],
459459
},
460460
},
461+
{
462+
"Action": "events:PutEvents",
463+
"Effect": "Allow",
464+
"Resource": {
465+
"Fn::Join": [
466+
"",
467+
[
468+
"arn:aws:events:",
469+
{
470+
"Ref": "AWS::Region",
471+
},
472+
":",
473+
{
474+
"Ref": "AWS::AccountId",
475+
},
476+
":event-bus/liveactivities-eventbus-CODE",
477+
],
478+
],
479+
},
480+
},
461481
{
462482
"Action": [
463483
"dynamodb:PutItem",
@@ -1015,6 +1035,26 @@ exports[`The FootballNotificationsLambda stack matches the snapshot for PROD 1`]
10151035
],
10161036
},
10171037
},
1038+
{
1039+
"Action": "events:PutEvents",
1040+
"Effect": "Allow",
1041+
"Resource": {
1042+
"Fn::Join": [
1043+
"",
1044+
[
1045+
"arn:aws:events:",
1046+
{
1047+
"Ref": "AWS::Region",
1048+
},
1049+
":",
1050+
{
1051+
"Ref": "AWS::AccountId",
1052+
},
1053+
":event-bus/liveactivities-eventbus-PROD",
1054+
],
1055+
],
1056+
},
1057+
},
10181058
{
10191059
"Action": [
10201060
"dynamodb:PutItem",

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

Lines changed: 5 additions & 228 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ exports[`The LiveActivities stack matches the snapshot for CODE 1`] = `
77
"GuDistributionBucketParameter",
88
"GuLambdaFunction",
99
"GuLambdaFunction",
10-
"GuLambdaFunction",
1110
],
1211
"gu:cdk:version": "TEST",
1312
},
@@ -46,13 +45,16 @@ exports[`The LiveActivities stack matches the snapshot for CODE 1`] = `
4645
},
4746
"liveGameEventsTargetingA85C7B8B": {
4847
"Properties": {
49-
"Description": "Deliver live game events from PA polling lambda CODE to liveGameTestingQueue",
48+
"Description": "Deliver live match events in CODE to liveGameTestingQueue",
5049
"EventBusName": {
5150
"Ref": "EventsD32975C2",
5251
},
5352
"EventPattern": {
53+
"detail-type": [
54+
"football-match-events-with-articleId",
55+
],
5456
"source": [
55-
"pa-live-game-updates",
57+
"football-lambda",
5658
],
5759
},
5860
"State": "DISABLED",
@@ -700,231 +702,6 @@ exports[`The LiveActivities stack matches the snapshot for CODE 1`] = `
700702
},
701703
"Type": "AWS::SQS::QueuePolicy",
702704
},
703-
"liveactivitieslivegamespapollinglambda16BDC7D1": {
704-
"DependsOn": [
705-
"liveactivitieslivegamespapollinglambdaServiceRoleDefaultPolicy24985231",
706-
"liveactivitieslivegamespapollinglambdaServiceRoleA3EF1A84",
707-
],
708-
"Properties": {
709-
"Code": {
710-
"S3Bucket": {
711-
"Ref": "DistributionBucketName",
712-
},
713-
"S3Key": "mobile-notifications/CODE/liveactivities/liveactivities.jar",
714-
},
715-
"Description": "Polls PA for live game updates and routes to event bus",
716-
"Environment": {
717-
"Variables": {
718-
"APP": "liveactivities",
719-
"App": "liveactivities",
720-
"STACK": "mobile-notifications",
721-
"STAGE": "CODE",
722-
"Stack": "mobile-notifications",
723-
"Stage": "CODE",
724-
},
725-
},
726-
"FunctionName": "liveactivities-live-games-pa-polling-CODE",
727-
"Handler": "com.gu.liveactivities.PollingLiveGamesDataLambda::handleRequest",
728-
"LoggingConfig": {
729-
"LogFormat": "JSON",
730-
},
731-
"MemorySize": 1024,
732-
"Role": {
733-
"Fn::GetAtt": [
734-
"liveactivitieslivegamespapollinglambdaServiceRoleA3EF1A84",
735-
"Arn",
736-
],
737-
},
738-
"Runtime": "java11",
739-
"Tags": [
740-
{
741-
"Key": "App",
742-
"Value": "liveactivities",
743-
},
744-
{
745-
"Key": "gu:cdk:version",
746-
"Value": "TEST",
747-
},
748-
{
749-
"Key": "gu:repo",
750-
"Value": "guardian/mobile-n10n",
751-
},
752-
{
753-
"Key": "Stack",
754-
"Value": "mobile-notifications",
755-
},
756-
{
757-
"Key": "Stage",
758-
"Value": "CODE",
759-
},
760-
],
761-
"Timeout": 120,
762-
},
763-
"Type": "AWS::Lambda::Function",
764-
},
765-
"liveactivitieslivegamespapollinglambdaServiceRoleA3EF1A84": {
766-
"Properties": {
767-
"AssumeRolePolicyDocument": {
768-
"Statement": [
769-
{
770-
"Action": "sts:AssumeRole",
771-
"Effect": "Allow",
772-
"Principal": {
773-
"Service": "lambda.amazonaws.com",
774-
},
775-
},
776-
],
777-
"Version": "2012-10-17",
778-
},
779-
"ManagedPolicyArns": [
780-
{
781-
"Fn::Join": [
782-
"",
783-
[
784-
"arn:",
785-
{
786-
"Ref": "AWS::Partition",
787-
},
788-
":iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
789-
],
790-
],
791-
},
792-
],
793-
"Tags": [
794-
{
795-
"Key": "App",
796-
"Value": "liveactivities",
797-
},
798-
{
799-
"Key": "gu:cdk:version",
800-
"Value": "TEST",
801-
},
802-
{
803-
"Key": "gu:repo",
804-
"Value": "guardian/mobile-n10n",
805-
},
806-
{
807-
"Key": "Stack",
808-
"Value": "mobile-notifications",
809-
},
810-
{
811-
"Key": "Stage",
812-
"Value": "CODE",
813-
},
814-
],
815-
},
816-
"Type": "AWS::IAM::Role",
817-
},
818-
"liveactivitieslivegamespapollinglambdaServiceRoleDefaultPolicy24985231": {
819-
"Properties": {
820-
"PolicyDocument": {
821-
"Statement": [
822-
{
823-
"Action": [
824-
"s3:GetObject*",
825-
"s3:GetBucket*",
826-
"s3:List*",
827-
],
828-
"Effect": "Allow",
829-
"Resource": [
830-
{
831-
"Fn::Join": [
832-
"",
833-
[
834-
"arn:",
835-
{
836-
"Ref": "AWS::Partition",
837-
},
838-
":s3:::",
839-
{
840-
"Ref": "DistributionBucketName",
841-
},
842-
],
843-
],
844-
},
845-
{
846-
"Fn::Join": [
847-
"",
848-
[
849-
"arn:",
850-
{
851-
"Ref": "AWS::Partition",
852-
},
853-
":s3:::",
854-
{
855-
"Ref": "DistributionBucketName",
856-
},
857-
"/mobile-notifications/CODE/liveactivities/liveactivities.jar",
858-
],
859-
],
860-
},
861-
],
862-
},
863-
{
864-
"Action": "ssm:GetParametersByPath",
865-
"Effect": "Allow",
866-
"Resource": {
867-
"Fn::Join": [
868-
"",
869-
[
870-
"arn:aws:ssm:",
871-
{
872-
"Ref": "AWS::Region",
873-
},
874-
":",
875-
{
876-
"Ref": "AWS::AccountId",
877-
},
878-
":parameter/CODE/mobile-notifications/liveactivities",
879-
],
880-
],
881-
},
882-
},
883-
{
884-
"Action": [
885-
"ssm:GetParameters",
886-
"ssm:GetParameter",
887-
],
888-
"Effect": "Allow",
889-
"Resource": {
890-
"Fn::Join": [
891-
"",
892-
[
893-
"arn:aws:ssm:",
894-
{
895-
"Ref": "AWS::Region",
896-
},
897-
":",
898-
{
899-
"Ref": "AWS::AccountId",
900-
},
901-
":parameter/CODE/mobile-notifications/liveactivities/*",
902-
],
903-
],
904-
},
905-
},
906-
{
907-
"Action": "events:PutEvents",
908-
"Effect": "Allow",
909-
"Resource": {
910-
"Fn::GetAtt": [
911-
"EventsD32975C2",
912-
"Arn",
913-
],
914-
},
915-
},
916-
],
917-
"Version": "2012-10-17",
918-
},
919-
"PolicyName": "liveactivitieslivegamespapollinglambdaServiceRoleDefaultPolicy24985231",
920-
"Roles": [
921-
{
922-
"Ref": "liveactivitieslivegamespapollinglambdaServiceRoleA3EF1A84",
923-
},
924-
],
925-
},
926-
"Type": "AWS::IAM::Policy",
927-
},
928705
},
929706
}
930707
`;

cdk/lib/liveactivities.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ export class LiveActivities extends GuStack {
123123
eventBus: eventBus,
124124
description: `Deliver live match events in ${stage} to liveGameTestingQueue`,
125125
eventPattern: {
126-
source: ['pa-live-updates'],
126+
source: ['football-lambda'],
127+
detailType: ['football-match-events-with-articleId'],
127128
},
128129
enabled: false, // only enable if we want to inspect events in the development queue
129130
targets: [new SqsQueue(liveGameTestingQueue)],

football/src/main/scala/com/gu/mobile/notifications/football/Lambda.scala

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import java.util.concurrent.TimeUnit
66
import com.amazonaws.regions.Regions
77
import com.amazonaws.services.dynamodbv2.{AmazonDynamoDBAsync, AmazonDynamoDBAsyncClientBuilder}
88
import com.gu.contentapi.client.GuardianContentClient
9-
import com.gu.mobile.notifications.football.lib.{ArticleSearcher, DynamoDistinctCheck, EventConsumer, EventFilter, FootballData, NotificationHttpProvider, NotificationSender, NotificationsApiClient, PaFootballClient, SyntheticMatchEventGenerator}
9+
import com.gu.mobile.notifications.football.lib.{ArticleSearcher, DynamoDistinctCheck, EventConsumer, EventFilter, FootballData, LiveActivityPusher, NotificationHttpProvider, NotificationSender, NotificationsApiClient, PaFootballClient, SyntheticMatchEventGenerator}
1010
import com.gu.mobile.notifications.football.notificationbuilders.MatchStatusNotificationBuilder
1111
import play.api.libs.json.Json
1212

@@ -52,6 +52,8 @@ object Lambda extends Logging {
5252

5353
lazy val matchStatusNotificationBuilder = new MatchStatusNotificationBuilder(configuration.mapiHost)
5454

55+
lazy val liveActivityPusher = new LiveActivityPusher()
56+
5557
lazy val eventConsumer = new EventConsumer(matchStatusNotificationBuilder)
5658

5759
lazy val distinctCheck = new DynamoDistinctCheck(dynamoDBClient, tableName)
@@ -87,11 +89,14 @@ object Lambda extends Logging {
8789

8890
logContainer()
8991

90-
val processing = footballData.pollFootballData(getZonedDateTime())
91-
.flatMap(articleSearcher.tryToMatchWithCapiArticle)
92-
.map(_.flatMap(eventConsumer.eventsToNotifications))
93-
.flatMap(eventFilter.filterNotifications)
94-
.flatMap(notificationSender.sendNotifications)
92+
val processing = for {
93+
footballDataResult <- footballData.pollFootballData(getZonedDateTime())
94+
articleMatches <- articleSearcher.tryToMatchWithCapiArticle(footballDataResult)
95+
_ = liveActivityPusher.pushToEventbus(articleMatches)
96+
notifications = articleMatches.flatMap(eventConsumer.eventsToNotifications)
97+
filteredNotifications <- eventFilter.filterNotifications(notifications) // todo what filtering is happening.
98+
result <- notificationSender.sendNotifications(filteredNotifications)
99+
} yield result
95100

96101
// we're in a lambda so we do need to block the main thread until processing is finished
97102
val result = Await.ready(processing, Duration(40, TimeUnit.SECONDS))

0 commit comments

Comments
 (0)