chore: Add cleanup and update stage for tenant at the server restart (#35786)
## Description PR to add the cleanup and update default tenant at the server restart. This was required because whenever we update tenant via migrations the result gets reverted because the cached tenant is not getting updated as we generally end up updating only the MongoDB state and not the cached entry. /test Sanity ### 🔍 Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!TIP] > 🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉 > Workflow run: <https://github.com/appsmithorg/appsmith/actions/runs/10471754877> > Commit: e288cfe1084bdbf2104a656bf60eb9c24708fe77 > <a href="https://internal.appsmith.com/app/cypress-dashboard/rundetails-65890b3c81d7400d08fa9ee5?branch=master&workflowId=10471754877&attempt=1" target="_blank">Cypress dashboard</a>. > Tags: `@tag.Sanity` > Spec: > <hr>Tue, 20 Aug 2024 13:01:42 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [ ] Yes - [x] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Implemented a mechanism to refresh tenant policies and manage cache cleanup during server restarts, ensuring accurate policy enforcement for multi-tenant applications. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
parent
de8caad999
commit
d1de33b019
|
|
@ -0,0 +1,45 @@
|
|||
package com.appsmith.server.configurations;
|
||||
|
||||
import com.appsmith.server.constants.FieldName;
|
||||
import com.appsmith.server.domains.Tenant;
|
||||
import com.appsmith.server.helpers.CollectionUtils;
|
||||
import com.appsmith.server.repositories.CacheableRepositoryHelper;
|
||||
import com.appsmith.server.repositories.TenantRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.context.event.ApplicationStartedEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static com.appsmith.external.models.BaseDomain.policySetToMap;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class TenantConfig implements ApplicationListener<ApplicationStartedEvent> {
|
||||
|
||||
private final TenantRepository tenantRepository;
|
||||
private final CacheableRepositoryHelper cachableRepositoryHelper;
|
||||
|
||||
// Method to cleanup the cache and update the default tenant policies if the policyMap is empty. This will make sure
|
||||
// cache will be updated if we update the tenant via startup DB migrations.
|
||||
// As we have mocked the TenantService in the tests, we had to manually evict the cache and save the object to DB
|
||||
private Mono<Tenant> cleanupAndUpdateRefreshDefaultTenantPolicies() {
|
||||
log.debug("Cleaning up and updating default tenant policies on server startup");
|
||||
return tenantRepository.findBySlug(FieldName.DEFAULT).flatMap(tenant -> {
|
||||
if (CollectionUtils.isNullOrEmpty(tenant.getPolicyMap())) {
|
||||
tenant.setPolicyMap(policySetToMap(tenant.getPolicies()));
|
||||
return cachableRepositoryHelper
|
||||
.evictCachedTenant(tenant.getId())
|
||||
.then(tenantRepository.save(tenant));
|
||||
}
|
||||
return Mono.just(tenant);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ApplicationStartedEvent event) {
|
||||
cleanupAndUpdateRefreshDefaultTenantPolicies().block();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user