Example #1
0
 /**
  * Função createUpdateJob
  *
  * Creates a new job in the database, if the parameter \$jobID is filled, the Job's content will be updated.
  *
  * @author Henrique Ramos <*****@*****.**>
  * @version 0.2.0
  * @since 0.1.0
  *
  * @param array[] $params F3 parameters array.
  *
  * @throws Exception If _POST parameters job_name, job_cron, job_type, job_path aren't filled or are invalid
  * @throws Exception If can't add the Job in the database
  *
  * @return array[]
  */
 public function createUpdateJob($params)
 {
     global $connectDB, $logger;
     try {
         $jobID = (!empty($params['jobID']) and is_numeric($params['jobID'])) ? $params['jobID'] : NULL;
         $job_author = 1;
         //Melhorar isso
         if (empty($_POST['job_name'])) {
             throw new \Exception(__("You need a valid name for this job."));
         }
         if (empty($_POST['job_cron'])) {
             throw new \Exception(__("We need the Cron filled."));
         }
         if (!\Cron\CronExpression::isValidExpression($_POST['job_cron'])) {
             throw new \Exception(__("We need a valid Cron syntax."));
         }
         if (empty($_POST['job_type']) or !in_array($_POST['job_type'], array('internal', 'external'))) {
             throw new \Exception(__("We need the job type filled.  Choose between \"internal\" and \"external\"."));
         }
         if (empty($_POST['job_path'])) {
             throw new \Exception(__("We need the job path filled."));
         }
         if (empty($_POST['job_status']) or !in_array($_POST['job_status'], array('active', 'disable'))) {
             throw new \Exception(__("We need the job type filled.  Choose between \"active\" and \"disable\"."));
         }
         $alertArray = $_POST['alerts'];
         if (!empty($alertArray) and !is_array($alertArray)) {
             throw new \Exception(__("Fill the \"alerts\" parameter correctly."));
         }
         $job_comment = !empty($_POST['job_comment']) ? $_POST['job_comment'] : NULL;
         $job_status = $_POST['job_status'] == 'active' ? 1 : 0;
         $job_cron = $_POST['job_cron'];
         $job_group = 1;
         //Groups::checkIfGroupExists($_POST['job_group'])?$_POST['job_group']:NULL;
         $timestamp = time();
         $connectDB->beginTransaction();
         $insertJobDatabase = $connectDB->prepare("INSERT INTO `jobs`(`job_id`, `job_name`, `job_date`, `job_cron`, `job_path`, `job_type`, `job_author`, `job_status`, `job_comment`, `job_group`, `is_running`) VALUES (:job_id,:job_name, FROM_UNIXTIME(:job_date),:job_cron,:job_path,:job_type,:job_author,:job_status,:job_comment,:job_group,0) ON DUPLICATE KEY UPDATE `job_name`=:job_name, `job_cron`=:job_cron, `job_path`=:job_path, `job_type`=:job_type, `job_status`=:job_status, `job_comment`=:job_comment, `job_group`=:job_group");
         $insertJobDatabase->bindValue(":job_id", $jobID, \PDO::PARAM_STR);
         $insertJobDatabase->bindValue(":job_name", $_POST['job_name'], \PDO::PARAM_STR);
         $insertJobDatabase->bindValue(":job_date", $timestamp, \PDO::PARAM_INT);
         $insertJobDatabase->bindValue(":job_cron", $job_cron, \PDO::PARAM_STR);
         $insertJobDatabase->bindValue(":job_path", $_POST['job_path'], \PDO::PARAM_STR);
         $insertJobDatabase->bindValue(":job_type", $_POST['job_type'], \PDO::PARAM_STR);
         $insertJobDatabase->bindValue(":job_author", $job_author, \PDO::PARAM_INT);
         $insertJobDatabase->bindValue(":job_status", $job_status, \PDO::PARAM_STR);
         $insertJobDatabase->bindValue(":job_comment", $job_comment, \PDO::PARAM_STR);
         $insertJobDatabase->bindValue(":job_group", $job_group, \PDO::PARAM_STR);
         if (!$insertJobDatabase->execute()) {
             $logger->addError(sprintf(__("MySQL Error %s"), var_Export($insertJobDatabase->errorInfo(), true)));
             throw new \Exception(sprintf(__("Problems with database insertion. Try again later. MySQL Error %s"), $insertJobDatabase->errorCode()));
         }
         $JobIDInfo = $connectDB->lastInsertId();
         foreach ($alertArray as $alertIndividual) {
             $alert = new Alert();
             $alert->addUpdateAlert($JobIDInfo, $alertIndividual);
         }
         $logger->addError(sprintf(__("About Job: {id: \"%s\" name: \"%s\", timestamp: \"%s\", path: \"%s\"}"), $JobIDInfo, $_POST['job_name'], $timestamp, $_POST['job_path']));
         //Inserir no crontab
         switch ($_POST['job_type']) {
             case 'external':
                 $logger->addInfo(sprintf(__("External Job %s"), ExternalCrawlerPath));
                 $pathToCallingJob = ExternalCrawlerPath;
                 break;
             case 'internal':
             default:
                 $logger->addInfo(sprintf(__("Internal Job %s"), InternalCrawlerPath));
                 $pathToCallingJob = InternalCrawlerPath;
                 break;
         }
         $logger->addInfo(sprintf(__("Job Info %s"), var_export($JobInfo, true)));
         $logger->addInfo(sprintf(__("CRON Expression %s"), $job_cron));
         $logger->addInfo(sprintf(__("CRON Log %s"), Zeus_Monitor_Log_Path));
         $logger->addInfo(sprintf(__("Path to call job %s"), $pathToCallingJob));
         $cronParser = \Cron\CronExpression::factory($job_cron);
         $cronMinute = !empty($cronParser->getExpression(1)) ? $cronParser->getExpression(1) : "*";
         $cronHour = !empty($cronParser->getExpression(2)) ? $cronParser->getExpression(2) : "*";
         $cronDayMonth = !empty($cronParser->getExpression(3)) ? $cronParser->getExpression(3) : "*";
         $cronMonth = !empty($cronParser->getExpression(4)) ? $cronParser->getExpression(4) : "*";
         $cronDayWeek = !empty($cronParser->getExpression(5)) ? $cronParser->getExpression(5) : "*";
         $addToCron = new \MyBuilder\Cronos\Formatter\Cron();
         $addToCron->comment(sprintf("Job %s", addslashes(htmlspecialchars($_POST['job_name']))))->job("php {$pathToCallingJob} {$JobIDInfo}")->setMinute($cronMinute)->setHour($cronHour)->setDayOfMonth($cronDayMonth)->setMonth($cronMonth)->setDayOfWeek($cronDayWeek)->setStandardOutFile('log')->appendStandardErrorToFile(Zeus_Monitor_Log_Path)->end();
         $connectDB->commit();
         if (is_null($jobID)) {
             return json_encode(array('success' => sprintf(__("Job inserted with success.<br />Please, insert this code inside your cron:<br /><code>%s</code>"), nl2br($addToCron->format()))));
         }
         return json_encode(array('success' => sprintf(__("Job updated with success.<br />Please, insert this code inside your cron:<br /><code>%s</code>"), nl2br($addToCron->format()))));
     } catch (Exception $e) {
         $connectDB->rollBack();
         throw $e;
     }
 }
