Example #1
0
 /**
  * 创建一个子进程
  * @param Worker $worker
  * @throws Exception
  */
 protected static function forkOneWorker($worker)
 {
     $pid = pcntl_fork();
     // 获得可用的id
     $id = self::getId($worker->workerId, 0);
     // 主进程记录子进程pid
     if ($pid > 0) {
         self::$_pidMap[$worker->workerId][$pid] = $pid;
         self::$_idMap[$worker->workerId][$id] = $pid;
     } elseif (0 === $pid) {
         // 设置自动加载根目录
         Autoloader::setRootPath($worker->_appInitPath);
         // 启动过程中尝试重定向标准输出
         if (self::$_status === self::STATUS_STARTING) {
             self::resetStd();
         }
         self::$_pidMap = array();
         self::$_workers = array($worker->workerId => $worker);
         Timer::delAll();
         self::setProcessTitle('WorkerClient: worker process  ' . $worker->name . ' ' . $worker->getSocketName());
         self::setProcessUser($worker->user);
         $worker->id = $id;
         $worker->run();
         exit(250);
     } else {
         throw new Exception("forkOneWorker fail");
     }
 }
Example #2
0
        $connection->close();
        return;
    }
    echo "auth ok\n";
    global $onMessage;
    $connection->onMessage = $onMessage;
    $onMessage($connection, null);
    //ugly but just working
};
// Emitted when new connection come
$worker->onConnect = function ($connection) {
    echo "connected to redis\n";
    global $redis_password;
    if ($redis_password) {
        $connection->send("AUTH {$redis_password}\n", true);
    } else {
        global $onLogin;
        $onLogin($connection, true);
        //no auth = success
    }
};
$worker->onMessage = $onLogin;
// Emitted when connection closed
$worker->onClose = function ($connection) {
    echo "Server Closed Connection, stop this worker.\n";
    //Master process will start a new worker process
    $connection->worker->stop();
};
// Run worker
Worker::runAll();