chore: Added checks to only return plugins when new updates are present (#27641)

This commit is contained in:
Nidhi 2023-09-26 18:21:33 +05:30 committed by GitHub
parent f32a034e7c
commit 4a21d99fec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 43 additions and 32 deletions

View File

@ -187,4 +187,6 @@ public class FieldNameCE {
public static final String IS_MERGEABLE = "isMergeable";
public static final String FILE_LOCK_DURATION = "fileLockDuration";
public static final String REMOTE_PLUGINS = "remotePlugins";
}

View File

@ -37,8 +37,10 @@ public class PluginScheduledTaskUtilsCEImpl implements PluginScheduledTaskUtilsC
return Mono.empty();
}
String lastUpdatedAtParam = lastUpdatedAt != null ? "&lastUpdatedAt=" + lastUpdatedAt : "";
return configService.getInstanceId().flatMap(instanceId -> WebClientUtils.create(
baseUrl + "/api/v1/plugins?instanceId=" + instanceId + "&lastUpdatedAt=" + lastUpdatedAt)
baseUrl + "/api/v1/plugins?instanceId=" + instanceId + lastUpdatedAtParam)
.get()
.exchangeToMono(clientResponse ->
clientResponse.bodyToMono(new ParameterizedTypeReference<ResponseDTO<List<Plugin>>>() {}))

View File

@ -1,8 +1,6 @@
package com.appsmith.server.services;
import com.appsmith.server.repositories.ApplicationRepository;
import com.appsmith.server.repositories.ConfigRepository;
import com.appsmith.server.repositories.DatasourceRepository;
import com.appsmith.server.services.ce.ConfigServiceCEImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@ -11,11 +9,7 @@ import org.springframework.stereotype.Service;
@Service
public class ConfigServiceImpl extends ConfigServiceCEImpl implements ConfigService {
public ConfigServiceImpl(
ConfigRepository repository,
ApplicationRepository applicationRepository,
DatasourceRepository datasourceRepository) {
super(repository, applicationRepository, datasourceRepository);
public ConfigServiceImpl(ConfigRepository repository) {
super(repository);
}
}

View File

@ -6,9 +6,7 @@ import com.appsmith.server.domains.Config;
import com.appsmith.server.domains.User;
import com.appsmith.server.exceptions.AppsmithError;
import com.appsmith.server.exceptions.AppsmithException;
import com.appsmith.server.repositories.ApplicationRepository;
import com.appsmith.server.repositories.ConfigRepository;
import com.appsmith.server.repositories.DatasourceRepository;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import reactor.core.publisher.Mono;
@ -17,23 +15,12 @@ import java.util.Map;
@Slf4j
public class ConfigServiceCEImpl implements ConfigServiceCE {
private static final String TEMPLATE_WORKSPACE_CONFIG_NAME = "template-workspace";
private final ApplicationRepository applicationRepository;
private final DatasourceRepository datasourceRepository;
private final ConfigRepository repository;
// This is permanently cached through the life of the JVM process as this is not intended to change at runtime ever.
private String instanceId = null;
public ConfigServiceCEImpl(
ConfigRepository repository,
ApplicationRepository applicationRepository,
DatasourceRepository datasourceRepository) {
this.applicationRepository = applicationRepository;
this.datasourceRepository = datasourceRepository;
public ConfigServiceCEImpl(ConfigRepository repository) {
this.repository = repository;
}

View File

@ -196,6 +196,11 @@ public class PluginServiceCEImpl extends BaseService<PluginRepository, Plugin, S
return new WorkspacePlugin(plugin.getId(), WorkspacePluginStatus.ACTIVATED);
})
.collect(Collectors.toList());
if (newWorkspacePlugins.isEmpty()) {
return Flux.empty();
}
return workspaceService.getAll().flatMap(workspace -> {
// Only perform a DB op if plugins associated to this org have changed
if (workspace.getPlugins().containsAll(newWorkspacePlugins)) {

View File

@ -1,6 +1,7 @@
package com.appsmith.server.solutions;
import com.appsmith.server.helpers.PluginScheduledTaskUtils;
import com.appsmith.server.services.ConfigService;
import com.appsmith.server.solutions.ce.PluginScheduledTaskCEImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@ -8,9 +9,7 @@ import org.springframework.stereotype.Component;
@Slf4j
@Component
public class PluginScheduledTaskImpl extends PluginScheduledTaskCEImpl implements PluginScheduledTask {
public PluginScheduledTaskImpl(PluginScheduledTaskUtils pluginScheduledTaskUtils) {
super(pluginScheduledTaskUtils);
public PluginScheduledTaskImpl(PluginScheduledTaskUtils pluginScheduledTaskUtils, ConfigService configService) {
super(pluginScheduledTaskUtils, configService);
}
}

View File

@ -1,15 +1,21 @@
package com.appsmith.server.solutions.ce;
import com.appsmith.server.constants.FieldName;
import com.appsmith.server.domains.Config;
import com.appsmith.server.helpers.PluginScheduledTaskUtils;
import com.appsmith.server.services.ConfigService;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONObject;
import org.springframework.scheduling.annotation.Scheduled;
import reactor.core.scheduler.Schedulers;
import java.time.Instant;
import java.util.Date;
import java.util.Map;
/**
* This class represents a scheduled task that pings cloud services for any updates in available plugins.
@ -19,18 +25,34 @@ import java.time.Instant;
public class PluginScheduledTaskCEImpl implements PluginScheduledTaskCE {
private final PluginScheduledTaskUtils pluginScheduledTaskUtils;
private Instant lastUpdatedAt = null;
private final ConfigService configService;
// Number of milliseconds between the start of each scheduled calls to this method.
@Scheduled(initialDelay = 30 * 1000 /* 30 seconds */, fixedRate = 2 * 60 * 60 * 1000 /* two hours */)
public void updateRemotePlugins() {
// Moving the fetch and update remote plugins to helper classes to have custom implementation for business
// edition
pluginScheduledTaskUtils
.fetchAndUpdateRemotePlugins(lastUpdatedAt)
configService
.getByName(FieldName.REMOTE_PLUGINS)
.onErrorReturn(new Config())
.map(config -> {
JSONObject config1 = config.getConfig();
Instant lastUpdatedAt = null;
if (config1 != null) {
Object tempUpdatedAt = config1.getOrDefault(FieldName.UPDATED_AT, null);
if (tempUpdatedAt != null) {
lastUpdatedAt = ((Date) tempUpdatedAt).toInstant();
}
}
return pluginScheduledTaskUtils.fetchAndUpdateRemotePlugins(lastUpdatedAt);
})
// Set new updated time
.doOnSuccess(success -> this.lastUpdatedAt = Instant.now())
.flatMap(success -> {
Config config = new Config(
new JSONObject(Map.of(FieldName.UPDATED_AT, Instant.now())), FieldName.REMOTE_PLUGINS);
return configService.save(config);
})
.subscribeOn(Schedulers.single())
.subscribe();
}