Compare commits

...

1 Commits

Author SHA1 Message Date
MentatBot
6cbcf13447 Fix: Skip execution of scheduled tasks for disabled tenants
This update addresses the issue where scheduled tasks were being executed for disabled tenants. The `TenantJobAspect` class has been modified to check the status of each tenant before executing the task. If a tenant is disabled, the task will be skipped for that tenant.

Changes:
- Added a check in `TenantJobAspect` to verify tenant status before task execution.
- Introduced a new test `testTenantJobAspect_skipDisabledTenants` in `TenantServiceImplTest` to ensure the aspect correctly skips disabled tenants.
2024-08-16 07:24:48 +00:00
2 changed files with 33 additions and 2 deletions

View File

@@ -5,6 +5,8 @@ import cn.hutool.core.exceptions.ExceptionUtil;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO;
import cn.iocoder.yudao.module.system.service.tenant.TenantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
@@ -29,6 +31,7 @@ import java.util.concurrent.ConcurrentHashMap;
public class TenantJobAspect {
private final TenantFrameworkService tenantFrameworkService;
private final TenantService tenantService;
@Around("@annotation(tenantJob)")
public String around(ProceedingJoinPoint joinPoint, TenantJob tenantJob) {
@@ -44,6 +47,10 @@ public class TenantJobAspect {
// TODO 芋艿:先通过 parallel 实现并行1多个租户是一条执行日志2异常的情况
TenantUtils.execute(tenantId, () -> {
try {
TenantDO tenant = tenantService.getTenant(tenantId);
if (tenant.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) {
return; // Skip disabled tenants
}
joinPoint.proceed();
} catch (Throwable e) {
results.put(tenantId, ExceptionUtil.getRootCauseMessage(e));
@@ -53,4 +60,4 @@ public class TenantJobAspect {
return JsonUtils.toJsonString(results);
}
}
}

View File

@@ -403,13 +403,37 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
}));
}
@Test
public void testTenantJobAspect_skipDisabledTenants() {
// mock 租户
TenantDO enabledTenant = randomPojo(TenantDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
TenantDO disabledTenant = randomPojo(TenantDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
tenantMapper.insert(enabledTenant);
tenantMapper.insert(disabledTenant);
// mock tenantService
when(tenantService.getTenant(enabledTenant.getId())).thenReturn(enabledTenant);
when(tenantService.getTenant(disabledTenant.getId())).thenReturn(disabledTenant);
// mock tenantFrameworkService
when(tenantFrameworkService.getTenantIds()).thenReturn(Arrays.asList(enabledTenant.getId(), disabledTenant.getId()));
// mock joinPoint
ProceedingJoinPoint joinPoint = mock(ProceedingJoinPoint.class);
// 调用
TenantJobAspect aspect = new TenantJobAspect(tenantFrameworkService, tenantService);
aspect.around(joinPoint, mock(TenantJob.class));
// 断言
verify(joinPoint, times(1)).proceed();
}
@Test
public void testHandleTenantMenu_disable() {
// 准备参数
TenantMenuHandler handler = mock(TenantMenuHandler.class);
// mock 禁用
when(tenantProperties.getEnable()).thenReturn(false);
// 调用
tenantService.handleTenantMenu(handler);
// 断言