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

Mutations

40

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

42

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

69

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

74

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

82

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

85

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