Skip to content

Commit

Permalink
Merge pull request #100 from KenSuenobu/ticket-68
Browse files Browse the repository at this point in the history
Updated code to add job statistics.
  • Loading branch information
KenSuenobu committed Jun 17, 2018
2 parents 593ab50 + 2785f6c commit 894911c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
Expand Up @@ -41,6 +41,7 @@ import com.scattersphere.core.util.JobBuilder._
case class Job(id: Int, name: String, tasks: Seq[Task]) {

private var jobStatus: JobStatus = JobQueued
private val jobStatistics: JobStatistics = new JobStatistics

/** Sets the [[JobStatus]] for this job.
*
Expand All @@ -54,8 +55,41 @@ case class Job(id: Int, name: String, tasks: Seq[Task]) {
*/
def status: JobStatus = jobStatus

override def toString = s"Job{name=$name,jobStatus=$jobStatus,tasks=$tasks}"
def getStatistics(): JobStatistics = jobStatistics

override def toString = s"Job{name=$name,jobStatus=$jobStatus,statistics=$jobStatistics,tasks=$tasks}"

}

class JobStatistics {
private var timeStarted: Long = 0
private var timeEnded: Long = 0

/** Triggers the start of the job. */
def triggerStart(): Unit = if (timeStarted == 0) timeStarted = System.currentTimeMillis()

/** Triggers the end of the job. */
def triggerEnd(): Unit = if (timeEnded == 0) timeEnded = System.currentTimeMillis()

/** Retrieves the total runtime for the job.
*
* @return Runtime in milliseconds.
*/
def getRuntime(): Long = (timeEnded - timeStarted)

/** Retrieves the start time of the job.
*
* @return time in milliseconds
*/
def getStart(): Long = timeStarted

/** Retrieves the end time of the job.
*
* @return time in milliseconds
*/
def getEnd(): Long = timeEnded

override def toString: String = s"JobStatistics{timeStarted=$timeStarted,timeEnded=$timeEnded}"
}

/** A builder class that allows for functional construction of a [[Job]].
Expand Down
Expand Up @@ -106,7 +106,7 @@ case class Task(id: Int, name: String, task: RunnableTask, dependencies: Seq[Tas
def getStatistics(): TaskStatistics = taskStatistics

override def toString: String = s"Task{id=$id,name=$name,status=$taskStatus," +
s"dependencies=${dependencies.length},async=$async}"
s"dependencies=${dependencies.length},statistics=$taskStatistics,async=$async}"

}

Expand Down
Expand Up @@ -60,6 +60,10 @@ class JobExecutor(job: Job) extends LazyLogging {
job.setStatus(JobFinished)
}

job.getStatistics().triggerEnd()

logger.info(s"Job ${job.name} finished: elapsed time ${job.getStatistics().getRuntime()}ms")

executorService.shutdown
logger.trace("Execution service shut down.")
}
Expand Down Expand Up @@ -122,6 +126,7 @@ class JobExecutor(job: Job) extends LazyLogging {
throw new InvalidJobExecutionStateException("Called out of order - queue required.")
}

job.getStatistics().triggerStart()
job.setStatus(JobRunning)

if (isPaused) {
Expand Down
Expand Up @@ -99,6 +99,9 @@ class SimpleJobTest extends FlatSpec with Matchers with LazyLogging {
jobExec.blocking shouldBe true
jobExec.queue().run()
job1.status shouldBe JobFinished
assert(job1.getStatistics().getRuntime() > 0)
assert(job1.getStatistics().getStart() != 0)
assert(job1.getStatistics().getEnd() != 0)

runnableTask1.setVar shouldBe 1
runnableTask2.setVar shouldBe 2
Expand Down Expand Up @@ -191,6 +194,7 @@ class SimpleJobTest extends FlatSpec with Matchers with LazyLogging {
jobExec.blocking shouldBe true
jobExec.queue().run()
job1.status shouldBe JobFinished
assert(job1.getStatistics().getRuntime() > 0)

runnableTask1.setVar shouldBe 1
task1.status shouldBe TaskFinished
Expand All @@ -205,14 +209,14 @@ class SimpleJobTest extends FlatSpec with Matchers with LazyLogging {
jobExec2.blocking shouldBe true
jobExec2.queue().run()
job2.status match {
case JobFailed(_) => println("Job failed, expected.")
case JobFailed(_) => // Do nothing
case x => fail(s"Unexpected job status: $x")
}
assert(job2.id > job1.id)

task1.status match {
case TaskFailed(reason) => reason match {
case _: InvalidTaskStateException => println(s"Expected InvalidTaskStateException caught.")
case _: InvalidTaskStateException => // Do nothing
case x => fail(s"Unexpected exception $x caught.")
}

Expand Down

0 comments on commit 894911c

Please sign in to comment.