/** * Fetch HTML * * @param string $tpl * @param string $dir */ public function fetch($tpl = null, $dir = null) { try { if ($this->_cacheKey && empty($this->_cache)) { $this->initCache(); } if ($this->_cacheKey && $this->_cache && ($data = $this->_cache->get($this->_cacheKey))) { return $data; } $this->_init($this->_arg); foreach ($this->_update as $key => $value) { $this->view->{$key} = $value; } if (empty($tpl)) { $tpl = $this->defaultTemplate(); } if (empty($dir)) { $dir = Cola::config('_widgetsHome'); } $data = $this->view->fetch($tpl, $dir); if ($this->_cacheKey && $this->_cache) { $this->_cache->set($this->_cacheKey, $data); } } catch (Exception $e) { $data = ''; } return $data; }
/** * Constructor * */ protected function __construct() { $config['_class'] = array('Cola_Router' => COLA_DIR . '/Router.php', 'Cola_Model' => COLA_DIR . '/Model.php', 'Cola_View' => COLA_DIR . '/View.php', 'Cola_Controller' => COLA_DIR . '/Controller.php', 'Cola_Com' => COLA_DIR . '/Com.php', 'Cola_Com_Widget' => COLA_DIR . '/Com/Widget.php', 'Cola_Exception' => COLA_DIR . '/Exception.php'); self::$config = new Cola_Config($config); self::$_config =& self::$config->reference(); Cola::registerAutoload(); }
public static function cache($name = null) { if (is_array($name)) { return Cola_Com_Cache::factory($name); } if (empty($name)) { $name = '_cache'; } if ($cache = Cola::reg($name)) { return $cache; } $config = (array) Cola::config($name); $cache = Cola_Com_Cache::factory($config); Cola::reg($name, $cache); return $cache; }
public function generate(swoole_process $worker) { $job = $worker->sitemap_job; //先判断这个job是否为空; if (empty($job)) { exit(0); } //需要新的mysql连接 $config = (array) Cola::config()->get('_db') + array('adapter' => 'Mysql', 'params' => array()); $db = Cola_Com_Db::factory($config); $this->cola_db = $db; //取出keys $files = array_keys($job); foreach ($files as $file) { //open this file , w+;不存在就创建,并且truncate文件 $fileHandler = fopen($file, 'w+'); $this->writeHeader($fileHandler); $low_id = $job[$file]['low']; $high_id = $job[$file]['high']; // count,从low到high有多少条记录 $count = $this->countPic($low_id, $high_id); $times = $count / self::BUFFER_ROW; //如果times为0,也会运行一次$offset=0,可以达到目的 foreach (range(0, $times) as $index) { $offset = $index * self::BUFFER_ROW; $pics = $this->selectPics($offset, $low_id, $high_id); $buffer_string = $this->fetchView($pics); fwrite($fileHandler, $buffer_string); } $this->writeFooter($fileHandler); fflush($fileHandler); fclose($fileHandler); } }
public function runAction() { //get log id $pic_id = intval($this->getLogId()); //die(var_dump($pic_id)); //取得为标签图片(id, url) $workerNum = 5; $limit = 20; $totalCount = $this->countUntaggedPic($pic_id); $partTotalCount = $totalCount / $workerNum; $jobs = $this->calculateJob($totalCount, $workerNum, $pic_id); var_dump($jobs); if ($jobs) { foreach ($jobs as $index => $range) { $pid = pcntl_fork(); if (!$pid) { //子进程 $endId = $range['endId']; if (file_exists(ROOT_DIR . "/sbin/child_pic_id_{$index}.log")) { $pid_log_file = fopen("child_pic_id_{$index}.log", 'r+'); $startId = (int) file_get_contents(ROOT_DIR . "/sbin/child_pic_id_{$index}.log"); } else { //log不存在 $pid_log_file = fopen("child_pic_id_{$index}.log", 'w+'); $startId = $range['startId']; } //新建一个mysql连接,抛弃原来的 $config = (array) Cola::config()->get('_db') + array('adapter' => 'Mysql', 'params' => array()); $db = Cola_Com_Db::factory($config); $this->cola_db = $db; //$this->dbs["DB_".$index] = $db; $times = $partTotalCount / $limit + 1; for ($i = 0; $i < $times; $i++) { $offset = $i * $limit; $pics = $this->getUntaggedPicWithEnd($startId, $endId, $offset, $limit); foreach ($pics as $pic) { $id = $pic['id']; $pic_url = $this->getPicUrl($pic['orig_path']); $this->handle($pic_url, $id); } //log pic_id rewind($pid_log_file); fwrite($pid_log_file, $id); fflush($pid_log_file); } //子进程退出时需要关闭资源 $this->cola_db->close(); fclose($pid_log_file); echo "Child runs to end"; } } //父进程等待子进程结束 while (pcntl_waitpid(0, $status) != -1) { $status = pcntl_wexitstatus($status); echo "Child {$status} completed\n"; } } else { //没有jobs,不需要多进程 $times = $totalCount / $limit + 1; for ($i = 0; $i < $times; $i++) { $offset = $i * $limit; $pics = $this->getUntaggedPic($pic_id, $offset, $limit); foreach ($pics as $pic) { $id = $pic['id']; $pic_url = $this->getPicUrl($pic['orig_path']); $this->handle($pic_url, $id); } $this->logId($id); } } }
/** * View * * @param array $config * @return Cola_View */ protected function view($params = array()) { $params = (array) $params + array('basePath' => $this->_viewsHome) + (array) Cola::config('_view'); return $this->view = new Cola_View($params); }
/** * Widget * * @param string $name * @param array $data * @return Cola_Com_Widget */ public function widget($name, $data = null) { if (empty($this->_widgetsHome) && ($widgetsHome = Cola::config('_widgetsHome'))) { $this->_widgetsHome = $widgetsHome; } $class = ucfirst($name) . 'Widget'; if (!Cola::loadClass($class, $this->_widgetsHome)) { throw new Cola_Exception("Can not find widget:{$class}"); } $widget = new $class($data); return $widget; }
public function push(swoole_process $worker){ $job = null; if(isset($worker->sitemap_job)){ $job = $worker->sitemap_job; } //先判断这个job是否为空; if(empty($job)){ exit(0); } //需要新的mysql连接 $config = (array)Cola::config()->get('_db') + array('adapter' => 'Mysql', 'params' => array()); $db = Cola_Com_Db::factory($config); $this->cola_db = $db; //取出keys $files = array_keys($job); foreach($files as $file){ $low_id = $job[$file]['low']; $high_id = $job[$file]['high']; // count,从low到high有多少条记录 $count = $this->countPic($low_id, $high_id); $times = $count/self::BUFFER_ROW; //如果times为0,也会运行一次$offset=0,可以达到目的 foreach(range(0, $times) as $index){ $offset = $index*self::BUFFER_ROW; $pics = $this->selectPics($offset, $low_id, $high_id); $this->pushToBaidu($pics); } } }
/** * Instantiated model * * @param string $name * @param string $dir * @return Cola_Model */ protected function model($name, $dir = null) { null === $dir && ($dir = Cola::config('_modelsHome')); $class = ucfirst($name) . 'Model'; if (Cola::loadClass($class, $dir)) { return new $class(); } throw new exception("Can't load model '{$class}' from '{$dir}'"); }