config = $config; $this->entityManager = $entityManager; } protected function getConfig() { return $this->config; } protected function getEntityManager() { return $this->entityManager; } /** * Get Pending Jobs * * @return array */ public function getPendingJobs() { /** Mark Failed old jobs and remove pending duplicates */ $this->markFailedJobs(); $this->removePendingJobDuplicates(); $jobList = $this->getActiveJobs(); $runningScheduledJobs = $this->getActiveJobs('scheduled_job_id', CronManager::RUNNING, PDO::FETCH_COLUMN); $list = array(); foreach ($jobList as $row) { if (!in_array($row['scheduled_job_id'], $runningScheduledJobs)) { $list[] = $row; } } return $list; } /** * Get active Jobs, which execution date in jobPeriod time * * @param string $displayColumns * @param string $status * * @return array */ public function getActiveJobs($displayColumns = '*', $status = CronManager::PENDING, $fetchMode = PDO::FETCH_ASSOC) { $jobConfigs = $this->getConfig()->get('cron'); $currentTime = time(); $periodTime = $currentTime - intval($jobConfigs['jobPeriod']); $limit = empty($jobConfigs['maxJobNumber']) ? '' : 'LIMIT '.$jobConfigs['maxJobNumber']; $query = "SELECT " . $displayColumns . " FROM job WHERE `status` = '" . $status . "' AND execute_time BETWEEN '".date('Y-m-d H:i:s', $periodTime)."' AND '".date('Y-m-d H:i:s', $currentTime)."' AND deleted = 0 ORDER BY execute_time ASC ".$limit; $pdo = $this->getEntityManager()->getPDO(); $sth = $pdo->prepare($query); $sth->execute(); $rows = $sth->fetchAll($fetchMode); return $rows; } /** * Get Jobs by ScheduledJobId and date * * @param string $scheduledJobId * @param string $date * * @return array */ public function getJobByScheduledJob($scheduledJobId, $date) { $query = "SELECT * FROM job WHERE scheduled_job_id = '".$scheduledJobId."' AND execute_time = '".$date."' AND deleted = 0 LIMIT 1"; $pdo = $this->getEntityManager()->getPDO(); $sth = $pdo->prepare($query); $sth->execute(); $scheduledJob = $sth->fetchAll(PDO::FETCH_ASSOC); return $scheduledJob; } /** * Mark pending jobs (all jobs that exceeded jobPeriod) * * @return void */ protected function markFailedJobs() { $jobConfigs = $this->getConfig()->get('cron'); $currentTime = time(); $periodTime = $currentTime - intval($jobConfigs['jobPeriod']); $update = "UPDATE job SET `status` = '" . CronManager::FAILED ."' WHERE (`status` = '" . CronManager::PENDING ."' OR `status` = '" . CronManager::RUNNING ."') AND execute_time < '".date('Y-m-d H:i:s', $periodTime)."' "; $pdo = $this->getEntityManager()->getPDO(); $sth = $pdo->prepare($update); $sth->execute(); } /** * Remove pending duplicate jobs, no need to run twice the same job * * @return void */ protected function removePendingJobDuplicates() { $duplicateJobs = $this->getActiveJobs('DISTINCT scheduled_job_id'); $pdo = $this->getEntityManager()->getPDO(); foreach ($duplicateJobs as $row) { if (!empty($row['scheduled_job_id'])) { /* no possibility to use limit in update or subqueries */ $query = "SELECT id FROM `job` WHERE scheduled_job_id = '" . $row['scheduled_job_id'] . "' AND `status` = '" . CronManager::PENDING ."' ORDER BY execute_time DESC LIMIT 1, 100000 "; $sth = $pdo->prepare($query); $sth->execute(); $jobIds = $sth->fetchAll(PDO::FETCH_COLUMN); $update = "UPDATE job SET deleted = 1 WHERE id IN ('". implode("', '", $jobIds)."') "; $sth = $pdo->prepare($update); $sth->execute(); } } } }