| 1 | package edu.ucsb.cs156.courses.services; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.courses.jobs.MissingCourseDataJob; | |
| 4 | import edu.ucsb.cs156.courses.jobs.UpdateCourseDataJobFactory; | |
| 5 | import edu.ucsb.cs156.courses.repositories.CourseDataJobsRepository; | |
| 6 | import edu.ucsb.cs156.courses.repositories.UCSBSubjectRepository; | |
| 7 | import edu.ucsb.cs156.jobs.services.JobService; | |
| 8 | import java.util.List; | |
| 9 | import lombok.RequiredArgsConstructor; | |
| 10 | import org.springframework.security.core.context.SecurityContextHolder; | |
| 11 | import org.springframework.stereotype.Service; | |
| 12 | import org.springframework.transaction.annotation.Isolation; | |
| 13 | import org.springframework.transaction.annotation.Transactional; | |
| 14 | ||
| 15 | @Service | |
| 16 | @RequiredArgsConstructor | |
| 17 | public class CourseDataLoadService { | |
| 18 | private final UCSBSubjectRepository subjects; | |
| 19 | private final UpdateService updates; | |
| 20 | private final CourseDataJobsRepository jobs; | |
| 21 | private final UpdateCourseDataJobFactory factory; | |
| 22 | private final JobService jobService; | |
| 23 | ||
| 24 | /** | |
| 25 | * Serialize the short check/submit step across app instances using the existing subject row. | |
| 26 | * JobService commits its queued row in a separate transaction before returning. READ_COMMITTED | |
| 27 | * ensures the next lock holder sees that job. No course retrieval happens in this transaction. | |
| 28 | * Terminal jobs do not suppress retries, and an old update still counts as loaded data. | |
| 29 | */ | |
| 30 | @Transactional(isolation = Isolation.READ_COMMITTED, noRollbackFor = Exception.class) | |
| 31 | public boolean ensureLoaded(String subject, String quarter) throws Exception { | |
| 32 | subjects | |
| 33 | .lockSubject(subject) | |
| 34 |
1
1. lambda$ensureLoaded$0 : replaced return value with null for edu/ucsb/cs156/courses/services/CourseDataLoadService::lambda$ensureLoaded$0 → KILLED |
.orElseThrow(() -> new IllegalArgumentException("Unknown subject")); |
| 35 |
1
1. ensureLoaded : negated conditional → KILLED |
if (updates.getLastUpdate(subject, quarter).isPresent()) { |
| 36 |
1
1. ensureLoaded : replaced boolean return with false for edu/ucsb/cs156/courses/services/CourseDataLoadService::ensureLoaded → KILLED |
return true; |
| 37 | } | |
| 38 | var job = | |
| 39 | new MissingCourseDataJob( | |
| 40 | subject, quarter, factory.createForSubjectAndQuarterAndIfStale(subject, quarter, true)); | |
| 41 |
1
1. ensureLoaded : negated conditional → KILLED |
if (!jobs.existsByJobNameAndStatusIn( |
| 42 | job.getJobName(), List.of("queued", "running", "cancelling"))) { | |
| 43 | var originalContext = SecurityContextHolder.getContext(); | |
| 44 | try { | |
| 45 | // Shared cache fills are system jobs; user attribution must not require an OAuth lookup. | |
| 46 |
1
1. ensureLoaded : removed call to org/springframework/security/core/context/SecurityContextHolder::setContext → KILLED |
SecurityContextHolder.setContext(SecurityContextHolder.createEmptyContext()); |
| 47 | jobService.runAsJob(job); | |
| 48 | } catch (RuntimeException e) { | |
| 49 | // The library commits the queued row before dispatching to its executor. If dispatch | |
| 50 | // fails, do not leave a queued row that would suppress every future retry. | |
| 51 |
1
1. ensureLoaded : removed call to edu/ucsb/cs156/courses/repositories/CourseDataJobsRepository::failQueuedSubmission → KILLED |
jobs.failQueuedSubmission(job.getJobName()); |
| 52 | throw e; | |
| 53 | } finally { | |
| 54 |
1
1. ensureLoaded : removed call to org/springframework/security/core/context/SecurityContextHolder::setContext → KILLED |
SecurityContextHolder.setContext(originalContext); |
| 55 | } | |
| 56 | } | |
| 57 |
1
1. ensureLoaded : replaced boolean return with true for edu/ucsb/cs156/courses/services/CourseDataLoadService::ensureLoaded → KILLED |
return false; |
| 58 | } | |
| 59 | } | |
Mutations | ||
| 34 |
1.1 |
|
| 35 |
1.1 |
|
| 36 |
1.1 |
|
| 41 |
1.1 |
|
| 46 |
1.1 |
|
| 51 |
1.1 |
|
| 54 |
1.1 |
|
| 57 |
1.1 |