Package edu.ucsb.cs156.jobs.services
Class JobService
java.lang.Object
edu.ucsb.cs156.jobs.services.JobService
Creates
Job rows and runs JobContextConsumers asynchronously on the
jobsExecutor, recording status and log output as they run.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetJobLogPreview(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.getJobLogs(Long jobId) The full log for one job, oldest line first.getJobLogTail(Long jobId, Long afterId) Everything logged for this job sinceafterId(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.runAsJob(JobContextConsumer jobFunction) voidrunJobAsync(Job job, JobContextConsumer jobFunction) Runs a job asynchronously.
-
Constructor Details
-
JobService
public JobService()
-
-
Method Details
-
runAsJob
-
runJobAsync
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 jobjobFunction- runnable job function
-
getJobLogs
The full log for one job, oldest line first. -
getJobLogTail
Everything logged for this job sinceafterId(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
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 ofJobrows).
-