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
Add registration cleaning worker #239
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.gu.notifications.worker | ||
|
||
import cats.effect.{ContextShift, IO} | ||
import cats.effect.IO._ | ||
import com.amazonaws.services.lambda.runtime.{Context, RequestHandler} | ||
import com.amazonaws.services.lambda.runtime.events.SQSEvent | ||
import com.gu.notifications.worker.models.InvalidTokens | ||
import db.{DatabaseConfig, RegistrationService} | ||
import doobie.util.transactor.Transactor | ||
import org.slf4j.{Logger, LoggerFactory} | ||
import play.api.libs.json.Json | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.concurrent.{ExecutionContext, ExecutionContextExecutor} | ||
import scala.util.control.NonFatal | ||
|
||
class RegistrationCleaningWorker extends RequestHandler[SQSEvent, Unit] { | ||
|
||
implicit val logger: Logger = LoggerFactory.getLogger(this.getClass) | ||
implicit val ec: ExecutionContextExecutor = ExecutionContext.global | ||
implicit val ioContextShift: ContextShift[IO] = IO.contextShift(ec) | ||
val config: CleanerConfiguration = Configuration.fetchCleaner() | ||
val transactor: Transactor[IO] = DatabaseConfig.transactor[IO](config.jdbcConfig) | ||
val registrationService = RegistrationService(transactor) | ||
|
||
case class CleaningResult( | ||
deletedRegistrations: Int, | ||
deletedRows: Int, | ||
failures: Int) { | ||
def combined(cleaningResult: CleaningResult): CleaningResult = this.copy( | ||
deletedRegistrations = cleaningResult.deletedRegistrations + deletedRegistrations, | ||
deletedRows = cleaningResult.deletedRows + deletedRows, | ||
failures = cleaningResult.failures + failures | ||
) | ||
} | ||
|
||
override def handleRequest(input: SQSEvent, context: Context): Unit = { | ||
|
||
def deleteAndSwallowError(token: String): IO[CleaningResult] = { | ||
registrationService.removeAllByToken(token) | ||
.map(deletedRows => CleaningResult(1, deletedRows, 0)) | ||
.handleErrorWith { | ||
case NonFatal(e) => | ||
logger.error(s"Unable to delete token $token", e) | ||
IO.pure(CleaningResult(0, 0, 1)) | ||
} | ||
} | ||
|
||
def traverseAndDelete(remainingTokens: List[String], total: CleaningResult): IO[CleaningResult] = remainingTokens match { | ||
case token :: moreTokens => deleteAndSwallowError(token).flatMap { deletionResult => | ||
traverseAndDelete(moreTokens, total.combined(deletionResult)) | ||
} | ||
case Nil => IO.pure(total) | ||
} | ||
|
||
val tokens = input | ||
.getRecords.asScala | ||
.map(_.getBody) | ||
.map(Json.parse) | ||
.flatMap(_.validate[InvalidTokens].asOpt) | ||
.foldLeft(List.empty[String]){ case (agg, value) => agg ++ value.tokens } | ||
|
||
val processReport = traverseAndDelete(tokens, CleaningResult(0, 0, 0)).unsafeRunSync() | ||
|
||
logger.info(s"Deleted ${processReport.deletedRows} rows, deleted ${processReport.deletedRegistrations} registration and failed ${processReport.failures} time(s)") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will keep using streams to handle the logic. It make it quite easy to follow the logic flow:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it, Streams still look like alien technology to me but I'll get used to it. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.gu.notifications.worker.models | ||
|
||
import play.api.libs.json.{Format, Json} | ||
|
||
case class InvalidTokens( | ||
tokens: List[String] | ||
) | ||
|
||
object InvalidTokens { | ||
implicit val invalidTokensJF: Format[InvalidTokens] = Json.format[InvalidTokens] | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. APNS provides a token invalidation timestamp that could be use to avoid discarding a token that would have been refreshed since last time it was fetched. I am not sure we need to handle this edge case though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I'm hesitating to implement that, I'm worried it might introduce more edge cases |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not
.flatMap(_.tokens)
rather thanfoldLeft
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that you mention it, yeah why? 😄