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