forked from datawhale/whale-town-end
feat(chat): 新增聊天业务模块
范围:src/business/chat/ - 实现 ChatService 聊天业务服务(登录/登出/消息发送/位置更新) - 实现 ChatSessionService 会话管理服务(会话创建/销毁/上下文注入) - 实现 ChatFilterService 消息过滤服务(频率限制/敏感词/权限验证) - 实现 ChatCleanupService 会话清理服务(定时清理过期会话) - 添加完整的单元测试覆盖 - 添加模块 README 文档
This commit is contained in:
113
src/business/chat/services/chat_cleanup.service.ts
Normal file
113
src/business/chat/services/chat_cleanup.service.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
/**
|
||||
* 聊天会话清理服务
|
||||
*
|
||||
* 功能描述:
|
||||
* - 定时清理过期会话
|
||||
* - 释放相关资源
|
||||
* - 管理Zulip队列清理
|
||||
*
|
||||
* 架构层级:Business Layer(业务层)
|
||||
*
|
||||
* 最近修改:
|
||||
* - 2026-01-14: 代码规范优化 - 移除未使用的依赖 (修改者: moyin)
|
||||
* - 2026-01-14: 代码规范优化 - 补充类级别JSDoc注释 (修改者: moyin)
|
||||
* - 2026-01-14: 代码规范优化 - 完善文件头注释和方法注释规范 (修改者: moyin)
|
||||
*
|
||||
* @author moyin
|
||||
* @version 1.0.3
|
||||
* @since 2026-01-14
|
||||
* @lastModified 2026-01-14
|
||||
*/
|
||||
|
||||
import { Injectable, Logger, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
||||
import { ChatSessionService } from './chat_session.service';
|
||||
|
||||
/**
|
||||
* 聊天会话清理服务类
|
||||
*
|
||||
* 职责:
|
||||
* - 定时检测和清理过期会话
|
||||
* - 释放Zulip队列等相关资源
|
||||
* - 维护系统资源的健康状态
|
||||
*
|
||||
* 主要方法:
|
||||
* - triggerCleanup() - 手动触发会话清理
|
||||
*
|
||||
* 使用场景:
|
||||
* - 系统启动时自动开始定时清理任务
|
||||
* - 管理员手动触发清理操作
|
||||
*/
|
||||
@Injectable()
|
||||
export class ChatCleanupService implements OnModuleInit, OnModuleDestroy {
|
||||
private readonly logger = new Logger(ChatCleanupService.name);
|
||||
private cleanupInterval: NodeJS.Timeout | null = null;
|
||||
private readonly CLEANUP_INTERVAL_MS = 5 * 60 * 1000; // 5分钟
|
||||
private readonly SESSION_TIMEOUT_MINUTES = 30;
|
||||
|
||||
constructor(
|
||||
private readonly sessionService: ChatSessionService,
|
||||
) {}
|
||||
|
||||
async onModuleInit() {
|
||||
this.logger.log('启动会话清理定时任务');
|
||||
this.startCleanupTask();
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
this.logger.log('停止会话清理定时任务');
|
||||
this.stopCleanupTask();
|
||||
}
|
||||
|
||||
private startCleanupTask() {
|
||||
this.cleanupInterval = setInterval(async () => {
|
||||
await this.performCleanup();
|
||||
}, this.CLEANUP_INTERVAL_MS);
|
||||
}
|
||||
|
||||
private stopCleanupTask() {
|
||||
if (this.cleanupInterval) {
|
||||
clearInterval(this.cleanupInterval);
|
||||
this.cleanupInterval = null;
|
||||
}
|
||||
}
|
||||
|
||||
private async performCleanup() {
|
||||
const startTime = Date.now();
|
||||
|
||||
this.logger.log('开始执行会话清理');
|
||||
|
||||
try {
|
||||
const result = await this.sessionService.cleanupExpiredSessions(this.SESSION_TIMEOUT_MINUTES);
|
||||
|
||||
// 清理Zulip队列
|
||||
for (const queueId of result.zulipQueueIds) {
|
||||
try {
|
||||
// 这里可以添加Zulip队列清理逻辑
|
||||
this.logger.debug('清理Zulip队列', { queueId });
|
||||
} catch (error) {
|
||||
this.logger.warn('清理Zulip队列失败', { queueId, error: (error as Error).message });
|
||||
}
|
||||
}
|
||||
|
||||
const duration = Date.now() - startTime;
|
||||
|
||||
this.logger.log('会话清理完成', {
|
||||
cleanedCount: result.cleanedCount,
|
||||
zulipQueueCount: result.zulipQueueIds.length,
|
||||
duration,
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
this.logger.error('会话清理失败', { error: (error as Error).message });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动触发清理
|
||||
* @returns 清理结果,包含清理的会话数量
|
||||
*/
|
||||
async triggerCleanup(): Promise<{ cleanedCount: number }> {
|
||||
const result = await this.sessionService.cleanupExpiredSessions(this.SESSION_TIMEOUT_MINUTES);
|
||||
return { cleanedCount: result.cleanedCount };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user