Skip to content

Commit

Permalink
manually reverting changes in case of failures
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-ebi committed Feb 5, 2024
1 parent c0a851a commit dadb928
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import uk.ac.ebi.eva.contigalias.repo.AssemblyRepository;
import uk.ac.ebi.eva.contigalias.repo.ChromosomeRepository;

import javax.transaction.Transactional;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
Expand Down Expand Up @@ -165,7 +164,6 @@ public Optional<Path> downloadAssemblyReport(String accession, NCBIBrowser ncbiB
}
}

@Transactional
public void parseFileAndInsertAssembly(String accession, ENAAssemblyDataSource enaDataSource,
AssemblyRepository assemblyRepository, ChromosomeRepository chromosomeRepository) throws IOException {
Optional<Path> downloadNCBIFilePathOpt = downloadAssemblyReport(accession);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package uk.ac.ebi.eva.contigalias.exception;

public class AssemblyIngestionException extends RuntimeException {

public AssemblyIngestionException(String accession) {
super("Error Ingesting assembly with accession " + accession);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,9 @@ public ResponseEntity<String> handleExceptions(DownloadFailedException exception
return new ResponseEntity<>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(AssemblyIngestionException.class)
public ResponseEntity<String> handleExceptions(AssemblyIngestionException exception, WebRequest webRequest){
return new ResponseEntity<>(exception.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import uk.ac.ebi.eva.contigalias.entities.AssemblyEntity;
Expand All @@ -46,8 +49,17 @@ default Optional<AssemblyEntity> findAssemblyEntityByAccession(String accession)
Page<AssemblyEntity> findAssemblyEntitiesByTaxid(long taxid, Pageable pageable);

@Transactional
void deleteAssemblyEntityByInsdcAccession(String insdcAccession);
@Modifying
@Query("DELETE FROM AssemblyEntity a WHERE a.insdcAccession=:asmInsdcAccession")
void deleteAssemblyEntityByInsdcAccession(@Param("asmInsdcAccession") String asmInsdcAccession);

@Transactional
void deleteAssemblyEntityByRefseq(String refseq);
@Modifying
@Query("DELETE FROM AssemblyEntity a WHERE a.refseq=:asmRefSeq")
void deleteAssemblyEntityByRefseq(@Param("asmRefSeq") String asmRefSeq);

@Transactional
@Modifying
@Query("DELETE FROM AssemblyEntity a WHERE a.insdcAccession=:asmAccession OR a.refseq=:asmAccession")
void deleteAssemblyEntityByInsdcAccessionOrRefseq(@Param("asmAccession") String asmAccession);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import uk.ac.ebi.eva.contigalias.entities.AssemblyEntity;
import uk.ac.ebi.eva.contigalias.entities.ChromosomeEntity;

import javax.transaction.Transactional;
import java.util.List;

@Repository
Expand All @@ -50,6 +51,11 @@ public interface ChromosomeRepository extends JpaRepository<ChromosomeEntity, Lo
@Query("UPDATE ChromosomeEntity c SET c.md5checksum = :md5Checksum WHERE c.assembly.insdcAccession= :asmInsdcAccession AND c.insdcAccession = :insdcAccession")
void updateMd5ChecksumByInsdcAccession(@Param("asmInsdcAccession") String asmInsdcAccession, @Param("insdcAccession") String insdcAccession, @Param("md5Checksum") String md5Checksum);

@Transactional
@Modifying
@Query("DELETE FROM ChromosomeEntity c WHERE c.assembly.insdcAccession=:asmInsdcAccession")
void deleteChromosomeEntitiesByAssembly_InsdcAccession(@Param("asmInsdcAccession") String asmInsdcAccession);

Page<ChromosomeEntity> findChromosomeEntitiesByAssembly_Refseq(String asmRefseq, Pageable request);

Page<ChromosomeEntity> findChromosomeEntitiesByGenbankSequenceNameAndAssembly_Taxid(String genbankName, long asmTaxid, Pageable request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;

@Component
public class ChecksumSetter {
Expand Down Expand Up @@ -48,7 +47,7 @@ public void updateMd5CheckSumForAllAssemblies() {
CompletableFuture<Void> future = updateMd5CheckSumForAssemblyAsync(assembly);
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
} catch (Exception e) {
logger.error("Encountered an error when running MD5Checksum update for assembly: " + assembly);
} finally {
scheduledToRunMD5ChecksumUpdateTasks.remove(assembly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import uk.ac.ebi.eva.contigalias.datasource.NCBIAssemblyDataSource;
import uk.ac.ebi.eva.contigalias.entities.AssemblyEntity;
import uk.ac.ebi.eva.contigalias.entities.ChromosomeEntity;
import uk.ac.ebi.eva.contigalias.exception.AssemblyIngestionException;
import uk.ac.ebi.eva.contigalias.exception.AssemblyNotFoundException;
import uk.ac.ebi.eva.contigalias.exception.DuplicateAssemblyException;
import uk.ac.ebi.eva.contigalias.repo.AssemblyRepository;
Expand Down Expand Up @@ -105,12 +106,24 @@ public void fetchAndInsertAssembly(String accession) throws IOException {
throw duplicateAssemblyInsertionException(accession, entity.get());
}

// download file and save assembly and chromosome data
ncbiDataSource.parseFileAndInsertAssembly(accession, enaDataSource, assemblyRepository, chromosomeRepository);
logger.info("Successfully inserted assembly for accession " + accession);
try {
// download file and save assembly and chromosome data
ncbiDataSource.parseFileAndInsertAssembly(accession, enaDataSource, assemblyRepository, chromosomeRepository);
logger.info("Successfully inserted assembly for accession " + accession);

// submit job for retrieving and updating MD5 Checksum for assembly (asynchronously)
checksumSetter.updateMd5CheckSumForAssemblyAsync(accession);
} catch (Exception e) {
// roll back inserted entries in case of any exception or error
logger.error("Exception while inserting assembly " + accession + " Rolling back changes");
deleteEntriesForAssembly(accession);
throw new AssemblyIngestionException(accession);
}
}

// submit job for retrieving and updating MD5 Checksum for assembly (asynchronously)
checksumSetter.updateMd5CheckSumForAssemblyAsync(accession);
public void deleteEntriesForAssembly(String accession) {
chromosomeRepository.deleteChromosomeEntitiesByAssembly_InsdcAccession(accession);
assemblyRepository.deleteAssemblyEntityByInsdcAccessionOrRefseq(accession);
}

public void retrieveAndInsertMd5ChecksumForAssembly(String assembly) {
Expand Down

0 comments on commit dadb928

Please sign in to comment.