JobService.java

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
import org.springframework.transaction.annotation.Transactional;
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
    Job job =
31
        Job.builder()
32
            .createdBy(currentUserService.getUser())
33
            .status("running")
34
            .jobName(jobName)
35
            .course(jobFunction.getCourse())
36
            .build();
37
38
    log.info("Starting job: {}, jobName={}", job.getId(), job.getJobName());
39
40
    jobsRepository.save(job);
41 1 1. runAsJob : removed call to edu/ucsb/cs156/frontiers/services/jobs/JobService::runJobAsync → KILLED
    self.runJobAsync(job, jobFunction);
42
43 1 1. runAsJob : replaced return value with null for edu/ucsb/cs156/frontiers/services/jobs/JobService::runAsJob → KILLED
    return job;
44
  }
45
46
  /**
47
   * Runs a job asynchronously.
48
   *
49
   * <p>This method is annotated with @Transactional because outside of the Spring context, you
50
   * cannot delete entities that are unmanaged by Hibernate. Adding @Transactional keeps the
51
   * database session open and allows Hibernate to maintain it's knowledge of the object graph (i.e.
52
   * the entities)
53
   *
54
   * <p>To learn more, read about Hibernate and the concept of a Spring Context.
55
   *
56
   * <p>Note that @Transactional means that if there is an unhandled exception, either every
57
   * database transactions succeeds, or all of them are rolled back.
58
   *
59
   * @param job
60
   * @param jobFunction
61
   */
62
  @Async
63
  @Transactional
64
  public void runJobAsync(Job job, JobContextConsumer jobFunction) {
65
    JobContext context = contextFactory.createContext(job);
66
67
    try {
68
      jobFunction.accept(context);
69
    } catch (Exception e) {
70 1 1. runJobAsync : removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED
      job.setStatus("error");
71
      context.log(e.getMessage());
72
      return;
73
    }
74
75 1 1. runJobAsync : removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED
    job.setStatus("complete");
76
    jobsRepository.save(job);
77
  }
78
79
  public String getJobLogs(Long jobId) {
80
    Job job =
81
        jobsRepository
82
            .findById(jobId)
83 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"));
84
85
    String log = job.getLog();
86 1 1. getJobLogs : negated conditional → KILLED
    return log != null ? log : "";
87
  }
88
}

Mutations

41

1.1
Location : runAsJob
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:runAsJob_fires_correctly()]
removed call to edu/ucsb/cs156/frontiers/services/jobs/JobService::runJobAsync → KILLED

43

1.1
Location : runAsJob
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:runAsJob_fires_correctly()]
replaced return value with null for edu/ucsb/cs156/frontiers/services/jobs/JobService::runAsJob → KILLED

70

1.1
Location : runJobAsync
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:runAsyncJob_handles_error()]
removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED

75

1.1
Location : runJobAsync
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:runAsyncJob_fires_correctly()]
removed call to edu/ucsb/cs156/frontiers/entities/Job::setStatus → KILLED

83

1.1
Location : lambda$getJobLogs$0
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:test_getJobLogs_job_not_found()]
replaced return value with null for edu/ucsb/cs156/frontiers/services/jobs/JobService::lambda$getJobLogs$0 → KILLED

86

1.1
Location : getJobLogs
Killed by : edu.ucsb.cs156.frontiers.services.JobServiceTests.[engine:junit-jupiter]/[class:edu.ucsb.cs156.frontiers.services.JobServiceTests]/[method:test_getJobLogs_with_null_log()]
negated conditional → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0