Skip to content

Commit

Permalink
Development: Fix an issue with the migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Krusche committed Dec 23, 2023
1 parent 7d57875 commit d7f78c6
Showing 1 changed file with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tum.in.www1.artemis.config.migration.entries;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
Expand All @@ -11,9 +12,8 @@
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;

import de.tum.in.www1.artemis.config.migration.MigrationEntry;
import de.tum.in.www1.artemis.domain.ProgrammingExercise;
import de.tum.in.www1.artemis.domain.enumeration.ProgrammingLanguage;
import de.tum.in.www1.artemis.repository.ProgrammingExerciseRepository;

Expand Down Expand Up @@ -64,33 +64,27 @@ public void execute() {
log.info("Migrating {} programming exercises. This might take a while.", exercisesToMigrate.size());

ExecutorService executorService = Executors.newFixedThreadPool(THREADS);

var chunks = Lists.partition(exercisesToMigrate, BATCH_SIZE);

for (var chunk : chunks) {
CompletableFuture<?>[] allFutures = new CompletableFuture[Math.min(THREADS, chunk.size())];

for (int i = 0; i < chunk.size(); i++) {
var exercise = chunk.get(i);
allFutures[i] = CompletableFuture.runAsync(() -> {
boolean checkoutSolutionRepository = false;
try {
checkoutSolutionRepository = ciMigrationService.get().hasSolutionRepository(exercise.getTemplateBuildPlanId());
}
catch (Exception e) {
log.error("Error while checking if exercise {} needs to check out solution repository in build plan, setting checkoutSolutionRepository to false",
exercise.getId(), e);
}
exercise.setCheckoutSolutionRepository(checkoutSolutionRepository);
log.debug("Migrated exercise {}", exercise.getId());
programmingExerciseRepository.save(exercise);
}, executorService);
}

// Wait until all currently loaded exercises are migrated to avoid loading too many exercises at the same time.
CompletableFuture.allOf(allFutures).join();
List<CompletableFuture<?>> allFutures = new ArrayList<>();

for (ProgrammingExercise exercise : exercisesToMigrate) {
allFutures.add(CompletableFuture.runAsync(() -> {
boolean checkoutSolutionRepository = false;
try {
checkoutSolutionRepository = ciMigrationService.get().hasSolutionRepository(exercise.getTemplateBuildPlanId());
}
catch (Exception e) {
log.error("Error while checking if exercise {} needs to check out solution repository in build plan, setting checkoutSolutionRepository to false",
exercise.getId(), e);
}
exercise.setCheckoutSolutionRepository(checkoutSolutionRepository);
log.debug("Migrated exercise {}", exercise.getId());
programmingExerciseRepository.save(exercise);
}, executorService));
}

// Wait until all currently loaded exercises are migrated to avoid loading too many exercises at the same time.
CompletableFuture.allOf(allFutures.toArray(new CompletableFuture[] {})).join();

executorService.shutdown();
}

Expand Down

0 comments on commit d7f78c6

Please sign in to comment.