Skip to content

Commit

Permalink
Refactor the method of obtaining user.dir (#3708)
Browse files Browse the repository at this point in the history
Signed-off-by: Zzm0809 <[email protected]>
Co-authored-by: Zzm0809 <[email protected]>
  • Loading branch information
Zzm0809 and Zzm0809 authored Aug 15, 2024
1 parent 92d9cc4 commit 5079687
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.dinky.context;

import org.dinky.aop.ProcessAspect;
import org.dinky.data.constant.DirConstant;
import org.dinky.data.enums.ProcessStatus;
import org.dinky.data.enums.ProcessStepType;
import org.dinky.data.enums.ProcessType;
Expand Down Expand Up @@ -98,7 +99,7 @@ public ProcessEntity getProcess(String processName) {
return logPross.get(processName);
}
try {
String filePath = String.format("%s/tmp/log/%s.json", System.getProperty("user.dir"), processName);
String filePath = String.format("%s/log/%s.json", DirConstant.getTempRootDir(), processName);
String string = FileUtil.readString(filePath, StandardCharsets.UTF_8);
ProcessEntity process = JSONObject.parseObject(string, ProcessEntity.class);
if (process.getStatus().isActiveStatus()) {
Expand All @@ -113,7 +114,7 @@ public ProcessEntity getProcess(String processName) {

public boolean clearProcessLog(String processName) {
// find process and delete
String filePath = String.format("%s/tmp/log/%s.json", System.getProperty("user.dir"), processName);
String filePath = String.format("%s/log/%s.json", DirConstant.getTempRootDir(), processName);
if (FileUtil.exist(filePath)) {
return FileUtil.del(filePath);
}
Expand Down Expand Up @@ -236,7 +237,7 @@ public synchronized void finishedProcess(String processName, ProcessStatus statu
if (e != null) {
appendLog(processName, null, LogUtil.getError(e.getCause()), true);
}
String filePath = String.format("%s/tmp/log/%s.json", System.getProperty("user.dir"), processName);
String filePath = String.format("%s/log/%s.json", DirConstant.getTempRootDir(), processName);
if (FileUtil.exist(filePath)) {
Assert.isTrue(FileUtil.del(filePath));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import org.dinky.data.annotations.CheckTaskOwner;
import org.dinky.data.annotations.Log;
import org.dinky.data.annotations.TaskId;
import org.dinky.data.constant.DirConstant;
import org.dinky.data.dto.CatalogueTaskDTO;
import org.dinky.data.dto.CatalogueTreeQueryDTO;
import org.dinky.data.enums.BusinessType;
import org.dinky.data.enums.Status;
import org.dinky.data.model.Catalogue;
import org.dinky.data.result.Result;
import org.dinky.data.vo.TreeVo;
import org.dinky.function.constant.PathConstant;
import org.dinky.service.TaskService;
import org.dinky.service.catalogue.CatalogueService;

Expand Down Expand Up @@ -77,7 +77,7 @@ public class CatalogueController {
@ApiOperation("Upload Zip Package And Create Catalogue")
public Result<String> upload(MultipartFile file, @PathVariable Integer id) {
// 获取上传的路径
String filePath = PathConstant.WORK_DIR;
String filePath = DirConstant.getRootPath();
// 获取源文件的名称
String fileName = file.getOriginalFilename();
String zipPath = filePath + File.separator + fileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.dinky.controller;

import org.dinky.assertion.Asserts;
import org.dinky.data.constant.DirConstant;
import org.dinky.data.exception.BusException;
import org.dinky.data.model.FlinkUdfManifest;
import org.dinky.function.constant.PathConstant;
Expand Down Expand Up @@ -107,8 +108,8 @@ public void downloadJavaUDF(@PathVariable Integer taskId, HttpServletResponse re
@GetMapping("downloadAppJar/{version}")
@ApiOperation("Download App Jar")
public void downloadAppJar(@PathVariable String version, HttpServletResponse resp) {
List<File> files = FileUtil.loopFiles(
PathConstant.WORK_DIR + "/jar", pathname -> pathname.getName().contains("dinky-app-" + version));
List<File> files = FileUtil.loopFiles(DirConstant.getRootPath() + "/jar", pathname -> pathname.getName()
.contains("dinky-app-" + version));
if (CollUtil.isNotEmpty(files)) {
ServletUtil.write(resp, files.get(0));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Result<List<TreeNodeDTO>> listLogDir() {
@ApiOperation("Get Root Log File Content")
@SaCheckPermission(PermissionConstants.SYSTEM_SETTING_INFO_ROOT_LOG)
public Result<String> getRootLog() {
return Result.data(systemService.readFile(DirConstant.ROOT_LOG_PATH));
return Result.data(systemService.readFile(DirConstant.getRootLog()));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class SystemServiceImpl implements SystemService {

@Override
public List<TreeNodeDTO> listLogDir() {
File systemLogFiles = TreeUtil.getFilesOfDir(DirConstant.LOG_DIR_PATH);
File systemLogFiles = TreeUtil.getFilesOfDir(DirConstant.getRootLogsPath());
return TreeUtil.treeNodeData(systemLogFiles, false);
}

Expand Down
6 changes: 3 additions & 3 deletions dinky-admin/src/test/java/org/dinky/utils/DirUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ public class DirUtilTest {
@Ignore
@Test
public void testListDirByPath() {
List<FileNode> dirList = DirUtil.listDirByPath(DirConstant.LOG_DIR_PATH);
List<FileNode> dirList = DirUtil.listDirByPath(DirConstant.getRootLogsPath());
Assertions.assertThat(dirList).isNotNull();
}

@Ignore
@Test
public void testReadFile() {
String result = DirUtil.readFile(DirConstant.LOG_DIR_PATH + "/dinky.log");
String result = DirUtil.readFile(DirConstant.getRootLog());
Assertions.assertThat(result).isNotNull();
}

@Ignore
@Test
public void testReadRootLog() {
String result = DirUtil.readFile(DirConstant.ROOT_LOG_PATH);
String result = DirUtil.readFile(DirConstant.getRootLog());
Assertions.assertThat(result).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.dinky.constant.FlinkSQLConstant;
import org.dinky.data.app.AppParamConfig;
import org.dinky.data.app.AppTask;
import org.dinky.data.constant.DirConstant;
import org.dinky.data.enums.GatewayType;
import org.dinky.data.model.SystemConfiguration;
import org.dinky.executor.Executor;
Expand Down Expand Up @@ -161,7 +162,7 @@ private static String readSql(Executor executor) {
if (!sqlFile.exists()) {
sqlFile = new File(confDir, sqlFileName);
if (!sqlFile.exists()) {
log.error("sql file not found,current dir:{},conf dir:{}", System.getProperty("user.dir"), confDir);
log.error("sql file not found,current dir:{},conf dir:{}", DirConstant.getRootPath(), confDir);
throw new RuntimeException("sql file not found");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;

import cn.hutool.core.util.StrUtil;
import cn.hutool.system.SystemUtil;

/**
Expand All @@ -29,9 +30,44 @@
* @since 2022/10/15 18:37
*/
public class DirConstant {

public static final String DINKY_ENV_OR_HOME_ROOT_DIR_NAME = "dinky.root.path";
public static final String FILE_SEPARATOR = File.separator;
public static final String ROOT_PATH = System.getProperty(SystemUtil.USER_DIR);
public static final String LOG_DIR_PATH = ROOT_PATH + FILE_SEPARATOR + "logs";
public static final String ROOT_LOG_PATH = LOG_DIR_PATH + FILE_SEPARATOR + "dinky.log";

// user dir path, default is project root path
public static final String USER_DIR_ENV_ROOT_PATH = System.getProperty(SystemUtil.USER_DIR);

// dinky env or home root path
public static final String DINKY_ENV_OR_HOME_ROOT_DIR = System.getProperty(DINKY_ENV_OR_HOME_ROOT_DIR_NAME);

/**
* get a root path of dinky
* @return root path of dinky
*/
public static String getRootPath() {
return StrUtil.isEmpty(DINKY_ENV_OR_HOME_ROOT_DIR) ? USER_DIR_ENV_ROOT_PATH : DINKY_ENV_OR_HOME_ROOT_DIR;
}

/**
* get a root logs path of dinky
* @return root logs path of dinky
*/
public static String getRootLogsPath() {
return getRootPath() + FILE_SEPARATOR + "logs";
}

/**
* get root log of dinky
* @return root log of dinky
*/
public static String getRootLog() {
return getRootLogsPath() + FILE_SEPARATOR + "dinky.log";
}

/**
* get tmp dir root of dinky
* @return tmp dir root of dinky
*/
public static String getTempRootDir() {
return getRootPath() + FILE_SEPARATOR + "tmp";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.dinky.utils;

import org.dinky.config.Docker;
import org.dinky.data.constant.DirConstant;
import org.dinky.docker.DockerClientBuilder;

import java.io.File;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class DockerClientUtils {
public DockerClientUtils(Docker docker) {
this.docker = docker;
this.dockerfile = FileUtil.writeUtf8String(
docker.getDockerfile(), System.getProperty("user.dir") + "/tmp/dockerfile/" + UUID.randomUUID());
docker.getDockerfile(), DirConstant.getTempRootDir() + "/dockerfile/" + UUID.randomUUID());
dockerClient = DockerClientBuilder.getInstance(DefaultDockerClientConfig.createDefaultConfigBuilder()
.withDockerHost(docker.getInstance())
.withRegistryUrl(docker.getRegistryUrl())
Expand Down
3 changes: 2 additions & 1 deletion dinky-common/src/main/java/org/dinky/utils/URLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.dinky.utils;

import org.dinky.assertion.Asserts;
import org.dinky.data.constant.DirConstant;

import java.io.File;
import java.net.MalformedURLException;
Expand All @@ -39,7 +40,7 @@
* @since 0.7.0
*/
public class URLUtils {
private static final String TMP_PATH = StrUtil.join(File.separator, System.getProperty("user.dir"), "tmp");
private static final String TMP_PATH = DirConstant.getTempRootDir();

/**
* url download file to local
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,34 +19,29 @@

package org.dinky.function.constant;

import org.dinky.data.constant.DirConstant;

import org.apache.flink.table.catalog.FunctionLanguage;

import java.io.File;

import cn.hutool.core.util.StrUtil;

/**
* 文件路径常量
*
* @since 0.6.8
*/
public class PathConstant {

/** 基本路径,dinky 部署的路径 */
public static final String WORK_DIR = System.getProperty("user.dir");
/** tmp路径 */
public static final String TMP_PATH = WORK_DIR + File.separator + "tmp" + File.separator;
/** Tmp path */
public static final String TMP_PATH = DirConstant.getTempRootDir() + File.separator;

/** udf路径 */
/** UDF path */
public static final String UDF_PATH = TMP_PATH + "udf" + File.separator;

public static final String COMPILER = "compiler";
public static final String PACKAGE = "package";
/** udf jar规则 */
/** UDF jar rules */
public static final String UDF_JAR_RULE = "udf-\\d+.jar";
/** udf版本规则 */
/** UDF version rules */
public static final String UDF_VERSION_RULE = "\\d+";
/** udf jar tmp名字 */
/**Udf jar tmp name */
public static final String UDF_JAR_TMP_NAME = "udf-tmp.jar";

public static final String UDF_JAR_NAME = "udf.jar";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.dinky.gateway.kubernetes;

import org.dinky.assertion.Asserts;
import org.dinky.data.constant.DirConstant;
import org.dinky.data.enums.Status;
import org.dinky.gateway.AbstractGateway;
import org.dinky.gateway.config.FlinkConfig;
Expand Down Expand Up @@ -72,7 +73,7 @@ public abstract class KubernetesGateway extends AbstractGateway {
private Pod defaultPodTemplate;

private K8sClientHelper k8sClientHelper;
private String tmpConfDir = String.format("%s/tmp/kubernets/%s", System.getProperty("user.dir"), UUID.randomUUID());
private String tmpConfDir = String.format("%s/kubernetes/%s", DirConstant.getTempRootDir(), UUID.randomUUID());

public KubernetesGateway() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.dinky.assertion.Asserts;
import org.dinky.constant.CustomerConfigureOptions;
import org.dinky.context.FlinkUdfPathContextHolder;
import org.dinky.data.constant.DirConstant;
import org.dinky.data.enums.JobStatus;
import org.dinky.data.model.CustomConfig;
import org.dinky.data.model.SystemConfiguration;
Expand Down Expand Up @@ -98,7 +99,7 @@
public abstract class YarnGateway extends AbstractGateway {
private static final String HTML_TAG_REGEX = "<pre>(.*)</pre>";
private final String TMP_SQL_EXEC_DIR =
String.format("%s/tmp/sql-exec/%s", System.getProperty("user.dir"), UUID.randomUUID());
String.format("%s/sql-exec/%s", DirConstant.getTempRootDir(), UUID.randomUUID());

protected YarnConfiguration yarnConfiguration;

Expand Down
8 changes: 4 additions & 4 deletions script/bin/auto.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fi
# OOM dump path
OOM_OPT="-XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${APP_HOME}/logs/heapdump.hprof"
# JVM parameters and log path
PARAMS_OPT="-Ddinky.logs.path=${DINKY_LOG_PATH} -Ddruid.mysql.usePingMethod=false -Dlog4j2.isThreadContextMapInheritable=true"
PARAMS_OPT="-Ddinky.logs.path=${DINKY_LOG_PATH} -Ddinky.root.path=${APP_HOME} -Ddruid.mysql.usePingMethod=false -Dlog4j2.isThreadContextMapInheritable=true"
# JAR parameters
JAR_PARAMS_OPT="--logging.config=${LOG_CONFIG}"
# JMX path
Expand Down Expand Up @@ -102,7 +102,7 @@ start() {
assertIsInputVersion
updatePid
if [ -z "$pid" ]; then
nohup java ${PARAMS_OPT} ${JVM_OPTS} ${OOM_OPT} ${GC_OPT} ${PARAMS_OPT} -Xverify:none -cp "${CLASS_PATH}" org.dinky.Dinky ${JAR_PARAMS_OPT} > ${DINKY_LOG_PATH}/dinky-start.log 2>&1 &
nohup java ${PARAMS_OPT} ${JVM_OPTS} ${OOM_OPT} ${GC_OPT} -Xverify:none -cp "${CLASS_PATH}" org.dinky.Dinky ${JAR_PARAMS_OPT} > ${DINKY_LOG_PATH}/dinky-start.log 2>&1 &
echo $! >"${PID_PATH}"/${PID_FILE}
echo "........................................Start Dinky Done........................................"
echo "current log path : ${DINKY_LOG_PATH}/dinky-start.log , you can execute tail -fn1000 ${DINKY_LOG_PATH}/dinky-start.log to watch the log"
Expand All @@ -115,7 +115,7 @@ startOnPending() {
assertIsInputVersion
updatePid
if [ -z "$pid" ]; then
java ${PARAMS_OPT} ${JVM_OPTS} ${OOM_OPT} ${GC_OPT} ${PARAMS_OPT} -Xverify:none -cp "${CLASS_PATH}" org.dinky.Dinky ${JAR_PARAMS_OPT}
java ${PARAMS_OPT} ${JVM_OPTS} ${OOM_OPT} ${GC_OPT} -Xverify:none -cp "${CLASS_PATH}" org.dinky.Dinky ${JAR_PARAMS_OPT}
echo "........................................Start Dinky Successfully........................................"
else
echo "Dinky pid $pid is in ${PID_PATH}/${PID_FILE}, Please stop first !!!"
Expand All @@ -126,7 +126,7 @@ startWithJmx() {
assertIsInputVersion
updatePid
if [ -z "$pid" ]; then
nohup java ${PARAMS_OPT} ${JVM_OPTS} ${OOM_OPT} ${GC_OPT} ${PARAMS_OPT} -Xverify:none "${JMX}" -cp "${CLASS_PATH}" org.dinky.Dinky ${JAR_PARAMS_OPT} > ${DINKY_LOG_PATH}/dinky-start.log 2>&1 &
nohup java ${PARAMS_OPT} ${JVM_OPTS} ${OOM_OPT} ${GC_OPT} -Xverify:none "${JMX}" -cp "${CLASS_PATH}" org.dinky.Dinky ${JAR_PARAMS_OPT} > ${DINKY_LOG_PATH}/dinky-start.log 2>&1 &
# echo $! >"${PID_PATH}"/${PID_FILE}
updatePid
echo "........................................Start Dinky with Jmx Successfully.....................................
Expand Down

0 comments on commit 5079687

Please sign in to comment.