添加 Kafka 消费者、数据库写入、Redis 集成等核心模块,实现设备上下线事件处理 - 创建项目基础目录结构与配置文件 - 实现 Kafka 消费逻辑与手动提交偏移量 - 添加 PostgreSQL 数据库连接与分区表管理 - 集成 Redis 用于错误队列和项目心跳 - 包含数据处理逻辑,区分重启与非重启数据 - 提供数据库初始化脚本与分区创建工具 - 添加单元测试与代码校验脚本
41 lines
1.0 KiB
JavaScript
41 lines
1.0 KiB
JavaScript
export class RedisIntegration {
|
|
constructor(client, projectName, apiBaseUrl) {
|
|
this.client = client;
|
|
this.projectName = projectName;
|
|
this.apiBaseUrl = apiBaseUrl;
|
|
this.heartbeatKey = '项目心跳';
|
|
this.logKey = `${projectName}_项目控制台`;
|
|
}
|
|
|
|
async info(message, context) {
|
|
const payload = {
|
|
timestamp: new Date().toISOString(),
|
|
level: 'info',
|
|
message,
|
|
metadata: context || undefined
|
|
};
|
|
await this.client.rPush(this.logKey, JSON.stringify(payload));
|
|
}
|
|
|
|
async error(message, context) {
|
|
const payload = {
|
|
timestamp: new Date().toISOString(),
|
|
level: 'error',
|
|
message,
|
|
metadata: context || undefined
|
|
};
|
|
await this.client.rPush(this.logKey, JSON.stringify(payload));
|
|
}
|
|
|
|
startHeartbeat() {
|
|
setInterval(() => {
|
|
const payload = {
|
|
projectName: this.projectName,
|
|
apiBaseUrl: this.apiBaseUrl,
|
|
lastActiveAt: Date.now()
|
|
};
|
|
this.client.rPush(this.heartbeatKey, JSON.stringify(payload));
|
|
}, 3000);
|
|
}
|
|
}
|