fix some endpoints.

- In apiDiscordRoutes, guildId and channelId return string.
- In wsSongListRoutes, retry 5 times, some logic changed.
This commit is contained in:
dalbodeule 2024-08-21 19:08:23 +09:00
parent 327862f6a5
commit a733f56d8c
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
2 changed files with 23 additions and 21 deletions

View File

@ -35,8 +35,8 @@ fun Route.apiDiscordRoutes() {
return@get return@get
} }
call.respond(HttpStatusCode.OK, GuildSettings( call.respond(HttpStatusCode.OK, GuildSettings(
user.liveAlertGuild, user.liveAlertGuild.toString(),
user.liveAlertChannel, user.liveAlertChannel.toString(),
user.liveAlertMessage user.liveAlertMessage
)) ))
return@get return@get
@ -54,7 +54,7 @@ fun Route.apiDiscordRoutes() {
call.respond(HttpStatusCode.BadRequest, "User does not exist") call.respond(HttpStatusCode.BadRequest, "User does not exist")
return@post return@post
} }
UserService.updateLiveAlert(user, body.guildId ?: 0L, body.channelId ?: 0L, body.message) UserService.updateLiveAlert(user, body.guildId?.toLong() ?: 0L, body.channelId?.toLong() ?: 0L, body.message)
call.respond(HttpStatusCode.OK) call.respond(HttpStatusCode.OK)
} }
get("/guild/{gid}") { get("/guild/{gid}") {
@ -102,7 +102,7 @@ fun Route.apiDiscordRoutes() {
@Serializable @Serializable
data class GuildSettings( data class GuildSettings(
val guildId: Long?, val guildId: String?,
val channelId: Long?, val channelId: String?,
val message: String? = null, val message: String? = null,
) )

View File

@ -44,14 +44,15 @@ fun Routing.wsSongListRoutes() {
sessions.remove(uid) sessions.remove(uid)
} }
suspend fun sendWithRetry(ws: WebSocketServerSession, res: SongResponse, maxRetries: Int, delayMillis: Long = 3000L) { suspend fun sendWithRetry(uid: String, res: SongResponse, maxRetries: Int = 5, delayMillis: Long = 3000L) {
var attempt = 0 var attempt = 0
var sentSuccessfully = false var sentSuccessfully = false
while (attempt < maxRetries && !sentSuccessfully) { while (attempt < maxRetries && !sentSuccessfully) {
val ws = sessions[uid]
try { try {
// Attempt to send the message // Attempt to send the message
ws.sendSerialized(res) ws?.sendSerialized(res)
sentSuccessfully = true // If no exception, mark as sent successfully sentSuccessfully = true // If no exception, mark as sent successfully
logger.debug("Message sent successfully on attempt $attempt") logger.debug("Message sent successfully on attempt $attempt")
} catch (e: Exception) { } catch (e: Exception) {
@ -203,8 +204,9 @@ fun Routing.wsSongListRoutes() {
CoroutineScope(Dispatchers.Default).launch { CoroutineScope(Dispatchers.Default).launch {
val user = UserService.getUser(it.uid) val user = UserService.getUser(it.uid)
if(user != null) { if(user != null) {
sessions[user.token ?: ""]?.let { ws -> user.token?.let { token ->
sendWithRetry(ws, SongResponse( sendWithRetry(
token, SongResponse(
it.type.value, it.type.value,
it.uid, it.uid,
it.reqUid, it.reqUid,
@ -212,7 +214,7 @@ fun Routing.wsSongListRoutes() {
it.author, it.author,
it.time, it.time,
it.url it.url
), 3) ))
} }
} }
} }
@ -222,8 +224,9 @@ fun Routing.wsSongListRoutes() {
CoroutineScope(Dispatchers.Default).launch { CoroutineScope(Dispatchers.Default).launch {
val user = UserService.getUser(it.uid) val user = UserService.getUser(it.uid)
if(user != null) { if(user != null) {
sessions[user.token ?: ""]?.let { ws -> user.token?.let { token ->
sendWithRetry(ws, SongResponse( sendWithRetry(
token, SongResponse(
it.type.value, it.type.value,
it.uid, it.uid,
null, null,
@ -231,8 +234,7 @@ fun Routing.wsSongListRoutes() {
null, null,
null, null,
null null
), 3) ))
removeSession(user.token ?: "")
} }
} }
} }