-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scheduler for retrieving and saving md5 checksum
- Loading branch information
Showing
6 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/uk/ac/ebi/eva/contigalias/scheduler/ChecksumSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package uk.ac.ebi.eva.contigalias.scheduler; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import uk.ac.ebi.eva.contigalias.entities.ChromosomeEntity; | ||
import uk.ac.ebi.eva.contigalias.service.ChromosomeService; | ||
|
||
import java.util.List; | ||
|
||
@Component | ||
public class ChecksumSetter { | ||
private final Logger logger = LoggerFactory.getLogger(ChecksumSetter.class); | ||
private int DEFAULT_PAGE_SIZE = 10000; | ||
private ChromosomeService chromosomeService; | ||
private Md5ChecksumRetriever md5ChecksumRetriever; | ||
|
||
@Autowired | ||
public ChecksumSetter(ChromosomeService chromosomeService, Md5ChecksumRetriever md5ChecksumRetriever) { | ||
this.chromosomeService = chromosomeService; | ||
this.md5ChecksumRetriever = md5ChecksumRetriever; | ||
} | ||
|
||
// @Scheduled(cron = "30 15 10 1 * ? 2023") -- the task to run at 10:15:30 AM on the 1st day of every month in the year 2023. | ||
//Seconds: 30 Minutes: 15 Hours: 10 Day of the month: 1 Month: Every month Day of the week: Every day of the week Year: 2023 | ||
@Scheduled(initialDelay = 0, fixedDelay = 24 * 60 * 60 * 1000) | ||
public void updateMd5CheckSumForAllAssemblies() { | ||
List<String> assemblyList = chromosomeService.getAssembliesWhereChromosomeMd5ChecksumIsNull(); | ||
for (String assembly : assemblyList) { | ||
logger.info("Trying to update md5checksum for assembly: " + assembly); | ||
updateMD5ChecksumForAllChromosomesInAssembly(assembly); | ||
} | ||
} | ||
|
||
public void updateMD5ChecksumForAllChromosomesInAssembly(String assembly) { | ||
int pageNumber = 0; | ||
Pageable pageable = PageRequest.of(pageNumber, DEFAULT_PAGE_SIZE); | ||
Slice<ChromosomeEntity> chrSlice = chromosomeService.getChromosomesByAssemblyInsdcAccessionWhereMd5ChecksumIsNull(assembly, pageable); | ||
while (chrSlice.hasContent()) { | ||
List<ChromosomeEntity> chromosomeEntityList = chrSlice.getContent(); | ||
updateMd5ChecksumForChromosome(chromosomeEntityList); | ||
|
||
pageNumber++; | ||
pageable = PageRequest.of(pageNumber, DEFAULT_PAGE_SIZE); | ||
chrSlice = chromosomeService.getChromosomesByAssemblyInsdcAccessionWhereMd5ChecksumIsNull(assembly, pageable); | ||
} | ||
} | ||
|
||
public void updateMd5ChecksumForChromosome(List<ChromosomeEntity> chromosomesList) { | ||
chromosomesList.parallelStream().forEach(chromosome -> { | ||
try { | ||
String md5Checksum = md5ChecksumRetriever.retrieveMd5Checksum(chromosome.getInsdcAccession()); | ||
chromosome.setMd5checksum(md5Checksum); | ||
} catch (Exception e) { | ||
logger.info("Could not retrieve md5Checksum for insdc accession: " + chromosome.getInsdcAccession()); | ||
} | ||
}); | ||
|
||
chromosomeService.updateMd5ChecksumForAll(chromosomesList); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/uk/ac/ebi/eva/contigalias/scheduler/Md5ChecksumRetriever.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package uk.ac.ebi.eva.contigalias.scheduler; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.retry.annotation.Backoff; | ||
import org.springframework.retry.annotation.Retryable; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Component | ||
public class Md5ChecksumRetriever { | ||
private final Logger logger = LoggerFactory.getLogger(Md5ChecksumRetriever.class); | ||
private String INSDC_ACCESSION_PLACE_HOLDER = "INSDC_ACCESSION_PLACE_HOLDER"; | ||
private String INSDC_CHECKSUM_URL = "https://www.ebi.ac.uk/ena/cram/sequence/insdc:" + INSDC_ACCESSION_PLACE_HOLDER + "/metadata"; | ||
private RestTemplate restTemplate = new RestTemplate(); | ||
|
||
@Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 2000, multiplier = 2)) | ||
public String retrieveMd5Checksum(String insdcAccession) { | ||
String apiURL = INSDC_CHECKSUM_URL.replace(INSDC_ACCESSION_PLACE_HOLDER, insdcAccession); | ||
JsonNode jsonResponse = restTemplate.getForObject(apiURL, JsonNode.class); | ||
String md5Checksum = jsonResponse.get("metadata").get("md5").asText(); | ||
return md5Checksum; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters