Skip to content

Commit

Permalink
handle sqs send failure
Browse files Browse the repository at this point in the history
  • Loading branch information
QuarpT committed Nov 5, 2018
1 parent ca3f8db commit 3eb51b1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package com.gu.effects.sqs
import com.amazonaws.auth._
import com.amazonaws.auth.profile.ProfileCredentialsProvider
import com.amazonaws.regions.Regions
import com.amazonaws.services.sqs.model.SendMessageRequest
import com.amazonaws.services.sqs.{AmazonSQSAsync, AmazonSQSAsyncClientBuilder}
import com.amazonaws.services.sqs.model.{SendMessageRequest, SendMessageResult}
import org.apache.log4j.Logger
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
Expand Down Expand Up @@ -44,18 +44,18 @@ object AwsSQSSend {
}
}

def sendSync(queueName: QueueName)(payload: Payload): Unit = {
def sendSync(queueName: QueueName)(payload: Payload): Try[Unit] = {
val (sqsClient: AmazonSQSAsync, queueUrl: String) = buildSqsClient(queueName)

logger.info(s"Sending message to SQS queue $queueUrl")

val messageResult = Try(sqsClient.sendMessage(new SendMessageRequest(queueUrl, payload.value)))

messageResult.foreach { result =>
logger.info(s"Successfully sent message to $queueUrl: $result")
}
messageResult.failed.foreach { throwable =>
logger.error(s"Failed to send message due to $queueUrl due to:", throwable)
Try(sqsClient.sendMessage(new SendMessageRequest(queueUrl, payload.value))) match {
case Success(result) =>
logger.info(s"Successfully sent message to $queueUrl: $result")
Success(())
case Failure(throwable) =>
logger.error(s"Failed to send message due to $queueUrl due to:", throwable)
Failure(throwable)
}

}
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/com/gu/paymentFailure/Lambda.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gu.paymentFailure

import java.io.{InputStream, OutputStream}

import com.amazonaws.services.lambda.runtime.Context
import com.gu.effects.sqs.AwsSQSSend
import com.gu.effects.sqs.AwsSQSSend.{Payload, QueueName}
Expand All @@ -16,6 +17,7 @@ import com.gu.util.reader.Types._
import com.gu.util.zuora.{ZuoraGetInvoiceTransactions, ZuoraRestConfig, ZuoraRestRequestMaker}
import okhttp3.{Request, Response}
import scalaz.\/
import scala.util.Try

object Lambda {

Expand All @@ -24,7 +26,7 @@ object Lambda {
fetchString: StringFromS3,
response: Request => Response,
lambdaIO: LambdaIO,
sqsSend: QueueName => Payload => Unit
sqsSend: QueueName => Payload => Try[Unit]
): Unit = {
val loadConfigModule = LoadConfigModule(stage, fetchString)
ApiGatewayHandler(lambdaIO)(operationForEffects(loadConfigModule[TrustedApiConfig], wiredOperation(stage, response, loadConfigModule, sqsSend)))
Expand All @@ -41,7 +43,7 @@ object Lambda {
stage: Stage,
response: Request => Response,
loadConfigModule: LoadConfigModule.PartialApply,
sqsSend: QueueName => Payload => Unit
sqsSend: QueueName => Payload => Try[Unit]
): ApiGatewayOp[ApiGatewayHandler.Operation] = {
for {
zuoraRestConfig <- loadConfigModule[ZuoraRestConfig].toApiGatewayOp("load zuora config")
Expand Down
15 changes: 12 additions & 3 deletions src/main/scala/com/gu/util/email/EmailSendSteps.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.gu.util.email

import com.gu.effects.sqs.AwsSQSSend.Payload
import com.gu.util.reader.Types.ApiGatewayOp.ContinueProcessing
import com.gu.util.apigateway.ResponseModels.ApiResponse
import com.gu.util.reader.Types.ApiGatewayOp.{ContinueProcessing, ReturnWithResponse}
import com.gu.util.reader.Types._
import play.api.libs.json.{Json, Writes, _}
import scala.util.{Failure, Success, Try}

case class ContactAttributesDef(SubscriberAttributes: SubscriberAttributesDef)

Expand Down Expand Up @@ -91,7 +93,14 @@ trait EmailSqsSerialisation {
}

object EmailSendSteps extends EmailSqsSerialisation {
def apply(sqsSend: Payload => Unit)(emailRequest: EmailMessage): ApiGatewayOp[Unit] =
ContinueProcessing(sqsSend(Payload(Json.toJson(emailRequest).toString)))
def apply(sqsSend: Payload => Try[Unit])(emailRequest: EmailMessage): ApiGatewayOp[Unit] = {
sqsSend(Payload(Json.toJson(emailRequest).toString)) match {
case Success(_) =>
ContinueProcessing(())
case Failure(_) =>
logger.error(s"failed to send $emailRequest to sqs queue")
ReturnWithResponse(ApiResponse("500", "failure trigger email"))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.gu.effects.sqs.AwsSQSSend.{Payload, QueueName}
import com.gu.util.apigateway.ApiGatewayHandler.LambdaIO
import com.gu.util.config.Stage
import org.scalatest.{FlatSpec, Matchers}
import scala.util.{Success, Try}

class EndToEndHandlerTest extends FlatSpec with Matchers {

Expand All @@ -25,7 +26,7 @@ class EndToEndHandlerTest extends FlatSpec with Matchers {
val config = new TestingRawEffects(200, EndToEndData.responses)
var capturedPayload: Option[Payload] = None

def sqsSend(queueName: QueueName)(payload: Payload): Unit = capturedPayload = Some(payload)
def sqsSend(queueName: QueueName)(payload: Payload): Try[Unit] = Success { capturedPayload = Some(payload) }

//execute
Lambda.runForLegacyTestsSeeTestingMd(
Expand Down
12 changes: 10 additions & 2 deletions src/test/scala/com/gu/util/exacttarget/EmailSendStepsTest.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.gu.util.exacttarget

import com.gu.effects.sqs.AwsSQSSend.Payload
import com.gu.util.apigateway.ResponseModels.ApiResponse
import com.gu.util.email._
import com.gu.util.reader.Types.ApiGatewayOp.ContinueProcessing
import com.gu.util.reader.Types.ApiGatewayOp.{ContinueProcessing, ReturnWithResponse}
import org.scalatest.{FlatSpec, Matchers}
import play.api.libs.json.Json
import scala.util.{Failure, Success, Try}

class EmailSendStepsTest extends FlatSpec with Matchers {

Expand Down Expand Up @@ -35,7 +37,7 @@ class EmailSendStepsTest extends FlatSpec with Matchers {

"EmailSendSteps" should "serialise and send an email" in {
var capturedPayload: Option[Payload] = None
def sqsSend(payload: Payload): Unit = capturedPayload = Some(payload)
def sqsSend(payload: Payload): Try[Unit] = Success { capturedPayload = Some(payload) }

EmailSendSteps(sqsSend)(makeMessage("james@jameson.com")) shouldBe ContinueProcessing(())
Json.parse(capturedPayload.get.value) shouldBe Json.parse(
Expand Down Expand Up @@ -66,4 +68,10 @@ class EmailSendStepsTest extends FlatSpec with Matchers {
)
}

"EmailSendSteps" should "return with response on failure" in {
def sqsSend(payload: Payload): Try[Unit] = Failure(new RuntimeException("foo"))

EmailSendSteps(sqsSend)(makeMessage("james@jameson.com")) shouldBe ReturnWithResponse(ApiResponse("500", "failure trigger email"))
}

}

0 comments on commit 3eb51b1

Please sign in to comment.