Skip to content

Commit

Permalink
Fix daily shop refresh notification because it was complely wrong
Browse files Browse the repository at this point in the history
  • Loading branch information
MrPowerGamerBR committed Nov 12, 2024
1 parent 03c7f60 commit 19520be
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package net.perfectdreams.loritta.morenitta.utils

import io.ktor.client.request.*
import io.ktor.http.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import mu.KotlinLogging
import net.perfectdreams.loritta.cinnamon.pudding.tables.*
import net.perfectdreams.loritta.morenitta.LorittaBot
import net.perfectdreams.loritta.serializable.internal.requests.LorittaInternalRPCRequest
import net.perfectdreams.loritta.serializable.internal.responses.LorittaInternalRPCResponse
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.*

Expand Down Expand Up @@ -38,18 +39,25 @@ class LorittaDailyShopUpdateTask(val loritta: LorittaBot) : Runnable {
// Notify that it was refreshed
val shards = loritta.config.loritta.clusters.instances

shards.map {
GlobalScope.launch {
try {
logger.info { "Sending daily shop refresh request to other clusters..." }
loritta.httpWithoutTimeout.post("${it.getUrl(loritta)}/daily-shop-refreshed?dailyShopId=${resultId}") {
userAgent(loritta.lorittaCluster.getUserAgent(loritta))
}
} catch (e: Exception) {
logger.warn(e) { "Shard ${it.name} ${it.id} offline!" }
val jobs = shards.map { cluster ->
cluster to GlobalScope.async {
withTimeout(25_000) {
loritta.makeRPCRequest<LorittaInternalRPCResponse.DailyShopRefreshedResponse>(
cluster,
LorittaInternalRPCRequest.DailyShopRefreshedRequest(resultId.value)
)
}
}
}

for (job in jobs) {
try {
job.second.await()
logger.info { "Successfully notified Cluster ${job.first.id} (${job.first.name}) that the daily shop was refreshed!" }
} catch (e: Exception) {
logger.warn(e) { "Something went wrong when notifying Cluster ${job.first.id} (${job.first.name}) that the daily shop was refreshed..." }
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import net.perfectdreams.loritta.morenitta.utils.escapeMentions
import net.perfectdreams.loritta.morenitta.utils.extensions.await
import net.perfectdreams.loritta.morenitta.utils.extensions.getGuildMessageChannelById
import net.perfectdreams.loritta.morenitta.website.utils.extensions.respondJson
import net.perfectdreams.loritta.morenitta.websiteinternal.loriinternalapi.DailyShopRefreshedRoute
import net.perfectdreams.loritta.morenitta.websiteinternal.loriinternalapi.GuildJsonBenchmarkRoute
import net.perfectdreams.loritta.morenitta.websiteinternal.loripublicapi.WebsitePublicAPIException
import net.perfectdreams.loritta.morenitta.websiteinternal.loripublicapi.v1.guilds.*
Expand Down Expand Up @@ -75,8 +74,7 @@ class InternalWebServer(val m: LorittaBot) {
PostMusicalChairsRoute(m)
)
private val internalAPIRoutes = listOf(
GuildJsonBenchmarkRoute(m),
DailyShopRefreshedRoute(m)
GuildJsonBenchmarkRoute(m)
)

@OptIn(ExperimentalCoroutinesApi::class)
Expand Down Expand Up @@ -300,6 +298,9 @@ class InternalWebServer(val m: LorittaBot) {
processors.executeDashGuildScopedProcessor.process(call, request)
}

is LorittaInternalRPCRequest.DailyShopRefreshedRequest -> {
processors.dailyShopRefreshedProcessor.process(call, request)
}
is LorittaInternalRPCRequest.UpdateTwitchSubscriptionsRequest -> {
if (m.isMainInstance) {
GlobalScope.launch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package net.perfectdreams.loritta.morenitta.websiteinternal.rpc.processors

import net.perfectdreams.loritta.morenitta.websiteinternal.InternalWebServer
import net.perfectdreams.loritta.morenitta.websiteinternal.rpc.processors.loritta.DailyShopRefreshedProcessor
import net.perfectdreams.loritta.morenitta.websiteinternal.rpc.processors.loritta.GetLorittaInfoProcessor

class Processors(val internalWebServer: InternalWebServer) {
val getLorittaInfoProcessor = GetLorittaInfoProcessor(internalWebServer.m)
val executeDashGuildScopedProcessor = ExecuteDashGuildScopedProcessor(internalWebServer, internalWebServer.m)
val dailyShopRefreshedProcessor = DailyShopRefreshedProcessor(internalWebServer.m)
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package net.perfectdreams.loritta.morenitta.websiteinternal.loriinternalapi
package net.perfectdreams.loritta.morenitta.websiteinternal.rpc.processors.loritta

import dev.minn.jda.ktx.messages.MessageCreate
import io.ktor.server.application.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.serialization.json.buildJsonObject
import mu.KotlinLogging
import net.dv8tion.jda.api.entities.Message
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel
Expand Down Expand Up @@ -34,22 +33,27 @@ import net.perfectdreams.loritta.morenitta.utils.extensions.getGuildMessageChann
import net.perfectdreams.loritta.morenitta.utils.extensions.getIconUrl
import net.perfectdreams.loritta.morenitta.utils.extensions.toJDA
import net.perfectdreams.loritta.morenitta.website.utils.WebsiteUtils
import net.perfectdreams.loritta.morenitta.website.utils.extensions.respondJson
import net.perfectdreams.loritta.morenitta.websiteinternal.rpc.processors.LorittaInternalRpcProcessor
import net.perfectdreams.loritta.serializable.*
import net.perfectdreams.sequins.ktor.BaseRoute
import net.perfectdreams.loritta.serializable.internal.requests.LorittaInternalRPCRequest
import net.perfectdreams.loritta.serializable.internal.responses.LorittaInternalRPCResponse
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.innerJoin
import org.jetbrains.exposed.sql.or
import org.jetbrains.exposed.sql.selectAll
import java.time.Instant

class DailyShopRefreshedRoute(val loritta: LorittaBot) : BaseRoute("/daily-shop-refreshed") {
class DailyShopRefreshedProcessor(val loritta: LorittaBot) : LorittaInternalRpcProcessor<LorittaInternalRPCRequest.DailyShopRefreshedRequest, LorittaInternalRPCResponse.DailyShopRefreshedResponse> {
companion object {
private val logger = KotlinLogging.logger {}
}

override suspend fun onRequest(call: ApplicationCall) {
val dailyShopId = call.parameters["dailyShopId"]?.toLong()
override suspend fun process(
call: ApplicationCall,
request: LorittaInternalRPCRequest.DailyShopRefreshedRequest
): LorittaInternalRPCResponse.DailyShopRefreshedResponse {
val dailyShopId = request.dailyShopId
logger.info { "Received information that the daily shop was refreshed! dailyShopId: $dailyShopId" }

val result = loritta.transaction {
val backgrounds = DailyShopItems
Expand Down Expand Up @@ -366,10 +370,10 @@ class DailyShopRefreshedRoute(val loritta: LorittaBot) : BaseRoute("/daily-shop-
}
}

// We don't await to avoid timing out, keep it in separate tasks
call.respondJson(buildJsonObject {})
return LorittaInternalRPCResponse.DailyShopRefreshedResponse
}


private data class Result(
val backgrounds: List<DailyShopBackgroundEntry>,
val profileDesigns: List<ProfileDesign>,
Expand Down

0 comments on commit 19520be

Please sign in to comment.