Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assessment: Fix sporadically delayed score calculation #10289

Merged
merged 5 commits into from
Feb 12, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import de.tum.cit.aet.artemis.assessment.ResultListener;
Expand Down Expand Up @@ -70,7 +69,7 @@ public class ParticipantScoreScheduleService {

private final TaskScheduler scheduler;

private final Map<Integer, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();
private final Map<ParticipantScoreId, ScheduledFuture<?>> scheduledTasks = new ConcurrentHashMap<>();

private Optional<Instant> lastScheduledRun = Optional.empty();

Expand Down Expand Up @@ -160,7 +159,6 @@ public void shutdown() {
* Additionally, we schedule all participant scores that are outdated/invalid.
*/
// TODO: could be converted to TaskScheduler, but tests depend on this implementation at the moment. See QuizScheduleService for reference
@Scheduled(cron = "0 * * * * *")
magaupp marked this conversation as resolved.
Show resolved Hide resolved
protected void scheduleTasks() {
log.debug("Schedule tasks to process...");
SecurityUtils.setAuthorizationObject();
Expand Down Expand Up @@ -223,17 +221,17 @@ public void scheduleTask(@NotNull Long exerciseId, @NotNull Long participantId,
* @param resultIdToBeDeleted the id of the result that is about to be deleted (or null, if result is created/updated)
*/
private void scheduleTask(Long exerciseId, Long participantId, Instant resultLastModified, Long resultIdToBeDeleted) {
final int participantScoreHash = new ParticipantScoreId(exerciseId, participantId).hashCode();
var task = scheduledTasks.get(participantScoreHash);
final var participantScoreId = new ParticipantScoreId(exerciseId, participantId);
var task = scheduledTasks.get(participantScoreId);
if (task != null) {
// If a task is already scheduled, cancel it and reschedule it with the latest result
task.cancel(true);
scheduledTasks.remove(participantScoreHash);
scheduledTasks.remove(participantScoreId);
}

var schedulingTime = ZonedDateTime.now().plus(DEFAULT_WAITING_TIME_FOR_SCHEDULED_TASKS, ChronoUnit.MILLIS);
var future = scheduler.schedule(() -> this.executeTask(exerciseId, participantId, resultLastModified, resultIdToBeDeleted), schedulingTime.toInstant());
scheduledTasks.put(participantScoreHash, future);
scheduledTasks.put(participantScoreId, future);
log.debug("Scheduled task for exercise {} and participant {} at {}.", exerciseId, participantId, schedulingTime);
}

Expand Down Expand Up @@ -342,7 +340,7 @@ private void executeTask(Long exerciseId, Long participantId, Instant resultLast
log.error("Exception while processing participant score for exercise {} and participant {} for participant scores:", exerciseId, participantId, e);
}
finally {
scheduledTasks.remove(new ParticipantScoreId(exerciseId, participantId).hashCode());
scheduledTasks.remove(new ParticipantScoreId(exerciseId, participantId));
}
long end = System.currentTimeMillis();
log.debug("Updating the participant score for exercise {} and participant {} took {} ms.", exerciseId, participantId, end - start);
Expand Down
Loading