From 61a5077d4d24526d8706a3d850ace05bde943456 Mon Sep 17 00:00:00 2001 From: WrBug Date: Sat, 14 Feb 2026 21:29:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(crypto-tail):=20=E5=A4=9A=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E8=AE=A2=E9=98=85=E6=97=B6=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E4=B8=8E=E8=B7=B3=E8=BF=87=E5=8E=9F=E5=9B=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - buildSubscriptionMap 中 fetchEventBySlug 失败时不再静默 continue,按原因打 warn/debug 日志 - 新增 fetchEventBySlugWithRetry,失败最多重试 3 次、间隔 1s,避免瞬时失败导致只订阅到其中一个策略 Co-authored-by: Cursor --- .../CryptoTailOrderbookWsService.kt | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/backend/src/main/kotlin/com/wrbug/polymarketbot/service/cryptotail/CryptoTailOrderbookWsService.kt b/backend/src/main/kotlin/com/wrbug/polymarketbot/service/cryptotail/CryptoTailOrderbookWsService.kt index 8ee7315..0e245ce 100644 --- a/backend/src/main/kotlin/com/wrbug/polymarketbot/service/cryptotail/CryptoTailOrderbookWsService.kt +++ b/backend/src/main/kotlin/com/wrbug/polymarketbot/service/cryptotail/CryptoTailOrderbookWsService.kt @@ -296,12 +296,26 @@ class CryptoTailOrderbookWsService( val interval = strategy.intervalSeconds val periodStartUnix = (nowSeconds / interval) * interval val windowEnd = periodStartUnix + strategy.windowEndSeconds - if (nowSeconds >= windowEnd) continue + if (nowSeconds >= windowEnd) { + logger.debug("尾盘策略跳过(已过时间窗口): strategyId=${strategy.id}, slug=${strategy.marketSlugPrefix}, windowEnd=$windowEnd") + continue + } val slug = "${strategy.marketSlugPrefix}-$periodStartUnix" - val event = fetchEventBySlug(slug).getOrNull() ?: continue - val market = event.markets?.firstOrNull() ?: continue + val event = fetchEventBySlugWithRetry(slug).getOrNull() + if (event == null) { + logger.warn("尾盘策略跳过(拉取事件失败): strategyId=${strategy.id}, slug=$slug,请确认 Gamma 是否存在该 slug 或稍后重试") + continue + } + val market = event.markets?.firstOrNull() + if (market == null) { + logger.warn("尾盘策略跳过(事件无市场): strategyId=${strategy.id}, slug=$slug") + continue + } val tokenIds = parseClobTokenIds(market.clobTokenIds) - if (tokenIds.size < 2) continue + if (tokenIds.size < 2) { + logger.warn("尾盘策略跳过(token 数量不足): strategyId=${strategy.id}, slug=$slug, tokenCount=${tokenIds.size}") + continue + } tokenIdSet.addAll(tokenIds) for (i in tokenIds.indices) { map.getOrPut(tokenIds[i]) { mutableListOf() }.add( @@ -313,6 +327,18 @@ class CryptoTailOrderbookWsService( return Pair(tokenIdSet.toList(), map) } + /** 拉取事件,失败时重试最多 2 次(间隔 1s),避免瞬时失败导致多策略只订阅到其中一个 */ + private fun fetchEventBySlugWithRetry(slug: String, maxAttempts: Int = 3): Result { + var lastFailure: Exception? = null + repeat(maxAttempts) { attempt -> + val result = fetchEventBySlug(slug) + if (result.isSuccess) return result + lastFailure = result.exceptionOrNull() as? Exception + if (attempt < maxAttempts - 1) runBlocking { delay(1000L) } + } + return Result.failure(lastFailure ?: Exception("fetchEventBySlug failed")) + } + private fun fetchEventBySlug(slug: String): Result { return try { val api = retrofitFactory.createGammaApi()