Skip to content

Commit

Permalink
Add better error catching for heartbeat cron job
Browse files Browse the repository at this point in the history
Seems that the exceptions are escaping, I think because there's some context shenanigns going on that I need to fix, but this should keep it functioning in the meantime
  • Loading branch information
NovaFox161 committed Jul 2, 2024
1 parent 2f6c1c7 commit d4e98ff
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import org.dreamexposure.discal.core.logger.LOGGER
import org.dreamexposure.discal.core.`object`.network.discal.InstanceData
import org.dreamexposure.discal.core.`object`.rest.HeartbeatRequest
import org.dreamexposure.discal.core.`object`.rest.HeartbeatType
import org.dreamexposure.discal.core.utils.GlobalVal
import org.dreamexposure.discal.core.utils.GlobalVal.DEFAULT
import org.dreamexposure.discal.core.utils.GlobalVal.JSON
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
Expand All @@ -31,26 +31,30 @@ class HeartbeatCronJob(
override fun run(args: ApplicationArguments?) {
Flux.interval(Config.HEARTBEAT_INTERVAL.getLong().asSeconds())
.flatMap { heartbeat() }
.doOnError { LOGGER.error(GlobalVal.DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.doOnError { LOGGER.error(DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.onErrorResume { Mono.empty() }
.subscribe()
}

private fun heartbeat() = mono {
val requestBody = HeartbeatRequest(HeartbeatType.CAM, instanceData = InstanceData())
try {
val requestBody = HeartbeatRequest(HeartbeatType.CAM, instanceData = InstanceData())

val request = Request.Builder()
.url("$apiUrl/v3/status/heartbeat")
.post(objectMapper.writeValueAsString(requestBody).toRequestBody(JSON))
.header("Authorization", "Int ${Config.SECRET_DISCAL_API_KEY.getString()}")
.header("Content-Type", "application/json")
.build()
val request = Request.Builder()
.url("$apiUrl/v3/status/heartbeat")
.post(objectMapper.writeValueAsString(requestBody).toRequestBody(JSON))
.header("Authorization", "Int ${Config.SECRET_DISCAL_API_KEY.getString()}")
.header("Content-Type", "application/json")
.build()

Mono.fromCallable(httpClient.newCall(request)::execute)
.map(Response::close)
.subscribeOn(Schedulers.boundedElastic())
.doOnError { LOGGER.error(GlobalVal.DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.onErrorResume { Mono.empty() }
.subscribe()
Mono.fromCallable(httpClient.newCall(request)::execute)
.map(Response::close)
.subscribeOn(Schedulers.boundedElastic())
.doOnError { LOGGER.error(DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.onErrorResume { Mono.empty() }
.subscribe()
} catch (ex: Exception) {
LOGGER.error(DEFAULT, "[Heartbeat] Failed to heartbeat", ex)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.dreamexposure.discal.core.`object`.network.discal.BotInstanceData
import org.dreamexposure.discal.core.`object`.rest.HeartbeatRequest
import org.dreamexposure.discal.core.`object`.rest.HeartbeatType
import org.dreamexposure.discal.core.utils.GlobalVal
import org.dreamexposure.discal.core.utils.GlobalVal.DEFAULT
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.stereotype.Component
Expand All @@ -33,27 +34,31 @@ class HeartbeatCronJob(
override fun run(args: ApplicationArguments?) {
Flux.interval(Config.HEARTBEAT_INTERVAL.getLong().asSeconds())
.flatMap { heartbeat() }
.doOnError { LOGGER.error(GlobalVal.DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.doOnError { LOGGER.error(DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.onErrorResume { Mono.empty() }
.subscribe()
}

private fun heartbeat() = mono {
val data = BotInstanceData.load(discordClient).awaitSingle()
try {
val data = BotInstanceData.load(discordClient).awaitSingle()

val requestBody = HeartbeatRequest(HeartbeatType.BOT, botInstanceData = data)
val request = Request.Builder()
.url("$apiUrl/v3/status/heartbeat")
.post(objectMapper.writeValueAsString(requestBody).toRequestBody(GlobalVal.JSON))
.header("Authorization", "Int ${Config.SECRET_DISCAL_API_KEY.getString()}")
.header("Content-Type", "application/json")
.build()
val requestBody = HeartbeatRequest(HeartbeatType.BOT, botInstanceData = data)
val request = Request.Builder()
.url("$apiUrl/v3/status/heartbeat")
.post(objectMapper.writeValueAsString(requestBody).toRequestBody(GlobalVal.JSON))
.header("Authorization", "Int ${Config.SECRET_DISCAL_API_KEY.getString()}")
.header("Content-Type", "application/json")
.build()

Mono.fromCallable(httpClient.newCall(request)::execute)
.map(Response::close)
.subscribeOn(Schedulers.boundedElastic())
.doOnError { LOGGER.error(GlobalVal.DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.onErrorResume { Mono.empty() }
.subscribe()
Mono.fromCallable(httpClient.newCall(request)::execute)
.map(Response::close)
.subscribeOn(Schedulers.boundedElastic())
.doOnError { LOGGER.error(DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.onErrorResume { Mono.empty() }
.subscribe()
} catch (ex: Exception) {
LOGGER.error(DEFAULT, "[Heartbeat] Failed to heartbeat", ex)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import org.dreamexposure.discal.core.`object`.network.discal.InstanceData
import org.dreamexposure.discal.core.`object`.rest.HeartbeatRequest
import org.dreamexposure.discal.core.`object`.rest.HeartbeatType
import org.dreamexposure.discal.core.utils.GlobalVal
import org.dreamexposure.discal.core.utils.GlobalVal.DEFAULT
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.stereotype.Component
Expand All @@ -30,26 +31,30 @@ class HeartbeatCronJob(
override fun run(args: ApplicationArguments?) {
Flux.interval(Config.HEARTBEAT_INTERVAL.getLong().asSeconds())
.flatMap { heartbeat() }
.doOnError { LOGGER.error(GlobalVal.DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.doOnError { LOGGER.error(DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.onErrorResume { Mono.empty() }
.subscribe()
}

private fun heartbeat() = mono {
val requestBody = HeartbeatRequest(HeartbeatType.WEBSITE, instanceData = InstanceData())
try {
val requestBody = HeartbeatRequest(HeartbeatType.WEBSITE, instanceData = InstanceData())

val request = Request.Builder()
.url("$apiUrl/v3/status/heartbeat")
.post(objectMapper.writeValueAsString(requestBody).toRequestBody(GlobalVal.JSON))
.header("Authorization", "Int ${Config.SECRET_DISCAL_API_KEY.getString()}")
.header("Content-Type", "application/json")
.build()
val request = Request.Builder()
.url("$apiUrl/v3/status/heartbeat")
.post(objectMapper.writeValueAsString(requestBody).toRequestBody(GlobalVal.JSON))
.header("Authorization", "Int ${Config.SECRET_DISCAL_API_KEY.getString()}")
.header("Content-Type", "application/json")
.build()

Mono.fromCallable(httpClient.newCall(request)::execute)
.map(Response::close)
.subscribeOn(Schedulers.boundedElastic())
.doOnError { LOGGER.error(GlobalVal.DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.onErrorResume { Mono.empty() }
.subscribe()
Mono.fromCallable(httpClient.newCall(request)::execute)
.map(Response::close)
.subscribeOn(Schedulers.boundedElastic())
.doOnError { LOGGER.error(DEFAULT, "[Heartbeat] Failed to heartbeat", it) }
.onErrorResume { Mono.empty() }
.subscribe()
} catch (ex: Exception) {
LOGGER.error(DEFAULT, "[Heartbeat] Failed to heartbeat", ex)
}
}
}

0 comments on commit d4e98ff

Please sign in to comment.