Example #2
0
 /**
  * Função visualizarJobs / showJobs
  *
  * Visualiza todos os jobs do banco de dados, se for passado um parâmetro jobID, retornará as informações desse job
  *
  * @author Henrique Ramos <*****@*****.**>
  * @version 0.1.0
  * @since 0.1.0
  *
  * @param array[] $params Array com parâmetros passados através do F3.
  *
  * @throws Exception Senão encontrar nenhum resultado no banco de dados
  *
  * @return array[]
  */
 public function showJobs($params = NULL)
 {
     global $connectDB, $logger;
     try {
         if (empty($params)) {
             throw new \Exception(__("Fill the \$params parameter."));
         }
         if (empty($params['jobID']) or !is_numeric($params['jobID'])) {
             $jobsSQL = $connectDB->query("SELECT `j`.`job_id`, `j`.`job_name`, `j`.`job_date`, `j`.`job_cron`, `j`.`job_path`, `j`.`job_type`, `j`.`job_author`, `j`.`job_status`, `j`.`job_comment` FROM `jobs` AS `j`", \PDO::FETCH_ASSOC);
             if ($jobsSQL->rowCount() == 0) {
                 if ($jobsSQL->errorCode() != 00) {
                     $logger->addError(sprintf(__("MySQL Error when we tried to retrieve jobs\r\n#############################\r\nError#############################\r\n%s\r\n#############################\r\n\r\n"), var_export($jobsSQL->errorInfo(), true)));
                 }
                 throw new \Exception(__("Not found any job matching this parameters."));
             }
             return array('jobs' => $jobsSQL->fetchAll());
         }
         $jobID = $params['jobID'];
         $jobsSQL = $connectDB->prepare("SELECT `j`.`job_id`, `j`.`job_name`, `j`.`job_date`, `j`.`job_cron`, `j`.`job_path`, `j`.`job_type`, `j`.`job_author`, `j`.`job_status`, `j`.`job_comment` FROM `jobs` AS `j` WHERE `j`.`job_id`=:jobID");
         $jobsSQL->bindValue(":jobID", $jobID, \PDO::PARAM_INT);
         $jobsSQL->execute();
         if ($jobsSQL->errorCode() != 00) {
             $logger->addError(sprintf(__("MySQL Error when we tried to retrieve job %s\r\n#############################\r\nError#############################\r\n%s\r\n#############################\r\n\r\n"), $jobID, var_export($jobsSQL->errorInfo(), true)));
             throw new \Exception(__("Not found any job matching this parameters."));
         }
         $finalArray = array('job' => $jobsSQL->fetch(\PDO::FETCH_ASSOC));
         $alert = new \Ramos\Zeus\Alert();
         $finalArray = array_merge($finalArray['job'], array('alerts' => $alert->viewAlertsByJob($jobID)));
         return $finalArray;
     } catch (Exception $e) {
         throw $e;
     }
 }