feat: allowed user to customize the storage path of the h2 database #521

This commit is contained in:
tjq 2023-01-22 17:50:29 +08:00
parent b8199bf036
commit 8e94976cdd
5 changed files with 39 additions and 4 deletions

View File

@ -41,6 +41,8 @@ public class PowerJobDKey {
* <a href="https://stackoverflow.com/questions/16504140/thread-stop-deprecated">It's VERY dangerous</a> * <a href="https://stackoverflow.com/questions/16504140/thread-stop-deprecated">It's VERY dangerous</a>
*/ */
public static final String WORKER_ALLOWED_FORCE_STOP_THREAD = "powerjob.worker.allowed-force-stop-thread"; public static final String WORKER_ALLOWED_FORCE_STOP_THREAD = "powerjob.worker.allowed-force-stop-thread";
public static final String WORKER_WORK_SPACE = "powerjob.worker.workspace";
/** /**
* ms * ms
*/ */

View File

@ -1,5 +1,6 @@
package tech.powerjob.official.processors.impl.script; package tech.powerjob.official.processors.impl.script;
import tech.powerjob.worker.common.utils.PowerFileUtils;
import tech.powerjob.worker.core.processor.ProcessResult; import tech.powerjob.worker.core.processor.ProcessResult;
import tech.powerjob.worker.core.processor.TaskContext; import tech.powerjob.worker.core.processor.TaskContext;
import tech.powerjob.worker.log.OmsLogger; import tech.powerjob.worker.log.OmsLogger;
@ -34,7 +35,7 @@ public abstract class AbstractScriptProcessor extends CommonBasicProcessor {
protected static final String SH_SHELL = "/bin/sh"; protected static final String SH_SHELL = "/bin/sh";
protected static final String CMD_SHELL = "cmd.exe"; protected static final String CMD_SHELL = "cmd.exe";
private static final String WORKER_DIR = System.getProperty("user.home") + "/powerjob/worker/official_script_processor/"; private static final String WORKER_DIR = PowerFileUtils.workspace() + "/official_script_processor/";
@Override @Override
protected ProcessResult process0(TaskContext context) throws Exception { protected ProcessResult process0(TaskContext context) throws Exception {

View File

@ -0,0 +1,30 @@
package tech.powerjob.worker.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import tech.powerjob.common.PowerJobDKey;
/**
* 文件工具
*
* @author tjq
* @since 2023/1/22
*/
@Slf4j
public class PowerFileUtils {
/**
* 获取工作目录
* @return 允许用户通过启动配置文件自定义存储目录默认为 user.home
*/
public static String workspace() {
String workspaceByDKey = System.getProperty(PowerJobDKey.WORKER_WORK_SPACE);
if (StringUtils.isNotEmpty(workspaceByDKey)) {
log.info("[PowerFileUtils] [workspace] use custom workspace: {}", workspaceByDKey);
return workspaceByDKey;
}
final String userHome = System.getProperty("user.home").concat("/powerjob/worker");
log.info("[PowerFileUtils] [workspace] use user.home as workspace: {}", userHome);
return userHome;
}
}

View File

@ -10,6 +10,7 @@ import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import tech.powerjob.worker.common.WorkerRuntime; import tech.powerjob.worker.common.WorkerRuntime;
import tech.powerjob.worker.common.utils.PowerFileUtils;
import tech.powerjob.worker.common.utils.TransportUtils; import tech.powerjob.worker.common.utils.TransportUtils;
import java.io.File; import java.io.File;
@ -26,13 +27,13 @@ import java.util.Map;
@Slf4j @Slf4j
public class OmsContainerFactory { public class OmsContainerFactory {
private static final String CONTAINER_DIR = System.getProperty("user.home") + "/powerjob/worker/container/"; private static final String CONTAINER_DIR = PowerFileUtils.workspace() + "/container/";
private static final Map<Long, OmsContainer> CARGO = Maps.newConcurrentMap(); private static final Map<Long, OmsContainer> CARGO = Maps.newConcurrentMap();
/** /**
* 获取容器 * 获取容器
* @param containerId 容器ID * @param containerId 容器ID
* @param serverActor 当容器不存在且 serverActor 非空时尝试从服务端重新拉取容器 * @param workerRuntime 当容器不存在且 serverActor 非空时尝试从服务端重新拉取容器
* @return 容器示例可能为 null * @return 容器示例可能为 null
*/ */
public static OmsContainer fetchContainer(Long containerId, WorkerRuntime workerRuntime) { public static OmsContainer fetchContainer(Long containerId, WorkerRuntime workerRuntime) {

View File

@ -8,6 +8,7 @@ import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.h2.Driver; import org.h2.Driver;
import tech.powerjob.worker.common.utils.PowerFileUtils;
import javax.sql.DataSource; import javax.sql.DataSource;
import java.io.File; import java.io.File;
@ -25,7 +26,7 @@ public class ConnectionFactory {
private volatile DataSource dataSource; private volatile DataSource dataSource;
private final String H2_PATH = System.getProperty("user.home") + "/powerjob/worker/h2/" + CommonUtils.genUUID() + "/"; private final String H2_PATH = PowerFileUtils.workspace() + "/h2/" + CommonUtils.genUUID() + "/";
private final String DISK_JDBC_URL = String.format("jdbc:h2:file:%spowerjob_worker_db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false", H2_PATH); private final String DISK_JDBC_URL = String.format("jdbc:h2:file:%spowerjob_worker_db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false", H2_PATH);
private final String MEMORY_JDBC_URL = String.format("jdbc:h2:mem:%spowerjob_worker_db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false", H2_PATH); private final String MEMORY_JDBC_URL = String.format("jdbc:h2:mem:%spowerjob_worker_db;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false", H2_PATH);