diff --git a/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/user/UserMgrApiClient.java b/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/user/UserMgrApiClient.java index 19499be576..9363644cf5 100644 --- a/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/user/UserMgrApiClient.java +++ b/src/backend/commons/paas-sdk/src/main/java/com/tencent/bk/job/common/paas/user/UserMgrApiClient.java @@ -31,17 +31,12 @@ import com.tencent.bk.job.common.constant.TenantIdConstants; import com.tencent.bk.job.common.esb.config.AppProperties; import com.tencent.bk.job.common.esb.config.BkApiGatewayProperties; -import com.tencent.bk.job.common.esb.metrics.EsbMetricTags; import com.tencent.bk.job.common.esb.model.BkApiAuthorization; -import com.tencent.bk.job.common.esb.model.EsbResp; import com.tencent.bk.job.common.esb.model.OpenApiRequestInfo; import com.tencent.bk.job.common.esb.model.OpenApiResponse; import com.tencent.bk.job.common.esb.sdk.BkApiV2Client; import com.tencent.bk.job.common.exception.InternalException; -import com.tencent.bk.job.common.exception.InternalUserManageException; import com.tencent.bk.job.common.model.dto.BkUserDTO; -import com.tencent.bk.job.common.paas.model.EsbListUsersResult; -import com.tencent.bk.job.common.paas.model.GetUserListReq; import com.tencent.bk.job.common.paas.model.OpenApiTenant; import com.tencent.bk.job.common.tenant.TenantEnvService; import com.tencent.bk.job.common.util.http.HttpHelperFactory; @@ -50,11 +45,8 @@ import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Tag; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.apache.http.message.BasicHeader; -import org.springframework.util.CollectionUtils; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.function.Function; @@ -68,12 +60,8 @@ @Slf4j public class UserMgrApiClient extends BkApiV2Client { - private static final String API_GET_USER_LIST = "/api/c/compapi/v2/usermanage/list_users/"; - private final BkApiAuthorization authorization; - private final TenantEnvService tenantEnvService; - public UserMgrApiClient(BkApiGatewayProperties bkApiGatewayProperties, AppProperties appProperties, MeterRegistry meterRegistry, @@ -86,69 +74,12 @@ public UserMgrApiClient(BkApiGatewayProperties bkApiGatewayProperties, ); this.authorization = BkApiAuthorization.appAuthorization(appProperties.getCode(), appProperties.getSecret()); - this.tenantEnvService = tenantEnvService; } public List getAllUserList(String tenantId) { - String fields = "id,username,display_name,logo"; - List esbUserList; - try { - GetUserListReq req = buildGetUserListReq(fields); - - HttpMetricUtil.setHttpMetricName(USER_MANAGE_API_HTTP); - HttpMetricUtil.addTagForCurrentMetric( - Tag.of(EsbMetricTags.KEY_API_NAME, API_GET_USER_LIST) - ); - EsbResp> esbResp = doRequest( - OpenApiRequestInfo.builder() - .method(HttpMethodEnum.GET) - .uri(API_GET_USER_LIST) - .queryParams(req.toUrlParams()) -// .addHeader(new BasicHeader(JobCommonHeaders.BK_TENANT_ID, tenantId)) - .authorization(authorization) - .build(), - new TypeReference>>() { - } - ); - esbUserList = esbResp.getData(); - } catch (Exception e) { - String errorMsg = "Get " + API_GET_USER_LIST + " error"; - log.error(errorMsg, e); - throw new InternalUserManageException(errorMsg, e, ErrorCode.BK_USER_MANAGE_API_ERROR); - } finally { - HttpMetricUtil.clearHttpMetric(); - } - return convert(esbUserList); + return Collections.emptyList(); } - private GetUserListReq buildGetUserListReq(String fields) { - GetUserListReq req = new GetUserListReq(); - if (StringUtils.isNotBlank(fields)) { - req.setFields(fields); - } - req.setPage(0L); - req.setPageSize(0L); - req.setNoPage(true); - return req; - } - - private List convert(List esbUserList) { - if (CollectionUtils.isEmpty(esbUserList)) { - return Collections.emptyList(); - } - List userList = new ArrayList<>(); - for (EsbListUsersResult esbUser : esbUserList) { - BkUserDTO user = new BkUserDTO(); - user.setId(esbUser.getId()); - user.setUsername(esbUser.getUsername()); - user.setDisplayName(esbUser.getDisplayName()); - user.setLogo(esbUser.getLogo()); - userList.add(user); - } - return userList; - } - - /** * 获取全量租户 */ diff --git a/src/backend/job-analysis/boot-job-analysis/src/main/java/com/tencent/bk/job/analysis/config/JobAnalysisConfiguration.java b/src/backend/job-analysis/boot-job-analysis/src/main/java/com/tencent/bk/job/analysis/config/JobAnalysisConfiguration.java index 1edc39ff28..25e5e4c9cb 100644 --- a/src/backend/job-analysis/boot-job-analysis/src/main/java/com/tencent/bk/job/analysis/config/JobAnalysisConfiguration.java +++ b/src/backend/job-analysis/boot-job-analysis/src/main/java/com/tencent/bk/job/analysis/config/JobAnalysisConfiguration.java @@ -39,7 +39,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes } @Bean - public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) { + public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) { return new BasicAppInterceptor(appCacheService); } } diff --git a/src/backend/job-assemble/src/main/java/com/tencent/bk/job/assemble/config/JobAssembleConfiguration.java b/src/backend/job-assemble/src/main/java/com/tencent/bk/job/assemble/config/JobAssembleConfiguration.java index b71fc51f09..83d1e8c83e 100644 --- a/src/backend/job-assemble/src/main/java/com/tencent/bk/job/assemble/config/JobAssembleConfiguration.java +++ b/src/backend/job-assemble/src/main/java/com/tencent/bk/job/assemble/config/JobAssembleConfiguration.java @@ -33,7 +33,7 @@ @Configuration public class JobAssembleConfiguration { @Bean - public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) { + public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) { return new BasicAppInterceptor(appCacheService); } } diff --git a/src/backend/job-backup/boot-job-backup/src/main/java/com/tencent/bk/job/backup/config/JobBackupConfiguration.java b/src/backend/job-backup/boot-job-backup/src/main/java/com/tencent/bk/job/backup/config/JobBackupConfiguration.java index a6e3740f2e..96863b086d 100644 --- a/src/backend/job-backup/boot-job-backup/src/main/java/com/tencent/bk/job/backup/config/JobBackupConfiguration.java +++ b/src/backend/job-backup/boot-job-backup/src/main/java/com/tencent/bk/job/backup/config/JobBackupConfiguration.java @@ -39,7 +39,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes } @Bean - public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) { + public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) { return new BasicAppInterceptor(appCacheService); } } diff --git a/src/backend/job-crontab/boot-job-crontab/src/main/java/com/tencent/bk/job/crontab/config/JobCrontabConfiguration.java b/src/backend/job-crontab/boot-job-crontab/src/main/java/com/tencent/bk/job/crontab/config/JobCrontabConfiguration.java index fafaaf2b12..be5b200f83 100644 --- a/src/backend/job-crontab/boot-job-crontab/src/main/java/com/tencent/bk/job/crontab/config/JobCrontabConfiguration.java +++ b/src/backend/job-crontab/boot-job-crontab/src/main/java/com/tencent/bk/job/crontab/config/JobCrontabConfiguration.java @@ -40,7 +40,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes } @Bean - public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) { + public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) { return new BasicAppInterceptor(appCacheService); } } diff --git a/src/backend/job-execute/boot-job-execute/src/main/java/com/tencent/bk/job/execute/config/JobExecuteConfiguration.java b/src/backend/job-execute/boot-job-execute/src/main/java/com/tencent/bk/job/execute/config/JobExecuteConfiguration.java index b1f92d2197..5ba65ba11d 100644 --- a/src/backend/job-execute/boot-job-execute/src/main/java/com/tencent/bk/job/execute/config/JobExecuteConfiguration.java +++ b/src/backend/job-execute/boot-job-execute/src/main/java/com/tencent/bk/job/execute/config/JobExecuteConfiguration.java @@ -40,7 +40,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes } @Bean - public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) { + public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) { return new BasicAppInterceptor(appCacheService); } } diff --git a/src/backend/job-file-gateway/boot-job-file-gateway/src/main/java/com/tencent/bk/job/file_gateway/config/JobFileGatewayConfiguration.java b/src/backend/job-file-gateway/boot-job-file-gateway/src/main/java/com/tencent/bk/job/file_gateway/config/JobFileGatewayConfiguration.java index 318c49e3b6..31bbbffec8 100644 --- a/src/backend/job-file-gateway/boot-job-file-gateway/src/main/java/com/tencent/bk/job/file_gateway/config/JobFileGatewayConfiguration.java +++ b/src/backend/job-file-gateway/boot-job-file-gateway/src/main/java/com/tencent/bk/job/file_gateway/config/JobFileGatewayConfiguration.java @@ -40,7 +40,7 @@ AppCacheService appScopeMappingService(ServiceApplicationResource applicationRes } @Bean - public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) { + public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) { return new BasicAppInterceptor(appCacheService); } } diff --git a/src/backend/job-manage/boot-job-manage/src/main/java/com/tencent/bk/job/manage/config/JobManageConfiguration.java b/src/backend/job-manage/boot-job-manage/src/main/java/com/tencent/bk/job/manage/config/JobManageConfiguration.java index a9e1f2ea75..998ddd4ea7 100644 --- a/src/backend/job-manage/boot-job-manage/src/main/java/com/tencent/bk/job/manage/config/JobManageConfiguration.java +++ b/src/backend/job-manage/boot-job-manage/src/main/java/com/tencent/bk/job/manage/config/JobManageConfiguration.java @@ -25,7 +25,6 @@ package com.tencent.bk.job.manage.config; import com.tencent.bk.job.common.service.AppCacheService; -import com.tencent.bk.job.common.service.AppScopeMappingService; import com.tencent.bk.job.common.web.interceptor.BasicAppInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,7 +33,7 @@ public class JobManageConfiguration { @Bean - public BasicAppInterceptor appResourceScopeInterceptor(AppCacheService appCacheService) { + public BasicAppInterceptor basicAppInterceptor(AppCacheService appCacheService) { return new BasicAppInterceptor(appCacheService); } } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/UserDAOImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/UserDAOImpl.java index 2b9d1b0a38..49498b6782 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/UserDAOImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/dao/impl/UserDAOImpl.java @@ -39,7 +39,8 @@ public UserDAOImpl(@Qualifier("job-manage-dsl-context") DSLContext dslContext) { @Override public void saveUser(BkUserDTO user) { dslContext - .insertInto(T_USER, + .insertInto( + T_USER, T_USER.USERNAME, T_USER.DISPLAY_NAME, T_USER.TENANT_ID, @@ -62,7 +63,8 @@ public int deleteUser(String username) { @Override public List listTenantUsers(String tenantId) { - Result result = dslContext.select(ALL_FIELDS) + Result result = dslContext + .select(ALL_FIELDS) .from(T_USER) .where(T_USER.TENANT_ID.eq(tenantId)) .fetch(); @@ -82,7 +84,8 @@ private BkUserDTO extract(Record record) { @Override public List listUsersByDisplayNamePrefix(String tenantId, String prefixStr, Long limit) { - Result result = dslContext.select(ALL_FIELDS) + Result result = dslContext + .select(ALL_FIELDS) .from(T_USER) .where(T_USER.TENANT_ID.eq(tenantId)) .and(T_USER.DISPLAY_NAME.startsWith(prefixStr)) @@ -95,7 +98,8 @@ public List listUsersByDisplayNamePrefix(String tenantId, String pref @Override public List listUsersByUsernames(String tenantId, Collection usernames) { - Result result = dslContext.select(ALL_FIELDS) + Result result = dslContext + .select(ALL_FIELDS) .from(T_USER) .where(T_USER.TENANT_ID.eq(tenantId)) .and(T_USER.USERNAME.in(usernames)) @@ -108,7 +112,8 @@ public List listUsersByUsernames(String tenantId, Collection @Override public List listExistUserName(String tenantId, Collection usernames) { - Result result = dslContext.select(ALL_FIELDS) + Result result = dslContext + .select(ALL_FIELDS) .from(T_USER) .where(T_USER.TENANT_ID.eq(tenantId)) .and(T_USER.USERNAME.in(usernames)) diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/UserService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/UserCacheService.java similarity index 91% rename from src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/UserService.java rename to src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/UserCacheService.java index 2f4c02674e..223a4549be 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/UserService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/UserCacheService.java @@ -7,9 +7,9 @@ import java.util.Set; /** - * 用户服务 + * 用户信息缓存服务 */ -public interface UserService { +public interface UserCacheService { void saveUser(BkUserDTO user); int deleteUser(String username); diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/UserServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/UserCacheServiceImpl.java similarity index 90% rename from src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/UserServiceImpl.java rename to src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/UserCacheServiceImpl.java index d9966d828e..7a13f3d7e8 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/UserServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/UserCacheServiceImpl.java @@ -3,7 +3,7 @@ import com.tencent.bk.job.common.model.dto.BkUserDTO; import com.tencent.bk.job.common.mysql.JobTransactional; import com.tencent.bk.job.manage.dao.UserDAO; -import com.tencent.bk.job.manage.service.UserService; +import com.tencent.bk.job.manage.service.UserCacheService; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -13,11 +13,11 @@ @Slf4j @Service -public class UserServiceImpl implements UserService { +public class UserCacheServiceImpl implements UserCacheService { private final UserDAO userDAO; - public UserServiceImpl(UserDAO userDAO) { + public UserCacheServiceImpl(UserDAO userDAO) { this.userDAO = userDAO; } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/NotifySendService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/NotifySendService.java index 16dfb55d23..3f64818cff 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/NotifySendService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/NotifySendService.java @@ -25,7 +25,7 @@ package com.tencent.bk.job.manage.service.impl.notify; import com.tencent.bk.job.manage.metrics.MetricsConstants; -import com.tencent.bk.job.manage.service.UserService; +import com.tencent.bk.job.manage.service.UserCacheService; import com.tencent.bk.job.manage.service.impl.WatchableSendMsgService; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Tag; @@ -47,15 +47,15 @@ public class NotifySendService { //发通知专用线程池 private final ThreadPoolExecutor notifySendExecutor; private final WatchableSendMsgService watchableSendMsgService; - private final UserService userService; + private final UserCacheService userCacheService; @Autowired public NotifySendService(WatchableSendMsgService watchableSendMsgService, - UserService userService, + UserCacheService userCacheService, @Qualifier("notifySendExecutor") ThreadPoolExecutor notifySendExecutor, MeterRegistry meterRegistry) { this.watchableSendMsgService = watchableSendMsgService; - this.userService = userService; + this.userCacheService = userCacheService; this.notifySendExecutor = notifySendExecutor; measureNotifySendExecutor(meterRegistry); } @@ -90,7 +90,7 @@ private SendNotifyTask buildSendTask(Long appId, .title(title) .content(content) .build(); - task.bindService(watchableSendMsgService, userService); + task.bindService(watchableSendMsgService, userCacheService); return task; } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/NotifyUserService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/NotifyUserService.java index e8856d3419..ebe9411745 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/NotifyUserService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/NotifyUserService.java @@ -31,7 +31,7 @@ import com.tencent.bk.job.manage.model.web.request.notify.NotifyBlackUsersReq; import com.tencent.bk.job.manage.model.web.vo.notify.NotifyBlackUserInfoVO; import com.tencent.bk.job.manage.model.web.vo.notify.UserVO; -import com.tencent.bk.job.manage.service.UserService; +import com.tencent.bk.job.manage.service.UserCacheService; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.apache.commons.lang3.StringUtils; @@ -49,13 +49,13 @@ public class NotifyUserService { private final NotifyBlackUserInfoDAO notifyBlackUserInfoDAO; - private final UserService userService; + private final UserCacheService userCacheService; @Autowired public NotifyUserService(NotifyBlackUserInfoDAO notifyBlackUserInfoDAO, - UserService userService) { + UserCacheService userCacheService) { this.notifyBlackUserInfoDAO = notifyBlackUserInfoDAO; - this.userService = userService; + this.userCacheService = userCacheService; } public List listNotifyBlackUsers(Integer start, Integer pageSize) { @@ -149,9 +149,9 @@ private List searchUserByPrefix(String prefixStr) { while (userNames.contains("")) { userNames.remove(""); } - return userService.listUsersByUsernames(null, userNames); + return userCacheService.listUsersByUsernames(null, userNames); } else { - return userService.listUsersByDisplayNamePrefix(null, prefixStr, -1L); + return userCacheService.listUsersByDisplayNamePrefix(null, prefixStr, -1L); } } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/SendNotifyTask.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/SendNotifyTask.java index b1e00cffa4..a7a1b95b3a 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/SendNotifyTask.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/notify/SendNotifyTask.java @@ -25,7 +25,7 @@ package com.tencent.bk.job.manage.service.impl.notify; import com.tencent.bk.job.common.util.ThreadUtils; -import com.tencent.bk.job.manage.service.UserService; +import com.tencent.bk.job.manage.service.UserCacheService; import com.tencent.bk.job.manage.service.impl.WatchableSendMsgService; import lombok.Builder; import lombok.extern.slf4j.Slf4j; @@ -45,7 +45,7 @@ public class SendNotifyTask implements Runnable { private final int NOTIFY_MAX_RETRY_COUNT = 1; private WatchableSendMsgService watchableSendMsgService; - private UserService userService; + private UserCacheService userCacheService; private final Long appId; private final long createTimeMillis; @@ -58,9 +58,9 @@ public class SendNotifyTask implements Runnable { private Set validReceivers; public void bindService(WatchableSendMsgService watchableSendMsgService, - UserService userService) { + UserCacheService userCacheService) { this.watchableSendMsgService = watchableSendMsgService; - this.userService = userService; + this.userCacheService = userCacheService; } @Override @@ -88,7 +88,7 @@ public void run() { private void pickValidReceivers() { // TODO 需要加入租户 ID - List existUserNameList = userService.listExistUserName(null, receivers); + List existUserNameList = userCacheService.listExistUserName(null, receivers); validReceivers = new HashSet<>(existUserNameList); } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/task/UserSyncService.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/task/UserSyncService.java index 24282dc813..703d956f43 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/task/UserSyncService.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/task/UserSyncService.java @@ -30,18 +30,16 @@ import com.tencent.bk.job.common.redis.util.LockUtils; import com.tencent.bk.job.common.redis.util.RedisKeyHeartBeatThread; import com.tencent.bk.job.common.util.ip.IpUtils; -import com.tencent.bk.job.manage.service.UserService; +import com.tencent.bk.job.manage.service.UserCacheService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import org.springframework.util.StopWatch; -import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -69,14 +67,14 @@ public class UserSyncService { private final String REDIS_KEY_SYNC_USER_JOB_RUNNING_MACHINE = "sync-user-job-running-machine"; private final RedisTemplate redisTemplate; private final UserMgrApiClient userMgrApiClient; - private final UserService userService; + private final UserCacheService userCacheService; @Autowired public UserSyncService(UserMgrApiClient userMgrApiClient, - UserService userService, + UserCacheService userCacheService, RedisTemplate redisTemplate) { this.userMgrApiClient = userMgrApiClient; - this.userService = userService; + this.userCacheService = userCacheService; this.redisTemplate = redisTemplate; } @@ -105,6 +103,7 @@ public boolean execute() { userSyncRedisKeyHeartBeatThread.setName("userSyncRedisKeyHeartBeatThread"); userSyncRedisKeyHeartBeatThread.start(); log.info("Begin sync all tenant users"); + boolean isAllSuccess = true; try { List allTenants = userMgrApiClient.listAllTenant(); if (CollectionUtils.isEmpty(allTenants)) { @@ -114,35 +113,30 @@ public boolean execute() { allTenants.stream().map(OpenApiTenant::getId).collect(Collectors.toList())); } for (OpenApiTenant tenant : allTenants) { - syncUsersByTenant(tenant.getId()); + boolean isSuccess = syncUsersByTenant(tenant.getId()); + isAllSuccess = isAllSuccess && isSuccess; } } catch (Throwable t) { log.error("FATAL: syncUser thread fail", t); } finally { userSyncRedisKeyHeartBeatThread.setRunFlag(false); + log.info("Sync all tenant users done, result: {}", isAllSuccess); } return true; } - private void syncUsersByTenant(String tenantId) { + private boolean syncUsersByTenant(String tenantId) { log.info("Sync user by tenant : {}", tenantId); - - StopWatch watch = new StopWatch("syncUserByTenant"); - watch.start("total"); + boolean isSuccess = true; try { - // 1.接口数据拉取 + // 1.获取租户下的所有用户列表 List remoteUserList = userMgrApiClient.getAllUserList(tenantId); - if (remoteUserList.isEmpty()) { - log.warn("[{}] Fail to fetch remote userInfo, return", tenantId); - return; - } - // 2.组装 - Set remoteUserSet = new HashSet<>(remoteUserList); + Set remoteUserSet = CollectionUtils.isEmpty(remoteUserList) ? + Collections.emptySet(): new HashSet<>(remoteUserList); - // 3.计算差异数据 - Set localUserSet = new HashSet<>(userService.listTenantUsers(tenantId)); - remoteUserSet.removeAll(localUserSet); + // 2.计算差异数据 + Set localUserSet = new HashSet<>(userCacheService.listTenantUsers(tenantId)); Set addUsers = remoteUserSet.stream() .filter(user -> !localUserSet.contains(user)).collect(Collectors.toSet()); log.info("[{}] New users : {}", @@ -154,15 +148,12 @@ private void syncUsersByTenant(String tenantId) { tenantId, deleteUsers.stream().map(BkUserDTO::getFullName).collect(Collectors.joining(","))); - // 4.入库 - userService.batchPatchUsers(deleteUsers, addUsers); + // 3.保存 + userCacheService.batchPatchUsers(deleteUsers, addUsers); } catch (Throwable t) { log.error("Sync user fail", t); - } finally { - if (watch.isRunning()) { - watch.stop(); - } - log.info("[{}] Sync user time consuming: {}", tenantId, watch); + isSuccess = false; } + return isSuccess; } }