/** * Checks whether a job exists or not * A Job is uniquely identified by three parameters * userId, type, and rundate * @param ESJob $job */ public function jobExists(ESJob $job) { $params = array(); $params['index'] = $this->indexName; $params['type'] = $this->getJobType(); $date = $job->getRunDate()->format('Y-m-d'); $query = array('filtered' => array('filter' => array('and' => array(array("numeric_range" => array("runDate" => array('gte' => $date, 'lte' => $date))), array("term" => array("jobType" => $job->getJobType())), array("term" => array("userId" => $job->getUserId())))))); $params['body']['query'] = $query; $results = $this->esClient->search($params); return $results['hits']['total'] != 0; }
/** * Runs a particular job * @param ESJob $job * @return SchedulerJobStatus */ public function run(ESJob $job) { $logger = $this->getLogger(); $class = $job->getClass(); $jobId = $job->getId(); $logger->info("RUNNER: Running job with id {$jobId}"); // Check if the class exists if (!class_exists($job->getClass())) { $logger->error("Runner: Class {$class} not found for job with id {$jobId}", ESJob::getArrayFromObj($job)); return SchedulerJobStatus::getStatusObject(SchedulerJobStatus::SCHEDULERJOB_STATUS_CLASS_NOT_FOUND, "The class {$class} has not been found"); } $task = new $class(); $task->setContainer($this->container); $task->setJob($job); $task->setUp(); $status = $task->perform(json_decode($job->getArgs(), true)); $task->tearDown(); $logger->info("RUNNER: Job with id {$jobId} completed", array('status' => SchedulerJobStatus::$statuses[$status->getStatus()], 'message' => $status->getMessage())); return $status; }
/** * Saves the job in elastic search * @param \DateTime $date * @param $class * @param $arguments */ public function schedule(\DateTime $date, $type, $class, $arguments = array(), $userId = -1) { $logger = $this->container->get('monolog.logger.scheduler'); $indexer = $this->container->get('es_indexer'); $esScheduler = $this->container->get('es_scheduler'); $id = md5(uniqid('', true)); $job = new ESJob($id); $job->setRunDate($date); $job->setClass($class); $job->setArgs($arguments); $job->setJobType($type); $job->setUserId($userId); // Check if the job already exists if ($esScheduler->jobExists($job)) { $logger->info("SCHEDULER : job already exists", ESJob::getArrayFromObj($job)); return false; } else { $indexer->index($job); $logger->info("SCHEDULER : job created with id {$id}", ESJob::getArrayFromObj($job)); return $id; } }
private function getESResultArray(ESJob $job) { return array('_source' => ESJob::getArrayFromObj($job), '_id' => $job->getId()); }