Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * The abstract class which every cron job class needs to be extending for it
- * to function properly. Nothing here needs to be changed, but if you feel it's
- * required to do so, please contact the author first.
- *
- * @author Stefan van Beusekom
- * @version 1.1
- * @abstract
- */
- abstract class Callcenter_Model_Cron_Abstract {
- protected $_errorList = array();
- protected $_handleExecList = false;
- protected $_lastExecuted = false;
- protected $_startScript = false;
- protected $_endScript = false;
- protected $_executionSuccessful = false;
- /**
- * Set the time limit for script execution
- *
- * @author Stefan van Beusekom
- * @since 1.1
- */
- protected function setTimelimit() {
- $childClass = get_called_class();
- set_time_limit(($childClass::CRON_TIMEOUT_MINUTES * 60) + $childClass::CRON_TIMEOUT_SECONDS);
- return $this;
- }
- /**
- * Set the date and time string of when this cronjob was started
- *
- * @author Stefan van Beusekom
- * @since 1.1
- */
- protected function setStartScript() {
- $dateTime = new DateTime();
- $this->_startScript = $dateTime->format("Y-m-d H:i:s");
- return $this;
- }
- /**
- * Set the date and time string of when this cronjob ended
- *
- * @author Stefan van Beusekom
- * @since 1.1
- */
- protected function setEndScript() {
- $dateTime = new DateTime();
- $this->_endScript = $dateTime->format("Y-m-d H:i:s");
- return $this;
- }
- /**
- * Get the path to the cronjob folder
- *
- * @author Stefan van Beusekom
- * @since 1.0
- * @return string
- */
- protected function getCronPath($file = null) {
- return APPLICATION_PATH . "/../cron/" . $file;
- }
- /**
- * Add an error for the current cronjob
- *
- * @author Stefan van Beusekom
- * @since 1.0
- * @param string $error Error message
- */
- protected function addError($error) {
- array_push($this->_errorList, $error);
- }
- /**
- * Return a list of errors for the current cronjob
- *
- * @author Stefan van Beusekom
- * @since 1.0
- * @return array
- */
- protected function getErrors() {
- return $this->_errorList;
- }
- /**
- * Set the current timestamp of when this cronjob was last executed
- *
- * @author Stefan van Beusekom
- * @since 1.0
- */
- protected function setLastExecuted() {
- $this->_handleExecList = fopen($this->getCronPath(get_called_class() . ".cron"), "w+");
- $cronArray = fread($this->_handleExecList, filesize($this->getCronPath(get_called_class() . ".cron")));
- if (($cronArray) == '')
- $cronArray = array();
- else
- $cronArray = unserialize($cronArray);
- $this->_lastExecuted = time();
- $cronArray[get_called_class()] = $this->_lastExecuted;
- fwrite($this->_handleExecList, serialize($cronArray));
- fclose($this->_handleExecList);
- return $this;
- }
- /**
- * Get the timestamp of when this cronjob was last executed
- *
- * @author Stefan van Beusekom
- * @since 1.0
- * @return int|boolean
- */
- protected function getLastExecuted() {
- if (!$this->_lastExecuted) {
- if (file_exists($this->getCronPath(get_called_class() . ".cron"))) {
- $this->_handleExecList = fopen($this->getCronPath(get_called_class() . ".cron"), "r");
- $cronArray = fread($this->_handleExecList, filesize($this->getCronPath(get_called_class() . ".cron")));
- if (!$cronArray)
- $cronArray = array();
- else
- $cronArray = unserialize($cronArray);
- return (key_exists(get_called_class(), $cronArray)) ? $cronArray[get_called_class()] : 0;
- } else {
- return 0;
- }
- } else {
- return $this->_lastExecuted;
- }
- }
- /**
- * Return a true or false if the current cronjob is allowed to run based on
- * the timestamps.
- *
- * @author Stefan van Beusekom
- * @since 1.0
- * @return boolean
- */
- protected function allowExecute() {
- $childClass = get_called_class();
- $dateTimePre = new DateTime();
- $dateTimePre->setTimestamp($this->getLastExecuted());
- $dateTimePre->setTime($dateTimePre->format("H"), $dateTimePre->format("i"), 0);
- if ($childClass::CRON_INTERVAL_HOURS != 0)
- $dateTimePre->setTime($dateTimePre->format("H"), 0, 0);
- if ($childClass::CRON_INTERVAL_DAYS != 0)
- $dateTimePre->setTime(0, 0, 0);
- $currentSeconds = (time() - $dateTimePre->getTimestamp());
- $intervalSeconds = ($childClass::CRON_INTERVAL_DAYS * 24 * 60 * 60) + ($childClass::CRON_INTERVAL_HOURS * 60 * 60) + ($childClass::CRON_INTERVAL_MINUTES * 60);
- $curDateObj = new DateTime();
- $allDateObj = new DateTime($childClass::CRON_START_DATETIME);
- if ($curDateObj->getTimestamp() < $allDateObj->getTimestamp())
- return false;
- if ($currentSeconds >= $intervalSeconds)
- return true;
- else
- return false;
- }
- /**
- * Append the log with data pertaining to the execution, or failure thereof
- *
- * @author Stefan van Beusekom
- * @since 1.0
- */
- protected function addLog() {
- $fileHandle = fopen($this->getCronPath(get_called_class() . ".txt"), "a");
- $dateObjectStart = new DateTime($this->_startScript);
- $dateObjectEnd = new DateTime($this->_endScript);
- $datePrefix = "[" . $dateObjectStart->format("d-m-Y H:i:s") . "][" . $dateObjectEnd->format("d-m-Y H:i:s") . "] ";
- $logEntry = "";
- if ($this->isSuccessful()) {
- $logEntry .= $datePrefix . "Cronjob was completed successfully." . "\r\n";
- } else {
- $errorPrefix = str_pad("", strlen($datePrefix), " ", STR_PAD_LEFT);
- $logEntry .= $datePrefix . "Cronjob could not be completed. The following errors occured:" . "\r\n";
- foreach ($this->getErrors() as $error) {
- $logEntry .= $errorPrefix . $error . "\r\n";
- }
- }
- fwrite($fileHandle, $logEntry);
- fclose($fileHandle);
- }
- /**
- * Return a true or false if the current cronjob has been successful
- *
- * @author Stefan van Beusekom
- * @since 1.0
- * @return boolean
- */
- protected function isSuccessful() {
- return ((count($this->getErrors()) == 0) ? true : false);
- }
- /**
- * The on exit callback which will produce an error message despite an
- * uncatchable error being thrown.
- *
- * @author Stefan van Beusekom
- * @since 1.1
- * @todo Although this method logs the error, nothing can be done about its
- * ability to continue running other cronjobs. Search for possible
- * solution to this problem.
- * @return boolean
- */
- public function onExecute() {
- if (!$this->_executionSuccessful) {
- $errorMessage = "CRITICAL ERROR - Entire cron process was abruptly halted due to an uncatchable error. Please disable this class until the problem has been resolved. - No stack trace is available for debugging, but common errors are: faulty code, maximum execution time exceeded, or a server crash.";
- $this->addError($errorMessage);
- $this->addLog();
- }
- }
- /**
- * Execute the cronjob
- *
- * @author Stefan van Beusekom
- * @since 1.0
- * @return boolean
- */
- public function execute() {
- register_shutdown_function(array($this, "onExecute"));
- $this->setTimelimit();
- if ($this->allowExecute()) {
- $this->setStartScript();
- try {
- $this->task();
- } catch (Exception $e) {
- $this->addError("The current task generated an exception, message: " . $e->getMessage());
- }
- if ($this->isSuccessful()) {
- $this->addLog();
- $this->setEndScript();
- $this->setLastExecuted();
- } else {
- $this->addLog();
- }
- $this->_executionSuccessful = true;
- return $this->isSuccessful();
- }
- $this->_executionSuccessful = true;
- return false;
- }
- /**
- * Task to be executed
- *
- * @author Stefan van Beusekom
- * @since 1.0
- */
- protected abstract function task();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment