Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LIVE-6447 Create a connection for the processing of each SQS message #1217

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import java.time.Instant
import java.util.UUID
import java.util.concurrent.Executors
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor}
import com.gu.notifications.worker.delivery.fcm.FcmFirebase
import scala.util.Try

class AndroidSender(val config: FcmWorkerConfiguration, val firebaseAppName: Option[String], val metricNs: String) extends SenderRequestHandler[FcmClient] {

Expand All @@ -37,8 +39,10 @@ class AndroidSender(val config: FcmWorkerConfiguration, val firebaseAppName: Opt
override implicit val ioContextShift: ContextShift[IO] = IO.contextShift(ec)
override implicit val timer: Timer[IO] = IO.timer(ec)

override val deliveryService: IO[Fcm[IO]] =
FcmClient(config.fcmConfig, firebaseAppName).fold(e => IO.raiseError(e), c => IO.delay(new Fcm(c)))
val fcmFirebase: Try[FcmFirebase] = FcmFirebase(config.fcmConfig, firebaseAppName)
override val deliveryService: IO[Fcm[IO]] =
fcmFirebase.fold(e => IO.raiseError(e), c => IO.delay(new Fcm(FcmClient(c))))

override val maxConcurrency = config.concurrencyForIndividualSend
override val batchConcurrency = 100

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ trait DeliveryClient {
type Payload <: DeliveryPayload
type BatchSuccess <: BatchDeliverySuccess

def close(): Unit
def sendNotification(notificationId: UUID, token: String, payload: Payload, dryRun: Boolean)
(onComplete: Either[DeliveryException, Success] => Unit)
(implicit ece: ExecutionContextExecutor): Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ class ApnsClient(private val underlying: PushyApnsClient, val config: ApnsConfig
"DeviceTokenNotForTopic"
)

def close(): Unit = underlying.close().get

def payloadBuilder: Notification => Option[ApnsPayload] = apnsPayloadBuilder.apply _

def sendNotification(notificationId: UUID, token: String, payload: Payload, dryRun: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ class FcmClient (firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp,
ErrorCode.PERMISSION_DENIED
)

def close(): Unit = firebaseApp.delete()

private final val FCM_URL: String = s"https://fcm.googleapis.com/v1/projects/${projectId}/messages:send";

private val fcmTransport: FcmTransport = new FcmTransportJdkImpl(credential, FCM_URL, jsonFactory)
Expand Down Expand Up @@ -176,8 +174,10 @@ class FcmClient (firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp,

}

object FcmClient {
def apply(config: FcmConfig, firebaseAppName: Option[String]): Try[FcmClient] =
case class FcmFirebase(firebaseMessaging: FirebaseMessaging, firebaseApp: FirebaseApp, config: FcmConfig, projectId: String, credential: GoogleCredentials, jsonFactory: JsonFactory)

object FcmFirebase {
def apply(config: FcmConfig, firebaseAppName: Option[String]): Try[FcmFirebase] =
Try {
val credential = GoogleCredentials.fromStream(new ByteArrayInputStream(config.serviceAccountKey.getBytes))
val firebaseOptions: FirebaseOptions = FirebaseOptions.builder()
Expand All @@ -193,10 +193,15 @@ object FcmClient {
case s: ServiceAccountCredentials => s.getProjectId()
case _ => ""
}
new FcmClient(FirebaseMessaging.getInstance(firebaseApp), firebaseApp, config, projectId, credential, firebaseOptions.getJsonFactory())
new FcmFirebase(FirebaseMessaging.getInstance(firebaseApp), firebaseApp, config, projectId, credential, firebaseOptions.getJsonFactory())
}
}

object FcmClient {
def apply(firebase: FcmFirebase): FcmClient =
new FcmClient(firebase.firebaseMessaging, firebase.firebaseApp, firebase.config, firebase.projectId, firebase.credential, firebase.jsonFactory)
}

object FirebaseHelpers {

implicit class RichApiFuture[T](val af: ApiFuture[T]) extends AnyVal {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ import java.net.http.HttpRequest
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpResponse
import org.slf4j.{Logger, LoggerFactory}

trait FcmTransport {
def sendAsync(token: String, payload: FcmPayload, dryRun: Boolean): Future[String]
}

class FcmTransportJdkImpl(credential: GoogleCredentials, url: String, jsonFactory: JsonFactory) extends FcmTransport {

implicit val logger: Logger = LoggerFactory.getLogger(this.getClass)

private val httpClient: HttpClient = HttpClient.newHttpClient()

logger.info("HttpClient is instantiated")

private val charSet = StandardCharsets.UTF_8

private val mediaType = "application/json; charset=UTF-8"
Expand Down
Loading