[dev] use random path for h2 to support startup multi instance in on pc

This commit is contained in:
朱八 2020-07-30 20:25:42 +08:00
parent 2d3e3af4f3
commit 46763ac325
10 changed files with 69 additions and 17 deletions

View File

@ -7,6 +7,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.util.Collection;
import java.util.UUID;
import java.util.function.Supplier;
@ -146,4 +147,12 @@ public class CommonUtils {
return OmsConstant.NONE;
}
/**
* 生成 UUID
* @return uuid
*/
public static String genUUID() {
return StringUtils.replace(UUID.randomUUID().toString(), "-", "");
}
}

View File

@ -2,6 +2,7 @@ package com.github.kfcfans.powerjob.server;
import com.github.kfcfans.powerjob.server.akka.OhMyServer;
import com.github.kfcfans.powerjob.server.common.utils.OmsFileUtils;
import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.boot.SpringApplication;
@ -45,14 +46,18 @@ public class OhMyApplication {
}
}
private static void pre() {
@VisibleForTesting
public static void pre() {
log.info(TIPS);
// 删除历史遗留的 H2 数据库文件
File oldH2 = new File(OmsFileUtils.genH2BasePath());
try {
FileUtils.forceDeleteOnExit(new File(OmsFileUtils.genH2Path()));
if (oldH2.exists()) {
FileUtils.forceDelete(oldH2);
}
}catch (Exception e) {
log.warn("[PowerJob] delete h2 workspace({}) failed, if server can't startup successfully, please delete it manually", OmsFileUtils.genH2Path(), e);
log.warn("[PowerJob] delete h2 workspace({}) failed, if server can't startup successfully, please delete it manually", oldH2, e);
}
}

View File

@ -1,13 +1,12 @@
package com.github.kfcfans.powerjob.server.common.utils;
import org.apache.commons.lang3.StringUtils;
import com.github.kfcfans.powerjob.common.utils.CommonUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.util.DigestUtils;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;
/**
* 文件工具类统一文件存放地址
@ -49,17 +48,19 @@ public class OmsFileUtils {
* @return 临时目录
*/
public static String genTemporaryWorkPath() {
String uuid = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
return genTemporaryPath() + uuid + "/";
return genTemporaryPath() + CommonUtils.genUUID() + "/";
}
/**
* 获取 H2 数据库工作目录
* @return H2 工作目录
*/
public static String genH2Path() {
public static String genH2BasePath() {
return COMMON_PATH + "h2/";
}
public static String genH2WorkPath() {
return genH2BasePath() + CommonUtils.genUUID() + "/";
}
/**
* 将文本写入文件

View File

@ -36,7 +36,7 @@ public class MultiDatasourceConfig {
public DataSource initOmsLocalDatasource() {
HikariConfig config = new HikariConfig();
config.setDriverClassName(H2_DRIVER_CLASS_NAME);
config.setJdbcUrl(String.format(H2_JDBC_URL_PATTERN, OmsFileUtils.genH2Path()));
config.setJdbcUrl(String.format(H2_JDBC_URL_PATTERN, OmsFileUtils.genH2WorkPath()));
config.setAutoCommit(true);
// 池中最小空闲连接数量
config.setMinimumIdle(H2_MIN_SIZE);

View File

@ -1,5 +1,6 @@
package com.github.kfcfans.powerjob.server.test;
import com.github.kfcfans.powerjob.server.OhMyApplication;
import com.github.kfcfans.powerjob.server.common.utils.CronExpression;
import com.github.kfcfans.powerjob.server.common.utils.timewheel.HashedWheelTimer;
import com.github.kfcfans.powerjob.server.common.utils.timewheel.TimerFuture;
@ -91,4 +92,9 @@ public class UtilsTest {
System.out.println(StringUtils.containsWhitespace(goodAppName));
System.out.println(StringUtils.containsWhitespace(appName));
}
@Test
public void testPre() {
OhMyApplication.pre();
}
}

View File

@ -34,8 +34,7 @@ public class OhMySchedulerConfig {
config.setAppName("powerjob-agent-test");
config.setServerAddress(serverAddress);
// 如果没有大型 Map/MapReduce 的需求建议使用内存来加速计算
// 为了本地模拟多个实例只能使用 MEMORY 启动文件只能由一个应用占有
config.setStoreStrategy(StoreStrategy.MEMORY);
config.setStoreStrategy(StoreStrategy.DISK);
// 2. 创建 Worker 对象设置配置文件
OhMyWorker ohMyWorker = new OhMyWorker();

View File

@ -0,0 +1,25 @@
package com.github.kfcfans.powerjob.samples.tester;
import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult;
import com.github.kfcfans.powerjob.worker.core.processor.TaskContext;
import com.github.kfcfans.powerjob.worker.core.processor.sdk.BasicProcessor;
import org.springframework.stereotype.Component;
/**
* 测试用户反馈的无法停止实例的问题
* https://github.com/KFCFans/PowerJob/issues/37
*
* @author tjq
* @since 2020/7/30
*/
@Component
public class StopInstanceTester implements BasicProcessor {
@Override
public ProcessResult process(TaskContext context) throws Exception {
int i = 0;
while (true) {
System.out.println(i++);
Thread.sleep(1000*10);
}
}
}

View File

@ -186,11 +186,13 @@ public class OhMyWorker implements ApplicationContextAware, InitializingBean, Di
private static void pre() {
// 删除历史遗留的 H2 数据库文件
String h2Path = OmsWorkerFileUtils.getH2Dir();
File oldH2File = new File(OmsWorkerFileUtils.getH2BaseDir());
try {
FileUtils.forceDeleteOnExit(new File(h2Path));
if (oldH2File.exists()) {
FileUtils.forceDelete(oldH2File);
}
}catch (Exception e) {
log.warn("[PowerJob] delete h2 workspace({}) failed, if worker can't startup successfully, please delete it manually", h2Path, e);
log.warn("[PowerJob] delete h2 workspace({}) failed, if worker can't startup successfully, please delete it manually", oldH2File, e);
}
}
}

View File

@ -1,5 +1,7 @@
package com.github.kfcfans.powerjob.worker.common.utils;
import com.github.kfcfans.powerjob.common.utils.CommonUtils;
/**
* 文件工具类
*
@ -19,7 +21,10 @@ public class OmsWorkerFileUtils {
return WORKER_DIR + "container/";
}
public static String getH2Dir() {
public static String getH2BaseDir() {
return WORKER_DIR + "h2/";
}
public static String getH2WorkDir() {
return WORKER_DIR + "h2/" + CommonUtils.genUUID() + "/";
}
}

View File

@ -20,8 +20,8 @@ public class ConnectionFactory {
private static volatile DataSource dataSource;
private static final String DISK_JDBC_URL = String.format("jdbc:h2:file:%spowerjob_worker_db", OmsWorkerFileUtils.getH2Dir());
private static final String MEMORY_JDBC_URL = String.format("jdbc:h2:mem:%spowerjob_worker_db", OmsWorkerFileUtils.getH2Dir());
private static final String DISK_JDBC_URL = String.format("jdbc:h2:file:%spowerjob_worker_db", OmsWorkerFileUtils.getH2WorkDir());
private static final String MEMORY_JDBC_URL = String.format("jdbc:h2:mem:%spowerjob_worker_db", OmsWorkerFileUtils.getH2WorkDir());
public static Connection getConnection() throws SQLException {
return getDataSource().getConnection();