每日0点执行删除sub
parent
1f00faccd9
commit
a02514748c
|
|
@ -0,0 +1,82 @@
|
||||||
|
package com.agri.quartz.task;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnection;
|
||||||
|
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||||
|
import org.springframework.data.redis.core.Cursor;
|
||||||
|
import org.springframework.data.redis.core.ScanOptions;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Component("agriTask")
|
||||||
|
public class AgriTask {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private RedisConnectionFactory redisConnectionFactory;
|
||||||
|
private final static Logger log = LoggerFactory.getLogger(AgriTask.class);
|
||||||
|
/**
|
||||||
|
* 每日零点执行
|
||||||
|
* 删除 redis 所有sub: 开头的数据
|
||||||
|
*/
|
||||||
|
public void clearInvalidCache() {
|
||||||
|
|
||||||
|
log.info("===== 开始执行Redis sub: 键清理任务 =====");
|
||||||
|
RedisConnection connection = null;
|
||||||
|
Cursor<byte[]> cursor = null;
|
||||||
|
int deletedCount = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 获取Redis连接
|
||||||
|
connection = redisConnectionFactory.getConnection();
|
||||||
|
// 配置SCAN参数:匹配sub:*,分批遍历(每次1000条)
|
||||||
|
ScanOptions scanOptions = ScanOptions.scanOptions()
|
||||||
|
.match("sub:*")
|
||||||
|
.count(1000)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
// 遍历所有匹配的键
|
||||||
|
cursor = connection.scan(scanOptions);
|
||||||
|
List<byte[]> batchKeys = new ArrayList<>(1000); // 批量删除缓冲区
|
||||||
|
|
||||||
|
while (cursor.hasNext()) {
|
||||||
|
batchKeys.add(cursor.next());
|
||||||
|
// 每攒1000个键批量删除(减少网络交互)
|
||||||
|
if (batchKeys.size() >= 1000) {
|
||||||
|
deletedCount += batchKeys.size();
|
||||||
|
connection.del(batchKeys.toArray(new byte[0][]));
|
||||||
|
log.info("批量删除 {} 个sub: 键", batchKeys.size());
|
||||||
|
batchKeys.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除剩余的键
|
||||||
|
if (!batchKeys.isEmpty()) {
|
||||||
|
deletedCount += batchKeys.size();
|
||||||
|
connection.del(batchKeys.toArray(new byte[0][]));
|
||||||
|
log.info("批量删除剩余 {} 个sub: 键", batchKeys.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("===== Redis sub: 键清理完成,总计删除 {} 个键 =====", deletedCount);
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Redis sub: 键清理失败", e);
|
||||||
|
// 可选:添加告警逻辑(如钉钉/邮件通知)
|
||||||
|
} finally {
|
||||||
|
// 关闭游标和连接
|
||||||
|
if (cursor != null) {
|
||||||
|
try {
|
||||||
|
cursor.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("关闭游标失败", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (connection != null) {
|
||||||
|
connection.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -30,7 +30,7 @@ import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 卷膜自动模式定时检查
|
* 卷膜自动模式定时检查
|
||||||
* 每五分钟执行一次
|
* 每五分钟执行一次 每日时间区间内执行
|
||||||
* 1、查询大棚列表开启自动模式的大棚,获取imei
|
* 1、查询大棚列表开启自动模式的大棚,获取imei
|
||||||
* 2、根据imei查询dtu_data最后一分钟最后一条温度数据
|
* 2、根据imei查询dtu_data最后一分钟最后一条温度数据
|
||||||
* 3、查询参数设置获取 风口总长 预留风口长度 在条件内开启指定风口,第一次开 预留风口+条件内设定的风口长度 还是得存日志、、、
|
* 3、查询参数设置获取 风口总长 预留风口长度 在条件内开启指定风口,第一次开 预留风口+条件内设定的风口长度 还是得存日志、、、
|
||||||
|
|
@ -183,6 +183,7 @@ public class RollerAutoTask {
|
||||||
|
|
||||||
//判断温度是否在适宜温度内
|
//判断温度是否在适宜温度内
|
||||||
TempCommandStatus tempCommandStatus = TempJudgeUtil.judgeTempCommand(currentTemp, term.getTemp());
|
TempCommandStatus tempCommandStatus = TempJudgeUtil.judgeTempCommand(currentTemp, term.getTemp());
|
||||||
|
// todo 开关指令需要通知用户 推送主题 && 更新数据 前端重新请求消息表
|
||||||
if (tempCommandStatus == TempCommandStatus.OPEN) {
|
if (tempCommandStatus == TempCommandStatus.OPEN) {
|
||||||
// 开指令
|
// 开指令
|
||||||
sendOpenCommand(imei,agriName, roller, isFirstRun, term.getVent(), reservedLen);
|
sendOpenCommand(imei,agriName, roller, isFirstRun, term.getVent(), reservedLen);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue