fix 迁移数据添加更多日志输出

This commit is contained in:
bwcx_jzy 2023-01-09 14:53:50 +08:00
parent bab26ccadc
commit 32ba894661
No known key found for this signature in database
GPG Key ID: 5E48E9372088B9E5
4 changed files with 39 additions and 25 deletions

View File

@ -1,5 +1,13 @@
# 🚀 版本日志
## 2.10.11
### 🐞 解决BUG、优化功能
1. 【server】优化 迁移数据添加更多日志输出
------
## 2.10.10 (2023-01-09)
### 🐣 新增功能

View File

@ -72,8 +72,13 @@ import java.util.stream.Collectors;
public class InitDb implements DisposableBean, ILoadEvent {
private final List<Runnable> BEFORE_CALLBACK = new LinkedList<>();
private final List<Supplier<Boolean>> AFTER_CALLBACK = new LinkedList<>();
private final Map<String, Supplier<Boolean>> AFTER_CALLBACK = new LinkedHashMap<>();
private final DbExtConfig dbExtConfig;
private final BackupInfoService backupInfoService;
/**
* 恢复 sql 文件
*/
private File recoverSqlFile;
public InitDb(DbExtConfig dbExtConfig, BackupInfoService backupInfoService) {
this.dbExtConfig = dbExtConfig;
@ -87,22 +92,18 @@ public class InitDb implements DisposableBean, ILoadEvent {
/**
* 添加监听回调
*
* @param name 事件名称
* @param supplier 回调返回 true 需要重新初始化数据库
*/
public void addCallback(Supplier<Boolean> supplier) {
AFTER_CALLBACK.add(supplier);
public void addCallback(String name, Supplier<Boolean> supplier) {
AFTER_CALLBACK.put(name, supplier);
}
private final DbExtConfig dbExtConfig;
private final BackupInfoService backupInfoService;
/**
* 恢复 sql 文件
*/
private File recoverSqlFile;
private void init() {
@SuppressWarnings("rawtypes")
public void afterPropertiesSet(ApplicationContext applicationContext) {
this.prepareCallback(applicationContext.getEnvironment());
//
log.debug("需要执行 {} 个前置事件", BEFORE_CALLBACK.size());
BEFORE_CALLBACK.forEach(Runnable::run);
IStorageService storageService = StorageServiceFactory.get();
log.info("start load {} db", dbExtConfig.getMode());
@ -147,7 +148,16 @@ public class InitDb implements DisposableBean, ILoadEvent {
//
StorageServiceFactory.saveExecuteSqlLog(executeSqlLog);
// 执行回调方法
long count = AFTER_CALLBACK.stream().mapToInt(value -> value.get() ? 1 : 0).count();
log.debug("需要执行 {} 个回调", AFTER_CALLBACK.size());
long count = AFTER_CALLBACK.entrySet()
.stream()
.mapToInt(value -> {
log.info("开始执行数据库事件:{}", value.getKey());
Supplier<Boolean> supplier = value.getValue();
boolean arg2 = supplier.get();
log.info("数据库 {} 事件执行结束,结果:{}", value.getKey(), arg2);
return arg2 ? 1 : 0;
}).sum();
if (count > 0) {
// 因为导入数据后数据结构可能发生变动
// 第二次初始化数据库
@ -254,7 +264,7 @@ public class InitDb implements DisposableBean, ILoadEvent {
});
Opt.ofNullable(environment.getProperty("backup-h2")).ifPresent(s -> {
// 备份数据库
this.addCallback(() -> {
this.addCallback("备份数据库", () -> {
log.info("Start backing up the database");
Future<BackupInfoModel> backupInfoModelFuture = backupInfoService.autoBackup();
try {
@ -292,16 +302,17 @@ public class InitDb implements DisposableBean, ILoadEvent {
String user = environment.getProperty("h2-user");
String url = environment.getProperty("h2-url");
String pass = environment.getProperty("h2-pass");
this.addCallback(() -> {
this.addCallback("迁移数据", () -> {
//
StorageServiceFactory.migrateH2ToNow(dbExtConfig, url, user, pass);
return false;
});
log.info("开始等待数据迁移");
});
}
private void importH2Sql(Environment environment, String importH2Sql) {
this.addCallback(() -> {
this.addCallback("导入数据", () -> {
File file = FileUtil.file(importH2Sql);
String sqlPath = FileUtil.getAbsolutePath(file);
if (!FileUtil.isFile(file)) {
@ -320,12 +331,6 @@ public class InitDb implements DisposableBean, ILoadEvent {
});
}
@Override
public void afterPropertiesSet(ApplicationContext applicationContext) throws Exception {
this.prepareCallback(applicationContext.getEnvironment());
this.init();
}
@Override
public int getOrder() {
return HIGHEST_PRECEDENCE;

View File

@ -79,7 +79,7 @@ jpom:
initial-size: 10
max-wait: 10
min-idle: 1
show-sql: true
show-sql: false
# 构建相关配置
build:
# 最多保存多少份历史记录

View File

@ -120,7 +120,7 @@ public class StorageServiceFactory {
return ArrayUtil.contains(modes, dbExtConfig.getMode());
})
.sorted((o1, o2) -> StrUtil.compare(o1.getSimpleName(), o2.getSimpleName(), false))
.collect(Collectors.toSet());
.collect(Collectors.toCollection(LinkedHashSet::new));
log.info("准备迁移数据");
int total = 0;
for (Class<?> aClass : classes) {
@ -130,6 +130,7 @@ public class StorageServiceFactory {
log.info("迁移完成,累计迁移 {} 条数据,耗时:{}", total, DateUtil.formatBetween(endTime - time));
h2DsFactory.destroy();
nowDsFactory.destroy();
log.info("准备删除当前数据库文件");
String dbFiles = h2StorageService.deleteDbFiles();
log.info("自动备份 h2 数据库文件,备份文件位于:{}", dbFiles);
} catch (Exception e) {