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

background jobs inprovements #57

Merged
merged 1 commit into from
Jun 7, 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 @@ -36,6 +36,7 @@ import org.flywaydb.core.Flyway
import org.jobrunr.configuration.JobRunr
import org.jobrunr.configuration.JobRunrConfiguration
import org.jobrunr.dashboard.JobRunrDashboardWebServerConfiguration
import org.jobrunr.jobs.filters.RetryFilter
import org.jobrunr.server.BackgroundJobServerConfiguration
import org.jobrunr.server.JobActivator
import org.jobrunr.storage.StorageProvider
Expand Down Expand Up @@ -119,6 +120,7 @@ fun configureDI(vertx: Vertx, conf: Conf) = DI {
bindSingleton {
JobRunr.configure()
.useStorageProvider(instance())
.withJobFilter(RetryFilter(2))
.useDashboardIf(
conf.backgroundTask.dashboardEnabled,
JobRunrDashboardWebServerConfiguration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package me.sujanpoudel.playdeals.common

import me.sujanpoudel.playdeals.logger
import me.sujanpoudel.playdeals.jobs.info
import org.jobrunr.jobs.lambdas.JobRequestHandler
import kotlin.time.DurationUnit
import kotlin.time.measureTimedValue

inline fun <T> loggingExecutionTime(message: String, action: () -> T): T {
val timedValue =
measureTimedValue {
action.invoke()
}
logger.info("$message (took ${timedValue.duration.toString(DurationUnit.MILLISECONDS)})")
inline fun <T> JobRequestHandler<*>.loggingExecutionTime(message: String, action: () -> T): T {
val timedValue = measureTimedValue { action.invoke() }
info("$message took ${timedValue.duration.toString(DurationUnit.MILLISECONDS)}ms")
return timedValue.value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package me.sujanpoudel.playdeals.jobs

import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.databind.ObjectMapper
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.getOrElse
import com.github.michaelbull.result.runCatching
import io.vertx.core.json.Json
import io.vertx.core.json.JsonArray
import io.vertx.core.json.JsonObject
import io.vertx.ext.web.client.WebClient
Expand Down Expand Up @@ -56,10 +60,11 @@ class AppDetailScrapper(
) {
val packageName = jobRequest.packageName

val app =
loggingExecutionTime("$SIMPLE_NAME:: scrapping app details $packageName") {
getAppDetail(packageName)
}
val app = loggingExecutionTime("$SIMPLE_NAME:: scrapping app details $packageName") {
getAppDetail(packageName)
}.getOrElse {
throw RuntimeException("AppDetailScrapper failed to scrap details ${it.message}")
}

when {
app.normalPrice == 0f -> {
Expand All @@ -68,43 +73,40 @@ class AppDetailScrapper(
}

app.normalPrice == app.currentPrice -> {
logger.infoNotify("App $packageName(${app.name}) deals has been expired")
infoNotify("App $packageName(${app.name}) deals has been expired")
repository.delete(packageName)
}

(app.currentPrice ?: 0f) < app.normalPrice -> {
logger.info("Found deal for $packageName(${app.name}) ${app.currentPrice} ${app.currency}(${app.normalPrice} ${app.currency})")
info("Found deal for $packageName(${app.name}) ${app.currentPrice} ${app.currency}(${app.normalPrice} ${app.currency})")
repository.upsert(app.asNewDeal()).also {
messagingService.sendMessageForNewDeal(it)
}
}
}
}

private suspend fun getAppDetail(packageName: String): AndroidAppDetail {
val response =
webClient.get("/store/apps/details?id=$packageName&hl=en&gl=us")
.send()
.coAwait()
private suspend fun getAppDetail(packageName: String): Result<AndroidAppDetail, Throwable> = runCatching {
val response = webClient.get("/store/apps/details?id=$packageName&hl=en&gl=us")
.send()
.coAwait()

val body = response.bodyAsString()

val mapper =
ObjectMapper().apply {
configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
}
val mapper = ObjectMapper().apply {
configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true)
configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
}

val matches =
INIT_DATA_PATTERN.matcher(body).let {
val snippets = mutableListOf<String>()
while (it.find()) {
snippets.add(it.group(1))
}
snippets
}.map {
io.vertx.core.json.Json.decodeValue(mapper.readTree(it).toPrettyString()) as JsonObject
val matches = INIT_DATA_PATTERN.matcher(body).let {
val snippets = mutableListOf<String>()
while (it.find()) {
snippets.add(it.group(1))
}
snippets
}.map {
Json.decodeValue(mapper.readTree(it).toPrettyString()) as JsonObject
}

val combined = jsonObjectOf()

Expand All @@ -115,7 +117,7 @@ class AppDetailScrapper(
val currentPrice = combined.getValue<Int>(Value.CURRENT_PRICE) / PRICE_MULTIPLIER
val normalPrice = combined.getValueOrNull<Int>(Value.NORMAL_PRICE)?.div(PRICE_MULTIPLIER) ?: currentPrice

return AndroidAppDetail(
AndroidAppDetail(
id = packageName,
name = combined.getValue(Value.TITLE),
icon = combined.getValue(Value.ICON),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package me.sujanpoudel.playdeals.jobs

import me.sujanpoudel.playdeals.common.SIMPLE_NAME
import me.sujanpoudel.playdeals.common.loggingExecutionTime
import me.sujanpoudel.playdeals.logger
import me.sujanpoudel.playdeals.repositories.DealRepository
import org.jobrunr.jobs.lambdas.JobRequest
import org.jobrunr.jobs.states.StateName
Expand Down Expand Up @@ -30,7 +29,7 @@ class AndroidAppExpiryCheckScheduler(

val lastUpdatedTime = Instant.now().minus(1, ChronoUnit.HOURS)
val jobs = storageProvider.deleteJobsPermanently(StateName.FAILED, lastUpdatedTime)
logger.info("deleted FAILED `$jobs`")
info("deleted FAILED `$jobs`")
}

class Request private constructor() : JobRequest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package me.sujanpoudel.playdeals.jobs

import kotlinx.coroutines.runBlocking
import me.sujanpoudel.playdeals.infoNotify
import me.sujanpoudel.playdeals.logger
import org.jobrunr.jobs.lambdas.JobRequest
import org.jobrunr.jobs.lambdas.JobRequestHandler

Expand All @@ -11,3 +13,13 @@ abstract class CoJobRequestHandler<T : JobRequest> : JobRequestHandler<T> {

abstract suspend fun handleRequest(jobRequest: T)
}

fun JobRequestHandler<*>.info(message: String) {
jobContext().logger().info(message)
logger.info(message)
}

fun JobRequestHandler<*>.infoNotify(message: String) {
jobContext().logger().info(message)
logger.infoNotify(message)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import me.sujanpoudel.playdeals.common.SIMPLE_NAME
import me.sujanpoudel.playdeals.common.loggingExecutionTime
import me.sujanpoudel.playdeals.domain.entities.formattedCurrentPrice
import me.sujanpoudel.playdeals.domain.entities.formattedNormalPrice
import me.sujanpoudel.playdeals.infoNotify
import me.sujanpoudel.playdeals.logger
import me.sujanpoudel.playdeals.repositories.DealRepository
import me.sujanpoudel.playdeals.repositories.KeyValuesRepository
import me.sujanpoudel.playdeals.services.MessagingService
Expand All @@ -29,33 +27,30 @@ class DealSummarizer(
override suspend fun handleRequest(jobRequest: Request): Unit = loggingExecutionTime(
"$SIMPLE_NAME:: handleRequest",
) {
val lastTimestamp =
keyValueRepository.get(LAST_SUMMARY_TIMESTAMP)?.let(OffsetDateTime::parse)
?: OffsetDateTime.now()
val lastTimestamp = keyValueRepository.get(LAST_SUMMARY_TIMESTAMP)?.let(OffsetDateTime::parse)
?: OffsetDateTime.now()

val deals = dealRepository.getNewDeals(lastTimestamp)

if (deals.isNotEmpty()) {
val maxCount = 6
val dealsDescription =
deals
.take(maxCount)
.mapIndexed { index, deal ->
"${index + 1}. ${deal.name} was ${deal.formattedNormalPrice()} is now ${deal.formattedCurrentPrice()}"
}.joinToString("\n")
val dealsDescription = deals
.take(maxCount)
.mapIndexed { index, deal ->
"${index + 1}. ${deal.name} was ${deal.formattedNormalPrice()} is now ${deal.formattedCurrentPrice()}"
}.joinToString("\n")

messagingService.sendMessageToTopic(
topic = Constants.PushNotificationTopic.DEALS_SUMMARY,
title = "New ${deals.size} app deals are found since yesterday",
body =
if (deals.size > maxCount) {
"$dealsDescription\n\n +${deals.size - maxCount} more..."
} else {
dealsDescription
},
body = if (deals.size > maxCount) {
"$dealsDescription\n\n +${deals.size - maxCount} more..."
} else {
dealsDescription
},
)
} else {
logger.infoNotify("$SIMPLE_NAME:: haven't got any deals since $lastTimestamp")
infoNotify("$SIMPLE_NAME:: haven't got any deals since $lastTimestamp")
}

keyValueRepository.set(LAST_SUMMARY_TIMESTAMP, OffsetDateTime.now().toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,31 +62,28 @@ class ForexFetcher(
private suspend fun getForexRates(): ForexRate {
val currencies = loadCurrencies()

val response =
webClient.get("/v1/latest?access_key=${conf.forexApiKey}&format=1&base=EUR")
.send()
.coAwait()
.bodyAsString()
.let {
Json.decodeValue(it) as io.vertx.core.json.JsonObject
}
val response = webClient.get("/v1/latest?access_key=${conf.forexApiKey}&format=1&base=EUR")
.send()
.coAwait()
.bodyAsString()
.let {
Json.decodeValue(it) as io.vertx.core.json.JsonObject
}

val epochSeconds = response.getLong("timestamp")
val usdRate = response.getJsonObject("rates").getNumber("USD").toFloat()

return ForexRate(
timestamp = OffsetDateTime.ofInstant(java.time.Instant.ofEpochSecond(epochSeconds), ZoneOffset.UTC),
rates =
response.getJsonObject("rates").map {
val currency = currencies[it.key]

ConversionRate(
currency = it.key,
symbol = currency?.symbol ?: "$",
name = currency?.name ?: it.key,
rate = (it.value as Number).toFloat() / usdRate,
)
},
rates = response.getJsonObject("rates").map {
val currency = currencies[it.key]
ConversionRate(
currency = it.key,
symbol = currency?.symbol ?: "$",
name = currency?.name ?: it.key,
rate = (it.value as Number).toFloat() / usdRate,
)
},
)
}

Expand Down
Loading