diff --git a/others/dev/docker-compose.yml b/others/dev/docker-compose.yml index 3f1b9bdb..9e7a5655 100644 --- a/others/dev/docker-compose.yml +++ b/others/dev/docker-compose.yml @@ -11,7 +11,7 @@ services: deploy: resources: limits: - memory: 768M + memory: 1024M restart: always container_name: powerjob-mysql image: powerjob/powerjob-mysql:test_env @@ -43,7 +43,7 @@ services: deploy: resources: limits: - memory: 896M + memory: 1024M container_name: powerjob-server image: powerjob/powerjob-server:test_env restart: always @@ -52,7 +52,7 @@ services: # - powerjob-mongodb environment: PARAMS: "--spring.profiles.active=daily --spring.datasource.core.jdbc-url=jdbc:mysql://powerjob-mysql:3306/powerjob-daily?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai --oms.storage.dfs.mysql_series.url=jdbc:mysql://powerjob-mysql:3306/powerjob-daily?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai" - JVMOPTIONS: "-server -XX:+UseG1GC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=7 -XX:GCLogFileSize=100M -Xloggc:/root/powerjob/server/gc.log" + JVMOPTIONS: "-server -XX:+UseG1GC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=7 -XX:GCLogFileSize=100M -Xloggc:/root/powerjob/server/gc.log -Dpowerjob.server.test.mode=true" ports: - "7700:7700" - "10086:10086" @@ -67,7 +67,7 @@ services: deploy: resources: limits: - memory: 384M + memory: 512M container_name: powerjob-worker-agent image: powerjob/powerjob-worker-agent:test_env restart: always @@ -89,7 +89,7 @@ services: deploy: resources: limits: - memory: 384M + memory: 512M container_name: powerjob-worker-agent2 image: powerjob/powerjob-worker-agent:test_env restart: always diff --git a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/NewSystemInitializer.java b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/NewSystemInitializer.java index 3f0549e2..f0e1be67 100644 --- a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/NewSystemInitializer.java +++ b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/NewSystemInitializer.java @@ -40,22 +40,23 @@ public class NewSystemInitializer implements CommandLineRunner { @Override public void run(String... args) throws Exception { - SystemInitializerContext context = new SystemInitializerContext(); - initSystemAdmin(context); - initDefaultNamespace(context); - initTestEnvironment(context); + clusterInit(SystemInitializeService.GOAL_INIT_LOCK, Void -> { + initSystemAdmin(); + initDefaultNamespace(); + initTestEnvironment(); + }); } - private void initSystemAdmin(SystemInitializerContext context) { - clusterInit(SystemInitializeService.GOAL_INIT_ADMIN, Void -> systemInitializeService.initAdmin(context)); + private void initSystemAdmin() { + clusterInit(SystemInitializeService.GOAL_INIT_ADMIN, Void -> systemInitializeService.initAdmin()); } - private void initDefaultNamespace(SystemInitializerContext context) { - clusterInit(SystemInitializeService.GOAL_INIT_NAMESPACE, Void -> systemInitializeService.initNamespace(context)); + private void initDefaultNamespace() { + clusterInit(SystemInitializeService.GOAL_INIT_NAMESPACE, Void -> systemInitializeService.initNamespace()); } - private void initTestEnvironment(SystemInitializerContext context) { - clusterInit(SystemInitializeService.GOAL_INIT_TEST_ENV, Void -> systemInitializeService.initTestEnvironment(context)); + private void initTestEnvironment() { + clusterInit(SystemInitializeService.GOAL_INIT_TEST_ENV, Void -> systemInitializeService.initTestEnvironment()); } private void clusterInit(String name, Consumer initFunc) { diff --git a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/SystemInitializeService.java b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/SystemInitializeService.java index 4cf34e01..db16db4d 100644 --- a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/SystemInitializeService.java +++ b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/SystemInitializeService.java @@ -8,6 +8,7 @@ package tech.powerjob.server.initializer; */ public interface SystemInitializeService { + String GOAL_INIT_LOCK = "goal_init_system"; String GOAL_INIT_ADMIN = "goal_init_admin"; String GOAL_INIT_NAMESPACE = "goal_init_namespace"; String GOAL_INIT_TEST_ENV = "goal_init_test_env"; @@ -16,15 +17,15 @@ public interface SystemInitializeService { /** * 初始化超级管理员 */ - void initAdmin(SystemInitializerContext context); + void initAdmin(); /** * 初始化 namespace */ - void initNamespace(SystemInitializerContext context); + void initNamespace(); /** * 初始化测试环境 */ - void initTestEnvironment(SystemInitializerContext context); + void initTestEnvironment(); } diff --git a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/SystemInitializeServiceImpl.java b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/SystemInitializeServiceImpl.java index 435c17e6..8ecb61aa 100644 --- a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/SystemInitializeServiceImpl.java +++ b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/initializer/SystemInitializeServiceImpl.java @@ -5,6 +5,7 @@ import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import tech.powerjob.common.PowerJobDKey; @@ -32,6 +33,7 @@ import javax.annotation.Resource; import javax.transaction.Transactional; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; /** @@ -65,14 +67,13 @@ public class SystemInitializeServiceImpl implements SystemInitializeService { @Override @Transactional(rollbackOn = Exception.class) - public void initAdmin(SystemInitializerContext context) { + public void initAdmin() { String username = SYSTEM_ADMIN_NAME; String password = StringUtils.isEmpty(defaultAdminPassword) ? RandomStringUtils.randomAlphabetic(8) : defaultAdminPassword; // 创建用户 PowerJobUser powerJobUser = createUser(username, password, true); - context.setAdminUserId(powerJobUser.getId()); // 授予全局管理员权限 powerJobPermissionService.grantRole(RoleScope.GLOBAL, AuthConstants.GLOBAL_ADMIN_TARGET_ID, powerJobUser.getId(), Role.ADMIN, null); @@ -80,26 +81,32 @@ public class SystemInitializeServiceImpl implements SystemInitializeService { // 循环10遍,强提醒用户,第一次使用必须更改 admin 密码 for (int i = 0; i < 10; i++) { - log.warn("[SystemInitializeService] The system has automatically created a super administrator account[username={},password={}], please log in and change the password immediately!", username, password); + log.warn("[SystemInitializeService] The system has automatically created a super administrator account[username={},password={}], please login and change the password immediately!", username, password); } } @Override @Transactional(rollbackOn = Exception.class) - public void initNamespace(SystemInitializerContext context) { + public void initNamespace() { + + Optional namespaceDOOptional = namespaceWebService.findByCode(SYSTEM_DEFAULT_NAMESPACE); + if (namespaceDOOptional.isPresent()) { + log.info("[SystemInitializeService] namespace[{}] already exist", SYSTEM_DEFAULT_NAMESPACE); + return; + } + ModifyNamespaceRequest saveNamespaceReq = new ModifyNamespaceRequest(); saveNamespaceReq.setName(SYSTEM_DEFAULT_NAMESPACE); saveNamespaceReq.setCode(SYSTEM_DEFAULT_NAMESPACE); log.info("[SystemInitializeService] create default namespace by request: {}", saveNamespaceReq); NamespaceDO savedNamespaceDO = namespaceWebService.save(saveNamespaceReq); - context.setDefaultNamespaceId(savedNamespaceDO.getId()); log.info("[SystemInitializeService] create default namespace successfully: {}", savedNamespaceDO); } @Override @Transactional(rollbackOn = Exception.class) - public void initTestEnvironment(SystemInitializerContext context) { + public void initTestEnvironment() { String testMode = System.getProperty(PowerJobDKey.SERVER_TEST_MODE); if (!Boolean.TRUE.toString().equalsIgnoreCase(testMode)) { return; @@ -119,32 +126,38 @@ public class SystemInitializeServiceImpl implements SystemInitializeService { String testAppName = System.getProperty(PowerJobDKey.SERVER_TEST_APP_USERNAME, "powerjob-worker-samples"); String testAppPassword = System.getProperty(PowerJobDKey.SERVER_TEST_APP_PASSWORD, "powerjob123"); - AppInfoDO testApp = createApp(testAppName, testAppPassword, context.getDefaultNamespaceId(), testUserIds); + AppInfoDO testApp = createApp(testAppName, testAppPassword, SYSTEM_DEFAULT_NAMESPACE, testUserIds); log.info("[SystemInitializeService] [TestEnv] test app: {}", testApp); } private PowerJobUser createUser(String username, String password, boolean allowedChangePwd) { // STEP1: 创建 PWJB 用户 - ModifyUserInfoRequest createUser = new ModifyUserInfoRequest(); - createUser.setUsername(username); - createUser.setNick(username); - createUser.setPassword(password); - if (!allowedChangePwd) { - Map extra = Maps.newHashMap(); - extra.put("allowedChangePwd", false); - createUser.setExtra(JsonUtils.toJSONString(extra)); - } + Optional pwjbUserOpt = pwjbUserWebService.findByUsername(username); + PwjbUserInfoDO savedPwjbUser = pwjbUserOpt.orElseGet(() -> { + ModifyUserInfoRequest createUser = new ModifyUserInfoRequest(); + createUser.setUsername(username); + createUser.setNick(username); + createUser.setPassword(password); - log.info("[SystemInitializeService] [username:{}] create PWJB user by request: {}", username, createUser); - PwjbUserInfoDO savedPwjbUser = pwjbUserWebService.save(createUser); - log.info("[SystemInitializeService] [username:{}] create PWJB user successfully: {}", username, savedPwjbUser); + if (!allowedChangePwd) { + Map extra = Maps.newHashMap(); + extra.put("allowedChangePwd", false); + createUser.setExtra(JsonUtils.toJSONString(extra)); + } + log.info("[SystemInitializeService] [username:{}] create PWJB user by request: {}", username, createUser); + PwjbUserInfoDO nPwjbUser = pwjbUserWebService.save(createUser); + log.info("[SystemInitializeService] [username:{}] create PWJB user successfully: {}", username, nPwjbUser); + return nPwjbUser; + }); + log.info("[SystemInitializeService] [username:{}] => PwjbUser: {}", username, savedPwjbUser); + + // STEP2: 创建 USER 对象 Map params = Maps.newHashMap(); params.put(AuthConstants.PARAM_KEY_USERNAME, username); params.put(AuthConstants.PARAM_KEY_PASSWORD, password); - // STEP2: 创建 USER 对象 LoginRequest loginRequest = new LoginRequest() .setLoginType(AuthConstants.ACCOUNT_TYPE_POWER_JOB) .setOriginParams(JsonUtils.toJSONString(params)); @@ -154,14 +167,22 @@ public class SystemInitializeServiceImpl implements SystemInitializeService { return powerJobUser; } - private AppInfoDO createApp(String appName, String password, Long namespaceId, List developers) { + private AppInfoDO createApp(String appName, String password, String namespaceCode, List developers) { + ModifyAppInfoRequest modifyAppInfoRequest = new ModifyAppInfoRequest(); + // 应用已存在则转为更新模式 + Optional oldAppOpt = appWebService.findByAppName(appName); + oldAppOpt.ifPresent(appInfoDO -> { + BeanUtils.copyProperties(appInfoDO, modifyAppInfoRequest); + modifyAppInfoRequest.setId(appInfoDO.getId()); + }); + modifyAppInfoRequest.setAppName(appName); modifyAppInfoRequest.setTitle(appName); modifyAppInfoRequest.setPassword(password); - modifyAppInfoRequest.setNamespaceId(namespaceId); + modifyAppInfoRequest.setNamespaceCode(namespaceCode); modifyAppInfoRequest.setTags("test_app"); diff --git a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/AppWebService.java b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/AppWebService.java index 5c996a75..cadbbb29 100644 --- a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/AppWebService.java +++ b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/AppWebService.java @@ -5,6 +5,8 @@ import tech.powerjob.server.persistence.remote.model.AppInfoDO; import tech.powerjob.server.web.request.ModifyAppInfoRequest; import tech.powerjob.server.web.request.QueryAppInfoRequest; +import java.util.Optional; + /** * AppWebService * @@ -17,5 +19,7 @@ public interface AppWebService { void delete(Long id); + Optional findByAppName(String appName); + Page list(QueryAppInfoRequest queryAppInfoRequest); } diff --git a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/PwjbUserWebService.java b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/PwjbUserWebService.java index 36e6ea14..890ab874 100644 --- a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/PwjbUserWebService.java +++ b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/PwjbUserWebService.java @@ -4,6 +4,8 @@ import tech.powerjob.server.persistence.remote.model.PwjbUserInfoDO; import tech.powerjob.server.web.request.ChangePasswordRequest; import tech.powerjob.server.web.request.ModifyUserInfoRequest; +import java.util.Optional; + /** * PwjbUserWebService * @@ -14,5 +16,7 @@ public interface PwjbUserWebService { PwjbUserInfoDO save(ModifyUserInfoRequest request); + Optional findByUsername(String username); + void changePassword(ChangePasswordRequest changePasswordRequest); } diff --git a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/impl/AppWebServiceImpl.java b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/impl/AppWebServiceImpl.java index ae951d7f..4dad662e 100644 --- a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/impl/AppWebServiceImpl.java +++ b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/impl/AppWebServiceImpl.java @@ -30,10 +30,7 @@ import tech.powerjob.server.web.service.AppWebService; import tech.powerjob.server.web.service.NamespaceWebService; import javax.persistence.criteria.Predicate; -import java.util.Collections; -import java.util.Date; -import java.util.List; -import java.util.Set; +import java.util.*; /** * AppWebService @@ -114,6 +111,11 @@ public class AppWebServiceImpl implements AppWebService { log.warn("[AppInfoController] delete app[id={}] successfully!", appId); } + @Override + public Optional findByAppName(String appName) { + return appInfoRepository.findByAppName(appName); + } + @Override public Page list(QueryAppInfoRequest queryAppInfoRequest) { Pageable pageable = PageRequest.of(queryAppInfoRequest.getIndex(), queryAppInfoRequest.getPageSize()); diff --git a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/impl/PwjbUserWebServiceImplImpl.java b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/impl/PwjbUserWebServiceImplImpl.java index 20e752d7..f09fdf16 100644 --- a/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/impl/PwjbUserWebServiceImplImpl.java +++ b/powerjob-server/powerjob-server-starter/src/main/java/tech/powerjob/server/web/service/impl/PwjbUserWebServiceImplImpl.java @@ -71,6 +71,11 @@ public class PwjbUserWebServiceImplImpl implements PwjbUserWebService { return pwjbUserInfoRepository.save(pwjbUserInfoDO); } + @Override + public Optional findByUsername(String username) { + return pwjbUserInfoRepository.findByUsername(username); + } + @Override public void changePassword(ChangePasswordRequest changePasswordRequest) { if (!StringUtils.equals(changePasswordRequest.getNewPassword(), changePasswordRequest.getNewPassword2())) {