Commit 30284150 authored by Matija Obreza's avatar Matija Obreza

Update accession#crop based on #cropName

- AccessionProcessor#apply needs @Transactional(noRollbackFor) for multithreaded execution
parent f4a9aed4
......@@ -111,15 +111,6 @@ public interface AccessionService {
Map<UUID, AccessionIdentifier3> toUUID(List<? extends AccessionIdentifier3> identifiers);
List<Accession> forUuids(Set<UUID> uuids);
/**
* Loads {@link Accession} by IDs and executes the action on each accession in a
* Spring transaction. Spring security context not supported.
*
* @param accessionIds List of accession IDs
* @param action the action to execute on each accession
* @return the list of processed accessions
*/
List<Accession> processAccessions(List<Long> accessionIds, IAccessionAction action);
/**
* Executes the action on a list of accession in a
......
......@@ -318,22 +318,7 @@ public class AccessionServiceImpl implements AccessionService {
}
@Override
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, timeout = 30)
public List<Accession> processAccessions(List<Long> accessionIds, IAccessionAction action) {
List<Accession> accessions = accessionRepository.findAll(accessionIds);
LOG.debug("Processing {} accessions of {} IDs provided", accessions.size(), accessionIds.size());
accessions.forEach(accession -> {
try {
action.apply(accession);
} catch (Exception e) {
LOG.info("Error processing accession: {}", e.getMessage());
}
});
return accessions;
}
@Override
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, timeout = 30)
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, noRollbackFor = Throwable.class)
public List<Accession> processAccessions(List<Long> accessionIds, IAccessionBatchAction action) {
List<Accession> accessions = accessionRepository.findAll(accessionIds);
LOG.debug("Processing {} accessions of {} IDs provided", accessions.size(), accessionIds.size());
......
......@@ -24,6 +24,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
......@@ -585,13 +586,13 @@ public class CropServiceImpl implements CropService {
accessionProcessor.apply(predicate, (accessions) -> {
accessions.forEach(accession -> {
Crop newCrop = getCrop(accession.getCropName());
LOG.debug("{} from {}: {} -> {}", accession.getUuid(), accession.getInstCode(), accession.getCropName(), newCrop == null ? "null" : newCrop.getShortName());
// assign new crop
accession.setCrop(newCrop);
});
asAdminInvoker.invoke(() -> {
accessionRepository.save(accessions);
return true;
LOG.trace("{} from {}: {} -> {}", accession.getUuid(), accession.getInstCode(), accession.getCropName(), newCrop == null ? "null" : newCrop.getShortName());
if (! Objects.equals(accession.getCrop(), newCrop)) {
// assign crop, bypass auditing
LOG.debug("Unlinking crop for {} from {}: {} = {}", accession.getUuid(), accession.getInstCode(), accession.getCropName(), newCrop == null ? "null" : newCrop.getShortName());
accessionRepository.updateCrop(accession, newCrop);
}
});
return accessions;
});
......@@ -615,13 +616,13 @@ public class CropServiceImpl implements CropService {
accessionProcessor.apply(predicate, (accessions) -> {
accessions.forEach(accession -> {
Crop newCrop = getCrop(accession.getCropName());
LOG.debug("{} from {}: {} -> {}", accession.getUuid(), accession.getInstCode(), accession.getCropName(), newCrop == null ? "null" : newCrop.getShortName());
// assign crop
accession.setCrop(newCrop);
});
asAdminInvoker.invoke(() -> {
accessionRepository.save(accessions);
return true;
LOG.trace("{} from {}: {} -> {}", accession.getUuid(), accession.getInstCode(), accession.getCropName(), newCrop == null ? "null" : newCrop.getShortName());
if (! Objects.equals(newCrop, accession.getCrop())) {
// assign crop, bypass auditing
LOG.debug("Updating crop for {} from {}: {} = {}", accession.getUuid(), accession.getInstCode(), accession.getCropName(), newCrop == null ? "null" : newCrop.getShortName());
accessionRepository.updateCrop(accession, newCrop);
}
});
return accessions;
});
......
......@@ -20,7 +20,6 @@ import java.util.List;
import javax.persistence.EntityManager;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.QAccession;
......@@ -206,23 +205,9 @@ public class AccessionProcessor {
final ArrayList<Long> copy = new ArrayList<>(accessionIds);
threadPoolTaskExecutor.execute(() -> {
for (int i = 5; i >= 0; i--) {
try {
LOG.trace("Executing action on {} Accessions.", copy.size());
accessionService.processAccessions(copy, action);
LOG.trace("Done executing action on {} accessions.", copy.size());
break; // break try-5-times loop
} catch (Throwable e) {
if (i == 0) {
throw e;
}
LOG.warn("Error executing action on batch: {}. Retrying.", e.getMessage());
try {
Thread.sleep(RandomUtils.nextInt(10, 1000));
} catch (InterruptedException e1) {
}
}
}
LOG.trace("Executing action on {} Accessions.", copy.size());
accessionService.processAccessions(copy, action);
LOG.trace("Done executing action on {} accessions.", copy.size());
});
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment