Skip to content

Commit

Permalink
#53 Create job with status and report when cases are merged
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Dec 13, 2016
1 parent d4bd9b2 commit 10375aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
7 changes: 4 additions & 3 deletions thehive-cortex/app/connectors/cortex/models/Job.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import play.api.libs.json.{ JsObject, JsValue, Json }

import org.elastic4play.JsonFormat.dateFormat
import org.elastic4play.models.{ AttributeDef, AttributeFormat F, AttributeOption O, BaseEntity, ChildModelDef, EntityDef, HiveEnumeration }
import org.elastic4play.utils.RichJson

import JsonFormat.jobStatusFormat
import models.{ Artifact, ArtifactModel }
Expand All @@ -35,9 +36,9 @@ trait JobAttributes { _: AttributeDef ⇒
class JobModel @Inject() (artifactModel: ArtifactModel) extends ChildModelDef[JobModel, Job, ArtifactModel, Artifact](artifactModel, "case_artifact_job") with JobAttributes with AuditedModel {

override def creationHook(parent: Option[BaseEntity], attrs: JsObject): Future[JsObject] = Future.successful {
attrs - "report" - "endDate" +
("startDate" Json.toJson(new Date)) +
("status" Json.toJson(JobStatus.InProgress))
attrs
.setIfAbsent("status", JobStatus.InProgress)
.setIfAbsent("startDate", new Date)
}
}
class Job(model: JobModel, attributes: JsObject) extends EntityDef[JobModel, Job](model, attributes) with JobAttributes {
Expand Down
10 changes: 5 additions & 5 deletions thehive-cortex/app/connectors/cortex/services/CortexSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class CortexSrv @Inject() (
implicit val system: ActorSystem,
implicit val mat: Materializer) {

lazy val log = Logger(getClass)
lazy val logger = Logger(getClass)

val mergeActor = actor(new Act {
become {
Expand Down Expand Up @@ -161,26 +161,26 @@ class CortexSrv @Inject() (
}

def updateJobWithCortex(jobId: String, cortexJobId: String, cortex: CortexClient)(implicit authContext: AuthContext): Unit = {
log.debug(s"Requesting status of job $cortexJobId in cortex ${cortex.name} in order to update job ${jobId}")
logger.debug(s"Requesting status of job $cortexJobId in cortex ${cortex.name} in order to update job ${jobId}")
cortex.waitReport(cortexJobId, 1.minute) andThen {
case Success(j)
val status = (j \ "status").asOpt[JobStatus.Type].getOrElse(JobStatus.Failure)
if (status == JobStatus.InProgress)
updateJobWithCortex(jobId, cortexJobId, cortex)
else {
val report = (j \ "report").asOpt[JsObject].getOrElse(JsObject(Nil)).toString
log.debug(s"Job $cortexJobId in cortex ${cortex.name} has finished with status $status, updating job ${jobId}")
logger.debug(s"Job $cortexJobId in cortex ${cortex.name} has finished with status $status, updating job ${jobId}")
val jobFields = Fields.empty
.set("status", status.toString)
.set("report", report)
.set("endDate", Json.toJson(new Date))
update(jobId, jobFields).onComplete {
case Failure(e) log.error(s"Update job fails", e)
case Failure(e) logger.error(s"Update job fails", e)
case _
}
}
case Failure(e)
log.debug(s"Request of status of job $cortexJobId in cortex ${cortex.name} fails, restarting ...")
logger.debug(s"Request of status of job $cortexJobId in cortex ${cortex.name} fails, restarting ...")
updateJobWithCortex(jobId, cortexJobId, cortex)
}
()
Expand Down

0 comments on commit 10375aa

Please sign in to comment.