| 1 | package edu.ucsb.cs156.frontiers.services.jobs; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.frontiers.entities.Course; | |
| 4 | import edu.ucsb.cs156.frontiers.entities.Job; | |
| 5 | import edu.ucsb.cs156.frontiers.repositories.JobsRepository; | |
| 6 | import edu.ucsb.cs156.frontiers.services.CurrentUserService; | |
| 7 | import lombok.extern.slf4j.Slf4j; | |
| 8 | import org.springframework.beans.factory.annotation.Autowired; | |
| 9 | import org.springframework.context.annotation.Lazy; | |
| 10 | import org.springframework.scheduling.annotation.Async; | |
| 11 | import org.springframework.stereotype.Service; | |
| 12 | ||
| 13 | @Service | |
| 14 | @Slf4j | |
| 15 | public class JobService { | |
| 16 | @Autowired private JobsRepository jobsRepository; | |
| 17 | ||
| 18 | @Autowired private CurrentUserService currentUserService; | |
| 19 | ||
| 20 | @Autowired private JobContextFactory contextFactory; | |
| 21 | ||
| 22 | /* | |
| 23 | * This is a self-referential bean to allow for async method calls within the same class. | |
| 24 | */ | |
| 25 | @Lazy @Autowired private JobService self; | |
| 26 | ||
| 27 | public Job runAsJob(JobContextConsumer jobFunction) { | |
| 28 | String jobName = jobFunction.getClass().getName().replace("edu.ucsb.cs156.frontiers.jobs.", ""); | |
| 29 | ||
| 30 | Course course = jobFunction.getCourse(); | |
| 31 | ||
| 32 | Job job = | |
| 33 | Job.builder() | |
| 34 | .createdBy(currentUserService.getUser()) | |
| 35 | .status("running") | |
| 36 | .jobName(jobName) | |
| 37 | .course(course) | |
| 38 |
1
1. runAsJob : negated conditional → KILLED |
.courseName(course != null ? course.getCourseName() : null) |
| 39 | .userEmail(currentUserService.getUser().getEmail()) | |
| 40 | .build(); | |
| 41 | ||
| 42 | log.info("Starting job: {}, jobName={}", job.getId(), job.getJobName()); | |
| 43 | ||
| 44 | jobsRepository.save(job); | |
| 45 |
1
1. runAsJob : removed call to edu/ucsb/cs156/frontiers/services/jobs/JobService::runJobAsync → KILLED |
self.runJobAsync(job, jobFunction); |
| 46 | ||
| 47 |
1
1. runAsJob : replaced return value with null for edu/ucsb/cs156/frontiers/services/jobs/JobService::runAsJob → KILLED |
return job; |
| 48 | } | |
| 49 | ||
| 50 | @Async | |
| 51 | public void runJobAsync(Job job, JobContextConsumer jobFunction) { | |
| 52 | JobContext context = contextFactory.createContext(job); | |
| 53 | ||
| 54 | try { | |
| 55 | jobFunction.accept(context); | |
| 56 | } catch (Exception e) { | |
| 57 |
1
1. runJobAsync : removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED |
job.setStatus("error"); |
| 58 | context.log(e.getMessage()); | |
| 59 | return; | |
| 60 | } | |
| 61 | ||
| 62 |
1
1. runJobAsync : removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED |
job.setStatus("complete"); |
| 63 | jobsRepository.save(job); | |
| 64 | } | |
| 65 | ||
| 66 | public String getJobLogs(Long jobId) { | |
| 67 | Job job = | |
| 68 | jobsRepository | |
| 69 | .findById(jobId) | |
| 70 |
1
1. lambda$getJobLogs$0 : replaced return value with null for edu/ucsb/cs156/frontiers/services/jobs/JobService::lambda$getJobLogs$0 → KILLED |
.orElseThrow(() -> new IllegalArgumentException("Job not found")); |
| 71 | ||
| 72 | String log = job.getLog(); | |
| 73 |
1
1. getJobLogs : negated conditional → KILLED |
return log != null ? log : ""; |
| 74 | } | |
| 75 | } | |
Mutations | ||
| 38 |
1.1 |
|
| 45 |
1.1 |
|
| 47 |
1.1 |
|
| 57 |
1.1 |
|
| 62 |
1.1 |
|
| 70 |
1.1 |
|
| 73 |
1.1 |