Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function __construct($config)
 {
     parent::__construct($config);
     $this->onMessage = [$this, 'onMessage'];
     if (!$this->statDir) {
         $this->statDir = StatServer::$statDir;
     }
     if (!$this->logDir) {
         $this->logDir = StatServer::$logDir;
     }
 }
Ejemplo n.º 2
0
 /**
  * 运行
  *
  * @see Workerman.Worker::run()
  */
 public function run()
 {
     $this->_onWorkerStart = $this->onWorkerStart;
     $this->onWorkerStart = [$this, 'onWorkerStart'];
     $this->onMessage = [$this, 'onMessage'];
     parent::run();
 }
Ejemplo n.º 3
0
<?php

use tourze\Server\Worker;
// 检查扩展
if (!extension_loaded('pcntl')) {
    exit("Please install pcntl extension. See http://doc3.workerman.net/install/install.html\n");
}
if (!extension_loaded('posix')) {
    exit("Please install posix extension. See http://doc3.workerman.net/install/install.html\n");
}
require 'bootstrap.php';
Worker::load('web');
Worker::runAll();
Ejemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function __construct($config)
 {
     parent::__construct($config);
     $this->onMessage = [$this, 'onMessage'];
 }
Ejemplo n.º 5
0
 /**
  * 解析运行命令,输出更加好看的格式
  *
  * php start.php start | stop | restart | reload | status
  *
  */
 public static function parseCommand()
 {
     // 检查运行命令的参数
     global $argv;
     $startFile = $argv[0];
     if (!isset($argv[1])) {
         self::printUsage($startFile);
         exit;
     }
     // 命令
     $command = trim($argv[1]);
     // 子命令,目前只支持-d
     $command2 = isset($argv[2]) ? $argv[2] : '';
     // 记录日志
     $mode = '';
     if ($command === 'start') {
         if ($command2 === '-d') {
             $mode = 'in DAEMON mode';
         } else {
             $mode = 'in DEBUG mode';
         }
     }
     Base::getLog()->debug(__METHOD__ . ' parse and execute command', ['file' => $startFile, 'command' => $command, 'mode' => $mode]);
     // 检查主进程是否在运行
     $masterPid = @file_get_contents(self::$pidFile);
     Base::getLog()->debug(__METHOD__ . ' get master pid from pidFile', ['file' => self::$pidFile, 'pid' => $masterPid]);
     $masterIsAlive = $masterPid && @posix_kill($masterPid, 0);
     Base::getLog()->debug(__METHOD__ . ' if master is alive', ['alive' => $masterIsAlive]);
     if ($masterIsAlive) {
         if ($command === 'start') {
             Base::getLog()->debug(__METHOD__ . ' server is running', ['file' => $startFile]);
         }
     } elseif ($command !== 'start' && $command !== 'restart') {
         Base::getLog()->debug(__METHOD__ . ' server is not running', ['file' => $startFile]);
     }
     // 根据命令做相应处理
     switch ($command) {
         // 启动 workerman
         case 'start':
             if ($command2 === '-d') {
                 Worker::$daemonize = true;
             }
             break;
             // 显示 workerman 运行状态
         // 显示 workerman 运行状态
         case 'status':
             // 尝试删除统计文件,避免脏数据
             if (is_file(self::$statusFile)) {
                 Base::getLog()->debug(__METHOD__ . ' delete old status file', ['file' => self::$statusFile]);
                 @unlink(self::$statusFile);
             }
             // 向主进程发送 SIGUSR2 信号 ,然后主进程会向所有子进程发送 SIGUSR2 信号
             // 所有进程收到 SIGUSR2 信号后会向 $statusFile 写入自己的状态
             posix_kill($masterPid, SIGUSR2);
             // 睡眠100毫秒,等待子进程将自己的状态写入 $statusFile 指定的文件
             usleep(100000);
             // 展示状态
             if (!is_file(self::$statusFile)) {
                 exit("Status file is missing.\n");
             }
             readfile(self::$statusFile);
             exit(0);
             // 重启 workerman
         // 重启 workerman
         case 'restart':
             // 停止 workeran
         // 停止 workeran
         case 'stop':
             Base::getLog()->debug(__METHOD__ . ' stopping all services', ['file' => $startFile]);
             // 想主进程发送SIGINT信号,主进程会向所有子进程发送SIGINT信号
             $masterPid && posix_kill($masterPid, SIGINT);
             // 如果 $timeout 秒后主进程没有退出则展示失败界面
             $timeout = 5;
             $startTime = time();
             while (1) {
                 // 检查主进程是否存活
                 $masterIsAlive = $masterPid && posix_kill($masterPid, 0);
                 if ($masterIsAlive) {
                     // 检查是否超过$timeout时间
                     if (time() - $startTime >= $timeout) {
                         self::log("Workerman[{$startFile}] stop fail");
                         exit;
                     }
                     usleep(10000);
                     continue;
                 }
                 self::log("Workerman[{$startFile}] stop success");
                 // 是restart命令
                 if ($command === 'stop') {
                     exit(0);
                 }
                 // -d 说明是以守护进程的方式启动
                 if ($command2 === '-d') {
                     Worker::$daemonize = true;
                 }
                 break;
             }
             break;
             // 平滑重启 workerman
         // 平滑重启 workerman
         case 'reload':
             posix_kill($masterPid, SIGUSR1);
             self::log("Workerman[{$startFile}] reload");
             exit;
             // 未知命令
         // 未知命令
         default:
             self::printUsage($startFile);
             exit;
     }
 }