Class JobManager

java.lang.Object
net.simplace.sim.wrapper.rest.job.JobManager

public class JobManager extends Object
Manages asynchronously running jobs (currently only /run) across all sessions. Jobs run in a dedicated single-thread executor - more threads would not help, because SessionManager.ENGINE_LOCK serialises the actual execution anyway; the executor only makes sure that Javalin's HTTP request thread can return immediately instead of waiting for ENGINE_LOCK. Jobs are not persisted and not actively cleaned up (no TTL as for sessions) - they are small and die with their session at the latest (SessionManager also removes the jobs of a session when it is closed or evicted, see removeJobsForSession).
  • Constructor Details

    • JobManager

      public JobManager()
  • Method Details

    • submit

      public RestJob submit(String sessionId, JobManager.ThrowingConsumer<RestJob> work)
      Creates a job and queues its work in the worker thread. The job is marked RUNNING when the work starts, DONE when it finishes and FAILED if it throws (also an Error, so that the job never stays RUNNING) or if a simulation reported an error during the run (FAILED is not overwritten).
      Parameters:
      sessionId - id of the session the job belongs to; used as prefix of the job id
      work - the work to execute; receives the job to report progress
      Returns:
      the new job (status PENDING)
    • get

      public RestJob get(String jobId)
      Parameters:
      jobId - job id
      Returns:
      the job
      Throws:
      JobManager.NoSuchJobException - if there is no job with this id
    • hasActiveJobs

      public boolean hasActiveJobs(String sessionId)
      Parameters:
      sessionId - session id
      Returns:
      true if the session has a job that is still queued (PENDING) or RUNNING
    • removeJobsForSession

      public void removeJobsForSession(String sessionId)
      Removes all jobs of a session. Jobs that are still queued or running are not cancelled.
      Parameters:
      sessionId - session id