From 5f95cc1d7cfb268576279446acf0c0956a61a38f Mon Sep 17 00:00:00 2001 From: Tanguy Pruvot Date: Sat, 29 Jul 2017 22:48:54 +0200 Subject: [PATCH] cron: create a class to handle console threads for now, only migrate loop2.sh screen task... todo: blocks.sh and main.sh after more tests --- web/loop2.sh | 6 +- web/runconsole.php | 29 ++++ web/yaamp/components/CYiimpConsoleApp.php | 188 ++++++++++++++++++++++ 3 files changed, 221 insertions(+), 2 deletions(-) create mode 100755 web/runconsole.php create mode 100644 web/yaamp/components/CYiimpConsoleApp.php diff --git a/web/loop2.sh b/web/loop2.sh index a77753e..b4728b1 100755 --- a/web/loop2.sh +++ b/web/loop2.sh @@ -1,10 +1,12 @@ #!/bin/bash -alias php5='php -d max_execution_time=120' +PHP_CLI='php -d max_execution_time=120' + +date cd /var/web while true; do - php5 run.php cronjob/runLoop2 + ${PHP_CLI} runconsole.php cronjob/runLoop2 sleep 60 done exec bash diff --git a/web/runconsole.php b/web/runconsole.php new file mode 100755 index 0000000..e4d3779 --- /dev/null +++ b/web/runconsole.php @@ -0,0 +1,29 @@ +runController($argv[1]); +} + +catch(CException $e) +{ + debuglog($e, 5); + + $message = $e->getMessage(); + echo "exception: $message\n"; +// send_email_alert('backend', "backend error", "$message"); +} + diff --git a/web/yaamp/components/CYiimpConsoleApp.php b/web/yaamp/components/CYiimpConsoleApp.php new file mode 100644 index 0000000..5e9f43e --- /dev/null +++ b/web/yaamp/components/CYiimpConsoleApp.php @@ -0,0 +1,188 @@ +getState/setState() and memcache + * + * php runconsole.php cronjob/runLoop2 + * will execute actionRunLoop2() from CronjobController.php + * + * @author Tanguy Pruvot + * @copyright 2017 YiiMP + */ + +class CYiimpConsoleApp extends CConsoleApplication +{ + private $_controllerPath; + private $_controller; + + public $defaultController = 'cronjob'; + + public $layoutPath; + public $layout; + public $viewPath; + public $systemViewPath; + + public $controllerMap=array(); + public $controllerNamespace; + + public $user; + public $memcache; + + protected function init() + { + parent::init(); + $this->_controllerPath = $this->getBasePath().DIRECTORY_SEPARATOR.'modules/thread'; + + $this->user = new CWebUser; // for user()->getState() + $this->memcache = new YaampMemcache; + } + + protected function registerCoreComponents() + { + parent::registerCoreComponents(); + $components = $this->getComponents(); + $components['user'] = array( + 'class'=>'CWebUser', + ); + $this->setComponents($components); + } + + public function createController($route,$owner=null) + { + if($owner===null) + $owner=$this; + if((array)$route===$route || ($route=trim($route,'/'))==='') + $route=$owner->defaultController; + $caseSensitive=$this->getUrlManager()->caseSensitive; + + $route.='/'; + while(($pos=strpos($route,'/'))!==false) + { + $id=substr($route,0,$pos); + if(!preg_match('/^\w+$/',$id)) + return null; + if(!$caseSensitive) + $id=strtolower($id); + $route=(string)substr($route,$pos+1); + if(!isset($basePath)) // first segment + { + if(isset($owner->controllerMap[$id])) + { + return array( + Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner), + $this->parseActionParams($route), + ); + } + + if(($module=$owner->getModule($id))!==null) + return $this->createController($route,$module); + + $basePath=$owner->getControllerPath(); + $controllerID=''; + } + else + $controllerID.='/'; + $className=ucfirst($id).'Controller'; + $classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php'; + + if($owner->controllerNamespace!==null) + $className=$owner->controllerNamespace.'\\'.str_replace('/','\\',$controllerID).$className; + + if(is_file($classFile)) + { + if(!class_exists($className,false)) + require($classFile); + if(class_exists($className,false) && is_subclass_of($className,'CController')) + { + $id[0]=strtolower($id[0]); + return array( + new $className($controllerID.$id,$owner===$this?null:$owner), + $this->parseActionParams($route), + ); + } + return null; + } + $controllerID.=$id; + $basePath.=DIRECTORY_SEPARATOR.$id; + } + } + + /** + * Creates the controller and performs the specified action. + * @param string $route the route of the current request. See {@link createController} for more details. + * @throws CException if the controller could not be created. + */ + public function runController($route) + { + if(($ca=$this->createController($route))!==null) + { + list($controller,$actionID)=$ca; + $oldController=$this->_controller; + $this->_controller=$controller; + $controller->init(); + $controller->run($actionID); + $this->_controller=$oldController; + } + else { + throw new CException(Yii::t('yii', 'Unable to resolve the request "{route}". (CYiimpConsoleApp)', + array('{route}'=>$route===''?$this->defaultController:$route))); + } + } + + // --------------------------------------------------------------------------------------------------------------------- + + /** + * Parses a path info into an action ID and GET variables. + * @param string $pathInfo path info + * @return string action ID + */ + protected function parseActionParams($pathInfo) + { + if(($pos=strpos($pathInfo,'/'))!==false) + { + $manager=$this->getUrlManager(); + $manager->parsePathInfo((string)substr($pathInfo,$pos+1)); + $actionID=substr($pathInfo,0,$pos); + return $manager->caseSensitive ? $actionID : strtolower($actionID); + } + else + return $pathInfo; + } + + + public function beforeControllerAction($controller,$action) + { + return true; + } + + public function afterControllerAction($controller,$action) + { + } + + // --------------------------------------------------------------------------------------------------------------------- + + /** + * @return string the directory that contains the controller classes. Defaults to 'protected/controllers'. + */ + public function getControllerPath() + { + if($this->_controllerPath!==null) + return $this->_controllerPath; + else + return $this->_controllerPath=$this->getBasePath().DIRECTORY_SEPARATOR.'controllers'; + } + + /** + * @param string $value the directory that contains the controller classes. + * @throws CException if the directory is invalid + */ + public function setControllerPath($value) + { + if(($this->_controllerPath=realpath($value))===false || !is_dir($this->_controllerPath)) + throw new CException(Yii::t('yii','The controller path "{path}" is not a valid directory.', + array('{path}'=>$value))); + } + +}