Package tenapull.run

Class Job

java.lang.Object
tenapull.run.Job
Direct Known Subclasses:
DbManagerJob, DbManagerJob.Child, IndexJob, ReformatAll, ReformatOutput, ResetDatabase

public abstract class Job
extends java.lang.Object
Represents a Job that will be queued by the JobFactory and run by a WorkerThread. Many of the critical internal methods of a job can only be accessed using its accessor, which will only be provided to the Main thread once.
  • Nested Class Summary

    Nested Classes 
    Modifier and Type Class Description
    class  Job.Accessor
    The "accessor" allows for quasi-public access to private methods of Job, which should only be accessible to the main thread and to worker threads as necessary.
    static class  Job.Stage
    The enum Stage, which represents the step in the job process which the jobs is at.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static long DEFAULT_TRY_AGAIN_TIME
    The constant DEFAULT_TRY_AGAIN_TIME.
  • Constructor Summary

    Constructors 
    Modifier Constructor Description
    protected Job()
    Instantiates a new Job with a wait timer of zero
    protected Job​(long waitMs)
    Instantiates a new Job, with a wait timer of the provided milliseconds
  • Method Summary

    Modifier and Type Method Description
    protected void addJob​(Job newJob)
    Adds a new job to the readyJobs queue or delayedJobs queue (depending on its tryAtTime).
    protected void addJobs​(java.util.Collection<Job> newJobs)
    Adds a list of new jobs to the readyJobs queue or delayedJobs queue (depending on their tryAtTime).
    boolean equals​(java.lang.Object o)  
    protected abstract boolean exceptionHandler​(java.lang.Exception e, Job.Stage stage)
    Handles any exceptions thrown by isReady, fetch, process, and output methods.
    protected void failed()
    Marks the job as failed, so that it will be permanently removed from the jobs queue after returning from the calling method.
    protected abstract void fetch​(NessusClient client)
    Fetch any API resources, using the NessusClient provided by the worker thread.
    Job.Accessor getAccessor()
    Gets the accessor, which will be provided only to the main thread and only once.
    Job.Stage getStage()
    Gets the Stage which the job is at
    protected abstract boolean isReady()
    Is ready boolean.
    protected void notifyOfExit()
    A method that will always be called whenever a job exits, due either the job returning false from isReady, an exception being thrown, or due to the job finishing.
    protected abstract void output()
    Perform any outputting of the processed data The job will remain in the OUTPUT stage until fetch returns without an exception, or the job is marked as failed.
    protected abstract void process()
    Perform any processing of the fetched data The job will remain in the PROCESS stage until fetch returns without an exception, or the job is marked as failed.
    void start​(WorkerThread runThread, NessusClient client)
    Start the job.
    protected void tryAgainIn​(long ms)
    Overrides the default tryAgainIn for a job which may have had an exception or will be returning false from the isReady method This method MAY ONLY BE INVOKED within the WorkerThread running the job WHILE the job is underway in one of the processing methods (isReady, fetch, process, or output)
    void waitForExit()
    Can be called by other jobs/worker threads while waiting for this job to finish.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • Job

      protected Job()
      Instantiates a new Job with a wait timer of zero
    • Job

      protected Job​(long waitMs)
      Instantiates a new Job, with a wait timer of the provided milliseconds
      Parameters:
      waitMs - the wait ms
  • Method Details

    • isReady

      protected abstract boolean isReady()
      Is ready boolean. Indicates whether the job is ready to run yet. If false is returned, the job will be placed in the JobFactory's waitingJobs queue, with either the default wait time, or another wait time if a new one is set before returning false from isReady. The job will remain in the IDLE stage until isReady returns true, or the job is marked as failed. Failed jobs are not marked as DONE until after the method has returned
      Returns:
      the boolean
    • fetch

      protected abstract void fetch​(NessusClient client) throws java.lang.Exception
      Fetch any API resources, using the NessusClient provided by the worker thread. The job will remain in the FETCH stage until fetch returns without an exception, or the job is marked as failed. Failed jobs are not marked as DONE until after the method has returned
      Parameters:
      client - the nessus client provided by the worker thread which is running this job
      Throws:
      java.lang.Exception - any relevant exception which the job implementation may need to throw
    • process

      protected abstract void process() throws java.lang.Exception
      Perform any processing of the fetched data The job will remain in the PROCESS stage until fetch returns without an exception, or the job is marked as failed. Failed jobs are not marked as DONE until after the method has returned
      Throws:
      java.lang.Exception - any relevant exception which the job implementation may need to throw
    • output

      protected abstract void output() throws java.lang.Exception
      Perform any outputting of the processed data The job will remain in the OUTPUT stage until fetch returns without an exception, or the job is marked as failed. Failed jobs are not marked as DONE until after the method has returned
      Throws:
      java.lang.Exception - any relevant exception which the job implementation may need to throw
    • exceptionHandler

      protected abstract boolean exceptionHandler​(java.lang.Exception e, Job.Stage stage)
      Handles any exceptions thrown by isReady, fetch, process, and output methods. Return true if the operation should be attempted again immediately. Mark as failed to permanently end the job. Otherwise, the job will be placed in the delayedJobs queue, and put back into readyJobs once its wait time is up
      Parameters:
      e - the e
      stage - the stage
      Returns:
      the boolean
    • waitForExit

      public void waitForExit()
      Can be called by other jobs/worker threads while waiting for this job to finish. The other thread/job will be awoken when this job either returns from the output method or is marked as failed. When marked as failed, the awakening won't happen until the job's method has returned
    • failed

      protected final void failed() throws java.lang.IllegalStateException
      Marks the job as failed, so that it will be permanently removed from the jobs queue after returning from the calling method. This method MAY ONLY BE INVOKED within the WorkerThread running the job WHILE the job is underway in one of the processing methods (isReady, fetch, process, or output)
      Throws:
      java.lang.IllegalStateException - if this method is not being called from within one of the processing methods by the established WorkerThread
    • tryAgainIn

      protected final void tryAgainIn​(long ms)
      Overrides the default tryAgainIn for a job which may have had an exception or will be returning false from the isReady method This method MAY ONLY BE INVOKED within the WorkerThread running the job WHILE the job is underway in one of the processing methods (isReady, fetch, process, or output)
      Parameters:
      ms - the ms to delay until the job is placed back in the readyJobs queue
      Throws:
      java.lang.IllegalStateException - if this method is not being called from within one of the processing methods by the established WorkerThread
    • getStage

      public final Job.Stage getStage()
      Gets the Stage which the job is at
      Returns:
      the stage
    • addJob

      protected void addJob​(Job newJob)
      Adds a new job to the readyJobs queue or delayedJobs queue (depending on its tryAtTime). Note that other than Main's seed job, this is the ONLY way to add new jobs. This method MAY ONLY BE INVOKED within the WorkerThread running the job WHILE the job is underway in one of the processing methods (isReady, fetch, process, or output)
      Parameters:
      newJob - the new job to add
      Throws:
      java.lang.IllegalStateException - if this method is not being called from within one of the processing methods by the established WorkerThread
    • addJobs

      protected final void addJobs​(java.util.Collection<Job> newJobs)
      Adds a list of new jobs to the readyJobs queue or delayedJobs queue (depending on their tryAtTime). Note that other than Main's seed job, this is the ONLY way to add new jobs. This method MAY ONLY BE INVOKED within the WorkerThread running the job WHILE the job is underway in one of the processing methods (isReady, fetch, process, or output)
      Parameters:
      newJobs - the list of new jobs
      Throws:
      java.lang.IllegalStateException - if this method is not being called from within one of the processing methods by the established WorkerThread
    • start

      public final void start​(WorkerThread runThread, NessusClient client) throws java.lang.IllegalStateException
      Start the job. This method may only be called by the WorkerThread running the job, which must provide itself and its NessusClient instances.
      Parameters:
      runThread - the run thread
      client - the nessus client to be used by the fetch method
      Throws:
      java.lang.IllegalStateException - if the runThread does not match the current thread, if there is already a runThread underway, or if the job was marked as failed or DONE
    • notifyOfExit

      protected void notifyOfExit()
      A method that will always be called whenever a job exits, due either the job returning false from isReady, an exception being thrown, or due to the job finishing. This method may be overridden by subclass implementations, but is not required to be implemented. By default, it does nothing.
    • getAccessor

      public final Job.Accessor getAccessor() throws java.lang.IllegalStateException
      Gets the accessor, which will be provided only to the main thread and only once. This is to ensure private access to critical methods, which only the main thread should have, and its worker threads as necessary.
      Returns:
      the accessor
      Throws:
      java.lang.IllegalStateException - if the invoking thread is not the main thread, or if the accessor has already been delivered to the main thread
    • equals

      public final boolean equals​(java.lang.Object o)
      Overrides:
      equals in class java.lang.Object