From af59f70d9681325548f4f78ddb77d5b17d27e17b Mon Sep 17 00:00:00 2001 From: Anne Marsan Date: Wed, 13 Nov 2024 09:22:18 -0500 Subject: [PATCH] Don't do jobs. --- .../cdmresults/AchillesClearCacheTasklet.java | 24 ------- .../CDMResultsClearCacheTasklet.java | 24 ------- .../webapi/service/CDMResultsService.java | 70 ++++--------------- 3 files changed, 12 insertions(+), 106 deletions(-) delete mode 100644 src/main/java/org/ohdsi/webapi/cdmresults/AchillesClearCacheTasklet.java delete mode 100644 src/main/java/org/ohdsi/webapi/cdmresults/CDMResultsClearCacheTasklet.java diff --git a/src/main/java/org/ohdsi/webapi/cdmresults/AchillesClearCacheTasklet.java b/src/main/java/org/ohdsi/webapi/cdmresults/AchillesClearCacheTasklet.java deleted file mode 100644 index 364f97242..000000000 --- a/src/main/java/org/ohdsi/webapi/cdmresults/AchillesClearCacheTasklet.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.ohdsi.webapi.cdmresults; - -import org.ohdsi.webapi.achilles.service.AchillesCacheService; -import org.ohdsi.webapi.source.Source; -import org.springframework.batch.core.StepContribution; -import org.springframework.batch.core.scope.context.ChunkContext; -import org.springframework.batch.core.step.tasklet.Tasklet; -import org.springframework.batch.repeat.RepeatStatus; - -public class AchillesClearCacheTasklet implements Tasklet { - private final Source source; - private final AchillesCacheService cacheService; - - public AchillesClearCacheTasklet(Source source, AchillesCacheService cacheService) { - this.source = source; - this.cacheService = cacheService; - } - - @Override - public RepeatStatus execute(final StepContribution contribution, final ChunkContext chunkContext) { - cacheService.clearCache(this.source); - return RepeatStatus.FINISHED; - } -} diff --git a/src/main/java/org/ohdsi/webapi/cdmresults/CDMResultsClearCacheTasklet.java b/src/main/java/org/ohdsi/webapi/cdmresults/CDMResultsClearCacheTasklet.java deleted file mode 100644 index 2d2d9b2fa..000000000 --- a/src/main/java/org/ohdsi/webapi/cdmresults/CDMResultsClearCacheTasklet.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.ohdsi.webapi.cdmresults; - -import org.ohdsi.webapi.cdmresults.service.CDMCacheService; -import org.ohdsi.webapi.source.Source; -import org.springframework.batch.core.StepContribution; -import org.springframework.batch.core.scope.context.ChunkContext; -import org.springframework.batch.core.step.tasklet.Tasklet; -import org.springframework.batch.repeat.RepeatStatus; - -public class CDMResultsClearCacheTasklet implements Tasklet { - private final Source source; - private final CDMCacheService cdmCacheService; - - public CDMResultsClearCacheTasklet(Source source, CDMCacheService cdmCacheService) { - this.source = source; - this.cdmCacheService = cdmCacheService; - } - - @Override - public RepeatStatus execute(final StepContribution contribution, final ChunkContext chunkContext) { - cdmCacheService.clearCache(this.source); - return RepeatStatus.FINISHED; - } -} \ No newline at end of file diff --git a/src/main/java/org/ohdsi/webapi/service/CDMResultsService.java b/src/main/java/org/ohdsi/webapi/service/CDMResultsService.java index 65e65b96b..483299df5 100644 --- a/src/main/java/org/ohdsi/webapi/service/CDMResultsService.java +++ b/src/main/java/org/ohdsi/webapi/service/CDMResultsService.java @@ -8,9 +8,7 @@ import org.ohdsi.webapi.achilles.aspect.AchillesCache; import org.ohdsi.webapi.achilles.service.AchillesCacheService; import org.ohdsi.webapi.cdmresults.AchillesCacheTasklet; -import org.ohdsi.webapi.cdmresults.AchillesClearCacheTasklet; import org.ohdsi.webapi.cdmresults.CDMResultsCacheTasklet; -import org.ohdsi.webapi.cdmresults.CDMResultsClearCacheTasklet; import org.ohdsi.webapi.cdmresults.DescendantRecordAndPersonCount; import org.ohdsi.webapi.cdmresults.DescendantRecordCount; import org.ohdsi.webapi.cdmresults.domain.CDMCacheEntity; @@ -48,6 +46,7 @@ import org.springframework.stereotype.Component; import javax.ws.rs.Consumes; +import javax.ws.rs.ForbiddenException; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; @@ -289,35 +288,17 @@ public JobExecutionResource refreshCache(@PathParam("sourceKey") final String so * Clear the cdm_cache and achilles_cache for all sources * * @summary Clear the cdm_cache and achilles_cache for all sources - * @return List of JobExecutionResources + * @return void + * @throws ForbiddenException if the user is not an admin */ @GET @Path("clearCache") - @Produces(MediaType.APPLICATION_JSON) - public List clearCache() { - List jobs = new ArrayList<>(); - + public void clearCache() { if (!isSecured() || !isAdmin()) { - return jobs; + throw new ForbiddenException(); } - List sources = getSourceRepository().findAll(); - for (Source source : sources) { - if (!sourceAccessor.hasAccess(source)) { - continue; - } - JobExecutionResource jobExecutionResource = jobService.findJobByName(Constants.CLEAR_CACHE, - getClearCacheJobName(String.valueOf(source.getSourceId()), source.getSourceKey())); - if (jobExecutionResource == null) { - if (source.getDaimons().stream() - .anyMatch(sd -> Objects.equals(sd.getDaimonType(), SourceDaimon.DaimonType.Results))) { - jobs.add(clearCache(source)); - } - } else { - jobs.add(jobExecutionResource); - } - } - return jobs; + sources.parallelStream().forEach(this::clearCache); } /** @@ -541,12 +522,12 @@ private void warmCaches(Collection sources) { /* * Clear cache for a single source */ - private JobExecutionResource clearCache(Source source) { - String jobName = getClearCacheJobName(String.valueOf(source.getSourceId()), source.getSourceKey()); - List jobSteps = createClearCacheJobSteps(source, jobName); - SimpleJobBuilder builder = createJob(jobName, - jobSteps); - return runJob(source.getSourceKey(), source.getSourceId(), jobName, builder); + private void clearCache(Source source) { + if (!sourceAccessor.hasAccess(source)) { + return; + } + cacheService.clearCache(source); + cdmCacheService.clearCache(source); } private SimpleJobBuilder createJob(String jobName, List steps) { @@ -611,29 +592,6 @@ private Step getCountStep(Source source, String jobStepName) { .build(); } - private List createClearCacheJobSteps(Source source, String jobName) { - SimpleJob job = new SimpleJob(jobName); - job.setJobRepository(jobRepository); - List steps = new ArrayList<>(); - steps.add(getAchillesClearCacheStep(source, jobName)); - steps.add(getCountClearCacheStep(source, jobName)); - return steps; - } - - private Step getAchillesClearCacheStep(Source source, String jobStepName) { - AchillesClearCacheTasklet achillesTasklet = new AchillesClearCacheTasklet(source, cacheService); - return stepBuilderFactory.get(jobStepName + " achilles") - .tasklet(achillesTasklet) - .build(); - } - - private Step getCountClearCacheStep(Source source, String jobStepName) { - CDMResultsClearCacheTasklet countTasklet = new CDMResultsClearCacheTasklet(source, cdmCacheService); - return stepBuilderFactory.get(jobStepName + " counts") - .tasklet(countTasklet) - .build(); - } - private int getResultsDaimonPriority(Source source) { Optional resultsPriority = source.getDaimons().stream() .filter(d -> d.getDaimonType().equals(SourceDaimon.DaimonType.Results)) @@ -647,10 +605,6 @@ private String getWarmCacheJobName(String sourceIds, String sourceKeys) { return getJobName("warming cache", sourceIds, sourceKeys); } - private String getClearCacheJobName(String sourceIds, String sourceKeys) { - return getJobName("clearing cache", sourceIds, sourceKeys); - } - private String getJobName(String jobType, String sourceIds, String sourceKeys) { // for multiple sources: try to compose a job name from source keys, and if it is too long - use source ids String jobName = String.format("%s: %s", jobType, sourceKeys);