Example #1
0
 protected static function initialization($cli = FALSE)
 {
     $hasFile = FALSE;
     //命令行模式,使用-c指定的配置文件
     if ($cli) {
         self::$options = getopt('c:f');
         if (isset(self::$options['c']) && is_file(self::$options['c'])) {
             require_once self::$options['c'];
             $hasFile = TRUE;
         }
     } else {
         foreach (self::$projects as $project => $val) {
             $file = $val['dir'] . '/config.php';
             if (is_file($file)) {
                 require_once $file;
                 $hasFile = TRUE;
                 break;
             }
         }
     }
     if (!$hasFile) {
         exit('Please set the config file.' . "\n");
     }
     //写到全局config对象
     isset($conf) && config()->set('conf', $conf);
     //设置数据库
     isset($conf['databases']) && db_config($conf['databases']);
     //设置缓存
     isset($conf['caches']) && cache_config($conf['caches']);
     //设置缓存
     isset($conf['loggers']) && logger_config($conf['loggers']);
 }
Example #2
0
 public function runWorker($pid = 0)
 {
     $worker = new Worker();
     $worker->setTimeout(5000);
     if (!empty($this->servers) && is_array($this->servers)) {
         $worker->setServers($this->servers);
     } else {
         $worker->setServer();
     }
     $functions = \GearmanKernel::getFunctions(null, $this->prefix);
     if (empty($functions)) {
         $this->output("Worker has no functions");
         return false;
     }
     $worker->addFunctions($functions, array('GearmanKernel', 'responseGearman'));
     while (!$this->stop) {
         //@notice GearmanWork的work()默认是阻塞,所以上面设置了timeout
         if ($worker->work()) {
             switch ($worker->returnCode()) {
                 case GEARMAN_SUCCESS:
                 case GEARMAN_IO_WAIT:
                 case GEARMAN_NO_JOBS:
                     break;
                 case GEARMAN_NO_ACTIVE_FDS:
                     sleep(2);
                     break;
                 case GEARMAN_NOT_CONNECTED:
                 case GEARMAN_COULD_NOT_CONNECT:
                 case GEARMAN_LOST_CONNECTION:
                     $this->output("Try to stop worker with pid {$pid} [not connected]");
                     $this->stop = true;
                     break;
                 default:
                     $this->output("Try to stop worker with pid {$pid} [unknown gearman runcode]");
                     $this->stop = true;
                     break;
             }
         }
         //超过最大运行时间则退出
         if ($this->maxRuntime > 0 && time() > $this->stopTime) {
             $this->output("Try to stop worker with pid {$pid} [out of maxRuntime]");
             $this->stop = true;
         }
     }
     $worker->unregisterAll();
 }