|
| 1 | +package cn.wildfirechat.app.conference; |
| 2 | + |
| 3 | +import cn.wildfirechat.app.jpa.ConferenceEntity; |
| 4 | +import cn.wildfirechat.app.jpa.ConferenceEntityRepository; |
| 5 | +import cn.wildfirechat.common.ErrorCode; |
| 6 | +import cn.wildfirechat.sdk.ConferenceAdmin; |
| 7 | +import cn.wildfirechat.sdk.model.IMResult; |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.scheduling.annotation.Scheduled; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | +import org.springframework.transaction.annotation.Transactional; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +@Service |
| 18 | +public class ConferenceCleanupService { |
| 19 | + private static final Logger LOG = LoggerFactory.getLogger(ConferenceCleanupService.class); |
| 20 | + |
| 21 | + @Autowired |
| 22 | + private ConferenceEntityRepository conferenceEntityRepository; |
| 23 | + |
| 24 | + @Autowired |
| 25 | + private ConferenceServiceImpl conferenceServiceImpl; |
| 26 | + |
| 27 | + /** |
| 28 | + * 每5分钟检查一次过期会议,并调用SDK销毁 |
| 29 | + */ |
| 30 | + @Scheduled(fixedRate = 5 * 60 * 1000) |
| 31 | + @Transactional |
| 32 | + public void cleanupExpiredConferences() { |
| 33 | + long currentTime = System.currentTimeMillis() / 1000; // 转换为秒 |
| 34 | + LOG.info("开始检查过期会议,当前时间: {} 秒", currentTime); |
| 35 | + |
| 36 | + List<ConferenceEntity> expiredConferences = conferenceEntityRepository.findExpiredConferences(currentTime); |
| 37 | + LOG.info("发现 {} 个过期会议", expiredConferences.size()); |
| 38 | + |
| 39 | + for (ConferenceEntity conference : expiredConferences) { |
| 40 | + try { |
| 41 | + LOG.info("正在销毁过期会议: {}, endTime: {}", conference.id, conference.endTime); |
| 42 | + |
| 43 | + // 调用SDK销毁会议 |
| 44 | + IMResult<Void> result = ConferenceAdmin.destroy(conference.id, conference.advance); |
| 45 | + if (result != null && result.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { |
| 46 | + LOG.info("成功销毁会议: {}", conference.id); |
| 47 | + } else { |
| 48 | + LOG.warn("销毁会议 {} 返回错误: {}", conference.id, |
| 49 | + result != null ? result.getErrorCode().getMsg() : "null result"); |
| 50 | + } |
| 51 | + |
| 52 | + Thread.sleep(100); |
| 53 | + |
| 54 | + // 记录会议结束并更新使用量(使用计划的endTime作为实际结束时间) |
| 55 | + conferenceServiceImpl.endConferenceAndUpdateUsage(conference.id, conference.endTime); |
| 56 | + |
| 57 | + // 从数据库删除会议记录 |
| 58 | + conferenceEntityRepository.delete(conference); |
| 59 | + LOG.info("已从数据库删除会议记录: {}", conference.id); |
| 60 | + |
| 61 | + } catch (Exception e) { |
| 62 | + LOG.error("销毁会议 {} 时发生异常", conference.id, e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + LOG.info("过期会议清理完成,共处理 {} 个会议", expiredConferences.size()); |
| 67 | + } |
| 68 | +} |
0 commit comments