新增清除空闲房间定时任务
This commit is contained in:
parent
5f01a36384
commit
94fc0606c2
@ -75,9 +75,6 @@ public class RoomVoiceDiscoverQryExe {
|
||||
|
||||
List<ActiveVoiceRoom> activeVoiceRooms = activeVoiceRoomService.listDiscover(cmd.requireReqSysOrigin(), cmd.isAllRegion(), region, 50);
|
||||
|
||||
//清除DB中没人的房间
|
||||
formatRoomList(activeVoiceRooms);
|
||||
|
||||
// 查询没人的房间缓存,加入进去
|
||||
List<ActiveVoiceRoom> emptyRooms = getEmptyRoomsFromCache();
|
||||
if (CollectionUtils.isNotEmpty(emptyRooms)) {
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
// 路径: rc-service-other/other-application/src/main/java/com/red/circle/other/app/scheduler/EmptyRoomCleanTask.java
|
||||
package com.red.circle.other.app.scheduler;
|
||||
|
||||
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
|
||||
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
|
||||
import com.red.circle.live.inner.endpoint.LiveMicClient;
|
||||
import com.red.circle.other.infra.database.mongo.entity.live.ActiveVoiceRoom;
|
||||
import com.red.circle.other.infra.database.mongo.service.live.ActiveVoiceRoomService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 清理空房间定时任务
|
||||
* 每2分钟清理一次DB中没人的房间
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class EmptyRoomCleanTask {
|
||||
|
||||
private final ActiveVoiceRoomService activeVoiceRoomService;
|
||||
private final LiveMicClient liveMicClient;
|
||||
|
||||
/**
|
||||
* 每2分钟清理一次空房间
|
||||
*/
|
||||
@Scheduled(cron = "0 */2 * * * ?")
|
||||
public void cleanEmptyRooms() {
|
||||
try {
|
||||
// 获取所有活跃房间
|
||||
List<ActiveVoiceRoom> allRooms = activeVoiceRoomService.listDiscover(SysOriginPlatformEnum.LIKEI.name(),true,"",200);
|
||||
if (CollectionUtils.isEmpty(allRooms)) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<Long> emptyRoomIds = new ArrayList<>();
|
||||
|
||||
// 检查每个房间的在线人数
|
||||
for (ActiveVoiceRoom room : allRooms) {
|
||||
// 跳过固定权重的房间(这些是固定展示的房间)
|
||||
if (room.getFixedWeights() != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
Long roomUserCount = liveMicClient.getLiveRoomUserSize(room.getId()).getBody();
|
||||
if (roomUserCount != null && roomUserCount == 0) {
|
||||
emptyRoomIds.add(room.getId());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("获取房间[{}]在线人数失败", room.getId(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量删除空房间
|
||||
if (!CollectionUtils.isEmpty(emptyRoomIds)) {
|
||||
activeVoiceRoomService.removeByIds(emptyRoomIds);
|
||||
log.info("清理空房间完成,删除房间数量: {}, 房间ID: {}", emptyRoomIds.size(), emptyRoomIds);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("清理空房间任务执行失败", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user