设备运行记录年备份定时任务

master
wws 1 year ago
parent 4dd5c4895f
commit f5060ed72c

@ -30,4 +30,7 @@ public interface RemoteDeviceService {
@PostMapping("/deviceTask/createEquipmentOperationTask")
public R createEquipmentOperationTask();
@PostMapping("/deviceTask/equipmentBKTask")
public R equipmentBKTask();
}

@ -42,6 +42,11 @@ public class RemoteDeviceFallbackFactory implements FallbackFactory<RemoteDevice
public R createEquipmentOperationTask() {
return R.fail("设备运行记录生成失败:" + throwable.getMessage());
}
@Override
public R equipmentBKTask() {
return R.fail("设备运行记录数据库备份失败"+throwable.getMessage());
}
};
}
}

@ -76,4 +76,17 @@ public class DeviceTaskController {
}
return R.fail("设备运行记录生成失败");
}
/**
*
* @return
*/
@PostMapping("/equipmentBKTask")
public R equipmentBKTask() {
AjaxResult result = taskService.equipmentBKTask();
if (result.isSuccess()){
return R.ok("设备运行记录数据库备份成功");
}
return R.fail("设备运行记录数据库备份失败");
}
}

@ -5,7 +5,8 @@ import java.util.List;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.op.device.domain.EquOperation;
import com.op.device.domain.EquOperationRecord;
import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Param;
/**
* Mapper
@ -73,4 +74,16 @@ public interface EquOperationMapper {
// 校验当天运行记录数量
int checkInsertOperation();
// 获取当前YEAR-2的备份表的运行数据第一条
EquOperation checkBackupOperation();
// 获取备份插入数据数量
int getBackupOperationNum();
// 分批次备份
int backupOperation(@Param("startIndex") int startIndex,@Param("batchSize") int batchSize);
// 分批次删除
int deleteOperation(@Param("delSize") int delSize);
}

@ -19,4 +19,6 @@ public interface IDeviceTaskService {
AjaxResult createMaintenancePlanTask();
AjaxResult createEquipmentOperationTask();
AjaxResult equipmentBKTask();
}

@ -21,6 +21,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -210,6 +211,39 @@ public class DeviceTaskServiceImpl implements IDeviceTaskService {
return success();
}
/**
*
* @return
*/
@Override
public AjaxResult equipmentBKTask() {
// 加载sf-cloud库的sys_datasource
SysUser sysUser = new SysUser();
sysUser.setUserId(1L);
R<List<Map<String, String>>> dateSources0 = remoteUserService.getPoolNameList(sysUser);
List<Map<String, String>> dateSources = dateSources0.getData();
ExecutorService executorService = new ThreadPoolExecutor(
dateSources.size(),
dateSources.size(),
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
try {
dateSources.forEach(dateSource -> {
logger.info("++++++++++++" + dateSource.get("poolName") + "++++设备运行记录数据库备份开始++++++++++");
Runnable run = () -> backupEquipmentOperation(dateSource.get("poolName"));
executorService.execute(run);
});
} catch (Exception e) {
logger.error("service == dataClearTask == exception", e);
return error("service == dataClearTask == exception");
} finally {
executorService.shutdown();
}
return success();
}
public void createSpotCheckPlanFunc(String poolName){
DynamicDataSourceContextHolder.push(poolName);// 这是数据源的key
/**equ_plan equ_plan_equ**/
@ -431,7 +465,7 @@ public class DeviceTaskServiceImpl implements IDeviceTaskService {
operation.setCreateTime(DateUtils.getNowDate());
}
int batchSize = 200;// 批次数量
int batchSize = 100;// 批次数量
// 分批次处理
int batchCount = (operationList.size() + batchSize - 1)/batchSize;// 计算批次数量
@ -450,6 +484,40 @@ public class DeviceTaskServiceImpl implements IDeviceTaskService {
}
private void backupEquipmentOperation(String poolName) {
DynamicDataSourceContextHolder.push(poolName);// 这是数据源的key
// 校验当前YEAR-2是否备份过数据
EquOperation check = equOperationMapper.checkBackupOperation();
if (check == null) {
// 获取插入数量
int insertNum = equOperationMapper.getBackupOperationNum();
int insertSize = 100;// 新增批次数量
// 分批次备份
int batchCount = (insertNum + insertSize - 1)/insertSize;// 计算批次数量
for (int i = 0;i < batchCount; i++) {
int startIndex = i * insertSize;// 索引开始值
int endIndex = Math.min((i + 1)*insertSize,insertNum);// 索引结束值
// 备份至数据库
int num = equOperationMapper.backupOperation(startIndex,insertSize);
System.out.println("数据源:"+poolName+"--->"+"第"+(i+1)+"轮备份"+"开始索引:"+startIndex+"________结束索引:"+endIndex+"________成功插入数量:"+num);
}
// 删除源数据库数据
int delSize = 100;// 删除批次数量
for (int i = 0; i < insertNum/delSize+1; i++) {
// 在源数据库中删除
int num = equOperationMapper.deleteOperation(delSize);
System.out.println("数据源:"+poolName+"--->"+"第"+(i+1)+"轮删除"+"------>成功删除数量:"+num);
}
}else {
System.out.println("数据源:"+poolName+"---->"+"年份:"+( LocalDate.now().getYear()-2) +"--->已备份");
}
}
public static void main(String[] args) {
int batchSize = 200;// 批次数量

@ -119,6 +119,14 @@
where CONVERT(date, create_time) = CONVERT(date, GETDATE())
</select>
<select id="checkBackupOperation" resultMap="EquOperationResult">
select top 1 id from equ_operation_backup where YEAR(create_time) = YEAR(GETDATE())-2
</select>
<select id="getBackupOperationNum" resultType="java.lang.Integer">
select count(id) from equ_operation where YEAR(create_time) = YEAR(GETDATE())-2
</select>
<insert id="insertEquOperation" parameterType="EquOperation">
insert into equ_operation
<trim prefix="(" suffix=")" suffixOverrides=",">
@ -188,6 +196,15 @@
</foreach>
</insert>
<insert id="backupOperation">
insert into equ_operation_backup
select s.*
from equ_operation s
where YEAR (s.create_time) = YEAR (GETDATE())-2
ORDER BY id
OFFSET #{startIndex} ROWS FETCH NEXT #{batchSize} ROWS ONLY
</insert>
<update id="updateEquOperation" parameterType="EquOperation">
update equ_operation
<trim prefix="SET" suffixOverrides=",">
@ -230,4 +247,12 @@
#{id}
</foreach>
</delete>
<delete id="deleteOperation">
delete
from equ_operation
where id in (select s.id from equ_operation s where YEAR (s.create_time) = YEAR (GETDATE())-2
ORDER BY id
OFFSET 0 ROWS FETCH NEXT #{delSize} ROWS ONLY)
</delete>
</mapper>

@ -144,4 +144,10 @@ public class RyTask {
logger.info("++设备运行记录生成任务开始++createEquipmentOperationTask+++++");
remoteDeviceService.createEquipmentOperationTask();
}
/** 每年1月1日150执行 **/
public void equipmentBKTask(){
logger.info("++设备运行记录+数据库备份的数据+开始++equipmentBkTask+++++");
remoteDeviceService.equipmentBKTask();
}
}

Loading…
Cancel
Save