Class JobService

java.lang.Object
edu.ucsb.cs156.jobs.services.JobService

public class JobService extends Object
Creates Job rows and runs JobContextConsumers asynchronously on the jobsExecutor, recording status and log output as they run.
  • Constructor Details

    • JobService

      public JobService()
  • Method Details

    • runAsJob

      public Job runAsJob(JobContextConsumer jobFunction)
    • runJobAsync

      @Async("jobsExecutor") public void runJobAsync(Job job, JobContextConsumer jobFunction)
      Runs a job asynchronously.

      This method uses a TransactionTemplate because outside of the Spring context, you cannot delete entities that are unmanaged by Hibernate. Using the transactionTemplate lambda keeps the database session open and allows Hibernate to maintain its knowledge of the object graph (i.e. the entities).

      Note that using the transactionTemplate lambda means that if there is an unhandled exception, either every database transaction succeeds, or all of them are rolled back.

      However, the job entity metadata will still be saved.

      Parameters:
      job - metadata entity about the job
      jobFunction - runnable job function
    • getJobLogs

      public String getJobLogs(Long jobId)
      The full log for one job, oldest line first.
    • getJobLogTail

      public List<JobLog> getJobLogTail(Long jobId, Long afterId)
      Everything logged for this job since afterId (exclusive), oldest first — the incremental "tail -f" query: a polling client passes the highest id it has already seen and gets back only new lines.
    • getJobLogPreview

      public String getJobLogPreview(Long jobId)
      The most recent log lines for one job, joined into a single string oldest-first — used to populate a preview on list/paginated responses without shipping each job's entire log. Assumes the job id is already known-valid (the caller has just fetched a page of Job rows).