Commit 5106cf1f43cb2a15dca3e700d4db1c50215f0d3c

Authored by 王明元
1 parent 2c44b108

2023年11月24日17:09:55 优化

src/main/java/cn/fw/freya/service/CrawlBizService.java
... ... @@ -16,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
16 16 import org.springframework.context.event.EventListener;
17 17 import org.springframework.scheduling.annotation.Async;
18 18 import org.springframework.stereotype.Service;
  19 +import org.springframework.util.StringUtils;
19 20  
20 21 import java.io.IOException;
21 22 import java.time.LocalDate;
... ... @@ -305,12 +306,33 @@ public class CrawlBizService {
305 306 String accountNo = account.getAccountNo();
306 307 AccountTypeEnum typeEnum = AccountTypeEnum.getEnumByValue(type);
307 308 CrawlStrategy crawlStrategy = crawlStrategyMap.get(typeEnum);
308   - LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10));// 线程挂起10秒, 防止并发上报, 旧数据覆盖新数据
309   - List<LivePool> yesterdayLiveMsg = crawlStrategy.getLiveMsg(accountNo);// 获取昨日直播信息
  309 + List<LivePool> yesterdayLiveMsg;
  310 + int tryGetDataCnt = 0;
  311 + do {
  312 + LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(5));// 线程挂起5秒, 防止并发上报, 旧数据覆盖新数据
  313 + yesterdayLiveMsg = crawlStrategy.getLiveMsg(accountNo);// 获取昨日直播信息
  314 + tryGetDataCnt++;
  315 + } while (tryGetDataCnt < 12 && !this.checkHasPlayback(yesterdayLiveMsg));
  316 + if (!this.checkHasPlayback(yesterdayLiveMsg)) {
  317 + log.info("{} 直播无回放记录, 放弃重新上报直播数据", accountNo);
  318 + return;
  319 + }
310 320 boolean reportLive = this.doReportLive(account, yesterdayLiveMsg);
311 321 if (!reportLive) {
312 322 accountService.setAccountUndone(accountNo);
313   - log.error(LocalDate.now() + " 上报账户为" + accountNo + "的" + AccountTypeEnum.getNameByValue(account.getType()) + "的直播数据失败");
  323 + log.error(LocalDate.now() + " 上报账户为" + accountNo + "的" +
  324 + AccountTypeEnum.getNameByValue(account.getType()) + "的直播数据失败");
314 325 }
315 326 }
  327 +
  328 + /**
  329 + * 检查是直播是否满足任意一场拥有回播
  330 + *
  331 + * @param lives 直播列表
  332 + * @return 是否满足任意一场拥有回播
  333 + */
  334 + private boolean checkHasPlayback(List<LivePool> lives) {
  335 + return lives.stream()
  336 + .anyMatch(item -> StringUtils.hasText(item.getPlaybackUrl()));
  337 + }
316 338 }
... ...