|
|
@@ -1,11 +1,11 @@
|
|
|
package com.fs.app.task;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fs.common.config.RedisTenantContext;
|
|
|
import com.fs.common.enums.DataSourceType;
|
|
|
import com.fs.common.utils.StringUtils;
|
|
|
import com.fs.config.saas.ProjectConfig;
|
|
|
import com.fs.core.config.TenantConfigContext;
|
|
|
-import com.fs.framework.config.TenantPrincipal;
|
|
|
import com.fs.framework.datasource.DynamicDataSourceContextHolder;
|
|
|
import com.fs.framework.datasource.TenantDataSourceManager;
|
|
|
import com.fs.system.domain.SysConfig;
|
|
|
@@ -14,14 +14,11 @@ import com.fs.tenant.domain.TenantInfo;
|
|
|
import com.fs.tenant.service.TenantInfoService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
-import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.PreDestroy;
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.util.Collections;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
|
import java.util.concurrent.*;
|
|
|
@@ -174,8 +171,14 @@ public class TenantTaskRunner {
|
|
|
private void runForOneTenant(TenantInfo tenant, String taskName, Consumer<TenantInfo> action) {
|
|
|
String dataSourceKey = "tenant:" + tenant.getId();
|
|
|
try {
|
|
|
+ // 切换到租户数据源
|
|
|
tenantDataSourceManager.switchTenant(tenant);
|
|
|
- log.info("[SaaS Task] 定时任务切换数据源 dataSource={}, tenantId={}, tenantCode={}, task={}", dataSourceKey, tenant.getId(), tenant.getTenantCode(), taskName != null ? taskName : "");
|
|
|
+ // 切换Redis租户上下文
|
|
|
+ RedisTenantContext.setTenantId(tenant.getId());
|
|
|
+ log.info("[SaaS Task] 定时任务切换数据源和Redis dataSource={}, tenantId={}, tenantCode={}, task={}",
|
|
|
+ dataSourceKey, tenant.getId(), tenant.getTenantCode(), taskName != null ? taskName : "");
|
|
|
+
|
|
|
+ // 加载租户项目配置
|
|
|
SysConfig cfg = sysConfigMapper.selectConfigByConfigKey("projectConfig");
|
|
|
if (cfg != null && StringUtils.isNotBlank(cfg.getConfigValue())) {
|
|
|
TenantConfigContext.set(JSONObject.parseObject(cfg.getConfigValue()));
|
|
|
@@ -183,22 +186,18 @@ public class TenantTaskRunner {
|
|
|
TenantConfigContext.set(null);
|
|
|
}
|
|
|
ProjectConfig.loadTenantConfigsFromContext();
|
|
|
- action.accept(tenant);
|
|
|
|
|
|
- // 设置租户到 SecurityContext,供 TenantKeyRedisSerializer 自动为 Redis Key 加 tenantid 前缀
|
|
|
- SecurityContextHolder.getContext().setAuthentication(
|
|
|
- new UsernamePasswordAuthenticationToken(
|
|
|
- new TenantPrincipal(tenant.getId()),
|
|
|
- null,
|
|
|
- Collections.emptyList()
|
|
|
- )
|
|
|
- );
|
|
|
+ // 执行租户任务
|
|
|
+ action.accept(tenant);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
- log.error("[SaaS Task] 租户 tenantId={}, tenantCode={} 执行异常", tenant.getId(), tenant.getTenantCode(), e);
|
|
|
+ log.error("[SaaS Task] 租户 tenantId={}, tenantCode={} 执行异常, task={}",
|
|
|
+ tenant.getId(), tenant.getTenantCode(), taskName, e);
|
|
|
} finally {
|
|
|
ProjectConfig.clearTenantConfigs();
|
|
|
TenantConfigContext.clear();
|
|
|
+ // 清理Redis租户上下文
|
|
|
+ RedisTenantContext.clear();
|
|
|
DynamicDataSourceContextHolder.clearDataSourceType();
|
|
|
}
|
|
|
}
|