mirror of
https://github.com/CodePhiliaX/Chat2DB.git
synced 2025-08-02 05:20:15 +08:00
Avoid process being occupied and optimize database initialization speed
This commit is contained in:
@ -1,11 +1,7 @@
|
|||||||
package ai.chat2db.server.domain.repository;
|
package ai.chat2db.server.domain.repository;
|
||||||
|
|
||||||
import ai.chat2db.server.domain.repository.entity.DataSourceDO;
|
|
||||||
import ai.chat2db.server.domain.repository.mapper.DataSourceMapper;
|
|
||||||
import ai.chat2db.server.tools.common.model.ConfigJson;
|
import ai.chat2db.server.tools.common.model.ConfigJson;
|
||||||
import ai.chat2db.server.tools.common.util.ConfigUtils;
|
import ai.chat2db.server.tools.common.util.ConfigUtils;
|
||||||
import cn.hutool.core.lang.UUID;
|
|
||||||
import com.alibaba.fastjson2.JSON;
|
|
||||||
import com.baomidou.mybatisplus.annotation.DbType;
|
import com.baomidou.mybatisplus.annotation.DbType;
|
||||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||||
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package ai.chat2db.server.start;
|
package ai.chat2db.server.start;
|
||||||
|
|
||||||
import ai.chat2db.server.domain.repository.Dbutils;
|
import ai.chat2db.server.domain.repository.Dbutils;
|
||||||
|
import ai.chat2db.server.tools.common.util.ConfigUtils;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
@ -27,7 +28,7 @@ import java.util.concurrent.CompletableFuture;
|
|||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//ConfigUtils.pid();
|
ConfigUtils.initProcess();
|
||||||
CompletableFuture.runAsync(() -> {
|
CompletableFuture.runAsync(() -> {
|
||||||
Dbutils.init();
|
Dbutils.init();
|
||||||
});
|
});
|
||||||
|
@ -2,6 +2,7 @@ package ai.chat2db.server.tools.common.util;
|
|||||||
|
|
||||||
import ai.chat2db.server.tools.common.model.ConfigJson;
|
import ai.chat2db.server.tools.common.model.ConfigJson;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.lang.UUID;
|
||||||
import com.alibaba.fastjson2.JSON;
|
import com.alibaba.fastjson2.JSON;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
@ -25,6 +26,9 @@ public class ConfigUtils {
|
|||||||
public static File configFile;
|
public static File configFile;
|
||||||
private static ConfigJson config = null;
|
private static ConfigJson config = null;
|
||||||
|
|
||||||
|
public static File clientIdFile;
|
||||||
|
private static String clientId = null;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
|
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
|
||||||
if (APP_PATH != null) {
|
if (APP_PATH != null) {
|
||||||
@ -39,6 +43,14 @@ public class ConfigUtils {
|
|||||||
if (!configFile.exists()) {
|
if (!configFile.exists()) {
|
||||||
FileUtil.writeUtf8String(JSON.toJSONString(new ConfigJson()), configFile);
|
FileUtil.writeUtf8String(JSON.toJSONString(new ConfigJson()), configFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
clientIdFile = new File(
|
||||||
|
CONFIG_BASE_PATH + File.separator + "config" + File.separator + "client_uuid");
|
||||||
|
if (!clientIdFile.exists()) {
|
||||||
|
String uuid = UUID.fastUUID().toString(true);
|
||||||
|
FileUtil.writeUtf8String(uuid, clientIdFile);
|
||||||
|
clientId = uuid;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateVersion(String version) {
|
public static void updateVersion(String version) {
|
||||||
@ -61,6 +73,7 @@ public class ConfigUtils {
|
|||||||
version = StringUtils.trim(FileUtil.readUtf8String(versionFile));
|
version = StringUtils.trim(FileUtil.readUtf8String(versionFile));
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getLatestLocalVersion() {
|
public static String getLatestLocalVersion() {
|
||||||
if (versionFile == null) {
|
if (versionFile == null) {
|
||||||
log.warn("VERSION_FILE is null");
|
log.warn("VERSION_FILE is null");
|
||||||
@ -77,6 +90,13 @@ public class ConfigUtils {
|
|||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String getClientId() {
|
||||||
|
if (clientId == null) {
|
||||||
|
clientId = StringUtils.trim(FileUtil.readUtf8String(clientIdFile));
|
||||||
|
}
|
||||||
|
return clientId;
|
||||||
|
}
|
||||||
|
|
||||||
public static void setConfig(ConfigJson config) {
|
public static void setConfig(ConfigJson config) {
|
||||||
String stringConfigJson = JSON.toJSONString(config);
|
String stringConfigJson = JSON.toJSONString(config);
|
||||||
FileUtil.writeUtf8String(stringConfigJson, configFile);
|
FileUtil.writeUtf8String(stringConfigJson, configFile);
|
||||||
@ -87,9 +107,6 @@ public class ConfigUtils {
|
|||||||
private static String getAppPath() {
|
private static String getAppPath() {
|
||||||
try {
|
try {
|
||||||
String jarPath = System.getProperty("project.path");
|
String jarPath = System.getProperty("project.path");
|
||||||
// log.info("user home: {}", System.getProperty("user.home"));
|
|
||||||
// log.info("project.path: {}", System.getProperty("project.path"));
|
|
||||||
// log.info("jarPath: {}", jarPath);
|
|
||||||
return FileUtil.getParent(jarPath, 4);
|
return FileUtil.getParent(jarPath, 4);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("getAppPath error", e);
|
log.error("getAppPath error", e);
|
||||||
@ -97,38 +114,36 @@ public class ConfigUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void pid() {
|
public static void initProcess() {
|
||||||
try {
|
try {
|
||||||
log.error("pidinit");
|
|
||||||
ProcessHandle currentProcess = ProcessHandle.current();
|
ProcessHandle currentProcess = ProcessHandle.current();
|
||||||
long pid = currentProcess.pid();
|
long pid = currentProcess.pid();
|
||||||
log.error("pid:{}", pid);
|
|
||||||
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
|
String environment = StringUtils.defaultString(System.getProperty("spring.profiles.active"), "dev");
|
||||||
File pidFile = new File(CONFIG_BASE_PATH + File.separator + "config" + File.separator + environment + "pid");
|
File pidFile = new File(CONFIG_BASE_PATH + File.separator + "config" + File.separator + environment + "app.pid");
|
||||||
if (!pidFile.exists()) {
|
if (!pidFile.exists()) {
|
||||||
FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
|
FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
|
||||||
} else {
|
} else {
|
||||||
String oldPid = FileUtil.readUtf8String(pidFile);
|
String oldPid = FileUtil.readUtf8String(pidFile);
|
||||||
log.error("oldPid:{}", oldPid);
|
log.info("oldPid:{}", oldPid);
|
||||||
if (StringUtils.isNotBlank(oldPid)) {
|
if (StringUtils.isNotBlank(oldPid)) {
|
||||||
Optional<ProcessHandle> processHandle = ProcessHandle.of(Long.parseLong(oldPid));
|
Optional<ProcessHandle> processHandle = ProcessHandle.of(Long.parseLong(oldPid));
|
||||||
log.error("processHandle:{}", JSON.toJSONString(processHandle));
|
//log.error("processHandle:{}", JSON.toJSONString(processHandle));
|
||||||
processHandle.ifPresent(handle -> {
|
processHandle.ifPresent(handle -> {
|
||||||
ProcessHandle.Info info = handle.info();
|
ProcessHandle.Info info = handle.info();
|
||||||
String[] arguments = info.arguments().orElse(null);
|
String[] arguments = info.arguments().orElse(null);
|
||||||
log.error("arguments:{}", JSON.toJSONString(arguments));
|
log.info("arguments:{}", JSON.toJSONString(arguments));
|
||||||
if (arguments == null) {
|
if (arguments == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for (String argument : arguments) {
|
for (String argument : arguments) {
|
||||||
if (StringUtils.equals("chat2db-server-start.jar", argument)) {
|
if (StringUtils.equals("chat2db-server-start.jar", argument)) {
|
||||||
handle.destroy();
|
handle.destroy();
|
||||||
log.error("destroy old process--------");
|
log.info("destroy old process--------");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (StringUtils.equals("application", argument)) {
|
if (argument.contains("Application")) {
|
||||||
handle.destroy();
|
handle.destroy();
|
||||||
log.error("destroy old process--------");
|
log.info("destroy old process--------");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -138,8 +153,8 @@ public class ConfigUtils {
|
|||||||
FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
|
FileUtil.writeUtf8String(String.valueOf(pid), pidFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
}catch (Exception e){
|
} catch (Exception e) {
|
||||||
log.error("updatePid error",e);
|
log.error("updatePid error", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import ai.chat2db.server.domain.api.service.DlTemplateService;
|
|||||||
import ai.chat2db.server.tools.base.enums.DataSourceTypeEnum;
|
import ai.chat2db.server.tools.base.enums.DataSourceTypeEnum;
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
import ai.chat2db.server.tools.base.wrapper.result.DataResult;
|
||||||
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
|
import ai.chat2db.server.tools.base.wrapper.result.ListResult;
|
||||||
|
import ai.chat2db.server.tools.common.util.ConfigUtils;
|
||||||
import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
|
import ai.chat2db.server.web.api.aspect.ConnectionInfoAspect;
|
||||||
import ai.chat2db.server.web.api.controller.ai.chat2db.client.Chat2dbAIClient;
|
import ai.chat2db.server.web.api.controller.ai.chat2db.client.Chat2dbAIClient;
|
||||||
import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter;
|
import ai.chat2db.server.web.api.controller.rdb.converter.RdbWebConverter;
|
||||||
@ -70,7 +71,7 @@ public class RdbDmlController {
|
|||||||
ListResult<ExecuteResult> resultDTOListResult = dlTemplateService.execute(param);
|
ListResult<ExecuteResult> resultDTOListResult = dlTemplateService.execute(param);
|
||||||
List<ExecuteResultVO> resultVOS = rdbWebConverter.dto2vo(resultDTOListResult.getData());
|
List<ExecuteResultVO> resultVOS = rdbWebConverter.dto2vo(resultDTOListResult.getData());
|
||||||
String type = Chat2DBContext.getConnectInfo().getDbType();
|
String type = Chat2DBContext.getConnectInfo().getDbType();
|
||||||
String clientId = getClientId(request.getClientId());
|
String clientId = getClientId();
|
||||||
String sqlContent = request.getSql();
|
String sqlContent = request.getSql();
|
||||||
executorService.submit(() -> {
|
executorService.submit(() -> {
|
||||||
try {
|
try {
|
||||||
@ -102,11 +103,11 @@ public class RdbDmlController {
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private String getClientId(String clientId) {
|
private String getClientId() {
|
||||||
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
|
ConfigService configService = ApplicationContextUtil.getBean(ConfigService.class);
|
||||||
Config keyConfig = configService.find(Chat2dbAIClient.CHAT2DB_OPENAI_KEY).getData();
|
Config keyConfig = configService.find(Chat2dbAIClient.CHAT2DB_OPENAI_KEY).getData();
|
||||||
if (Objects.isNull(keyConfig) || StringUtils.isBlank(keyConfig.getContent())) {
|
if (Objects.isNull(keyConfig) || StringUtils.isBlank(keyConfig.getContent())) {
|
||||||
return clientId;
|
return ConfigUtils.getClientId();
|
||||||
}
|
}
|
||||||
return keyConfig.getContent();
|
return keyConfig.getContent();
|
||||||
}
|
}
|
||||||
@ -149,7 +150,7 @@ public class RdbDmlController {
|
|||||||
ExecuteResultVO executeResultVO = rdbWebConverter.dto2vo(result.getData());
|
ExecuteResultVO executeResultVO = rdbWebConverter.dto2vo(result.getData());
|
||||||
String type = Chat2DBContext.getConnectInfo().getDbType();
|
String type = Chat2DBContext.getConnectInfo().getDbType();
|
||||||
String sqlContent = request.getSql();
|
String sqlContent = request.getSql();
|
||||||
String clientId = getClientId(request.getClientId());
|
String clientId = getClientId();
|
||||||
executorService.submit(() -> {
|
executorService.submit(() -> {
|
||||||
try {
|
try {
|
||||||
addOperationLog(clientId, type, sqlContent, result.getErrorMessage(), result.getSuccess(), Lists.newArrayList(executeResultVO));
|
addOperationLog(clientId, type, sqlContent, result.getErrorMessage(), result.getSuccess(), Lists.newArrayList(executeResultVO));
|
||||||
|
@ -45,8 +45,4 @@ public class DmlRequest extends DataSourceBaseRequest implements DataSourceConso
|
|||||||
*/
|
*/
|
||||||
private Boolean pageSizeAll;
|
private Boolean pageSizeAll;
|
||||||
|
|
||||||
/**
|
|
||||||
* 客户端id
|
|
||||||
*/
|
|
||||||
private String clientId;
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user