Skip to content

Commit

Permalink
chore: Add cleanup and update stage for tenant at the server restart (#…
Browse files Browse the repository at this point in the history
…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: e288cfe
> <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 -->

(cherry picked from commit d1de33b)
  • Loading branch information
abhvsn committed Aug 20, 2024
1 parent c5c466e commit 0500bb2
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 0500bb2

Please sign in to comment.