您的位置:首页 > 编程语言 > PHP开发

workerman与php框架结合使用的方法

2018-03-10 15:09 330 查看
主要是修改一下workerman里面 work类的一个方法 使workerman  支持 PHP yourfile.php   /Work/index start  的形式

BaseWork 继承workerman的Work 类 主要是为了重写parseCommand方法  重写其实就是修改了代码中标红的部分
上代码吧 简单明了
BaseWork类<?php
/**
* Created by PhpStorm.
* Date: 2018/3/9
* Time: 16:12
*/

namespace app\controller;

use Workerman\Worker;

class BaseWork extends Worker
{
/**
* Parse command.
* php yourfile.php start | stop | restart | reload | status [-d]
*
* @return void
*/
protected static function parseCommand()
{
if (static::$_OS !== 'linux') {
return;
}
global $argv;
// Check argv;
$start_file = $argv[0];
$available_commands = array(
'start',
'stop',
'restart',
'reload',
'status',
'connections',
);
$usage = "Usage: php yourfile.php {" . implode('|', $available_commands) . "} [-d]\n";
if (!isset($argv[2]) || !in_array($argv[2], $available_commands)) {
exit($usage);
}

// Get command.
$command = trim($argv[2]);
$command2 = isset($argv[3]) ? $argv[3] : '';


// Start command.
$mode = '';
if ($command === 'start') {
if ($command2 === '-d' || static::$daemonize) {
$mode = 'in DAEMON mode';
} else {
$mode = 'in DEBUG mode';
}
}
static::log("Workerman[$start_file] $command $mode");

// Get master process PID.
$master_pid = is_file(static::$pidFile) ? file_get_contents(static::$pidFile) : 0;
$master_is_alive = $master_pid && @posix_kill($master_pid, 0) && posix_getpid() != $master_pid;
// Master is still alive?
if ($master_is_alive) {
if ($command === 'start') {
static::log("Workerman[$start_file] already running");
exit;
}
} elseif ($command !== 'start' && $command !== 'restart') {
static::log("Workerman[$start_file] not run");
exit;
}

// execute command.
switch ($command) {
case 'start':
if ($command2 === '-d') {
static::$daemonize = true;
}
break;
case 'status':
while (1) {
if (is_file(static::$_statisticsFile)) {
@unlink(static::$_statisticsFile);
}
// Master process will send SIGUSR2 signal to all child processes.
posix_kill($master_pid, SIGUSR2);
// Sleep 1 second.
sleep(1);
// Clear terminal.
if ($command2 === '-d') {
echo "\33[H\33[2J\33(B\33[m";
}
// Echo status data.
echo static::formatStatusData();
if ($command2 !== '-d') {
exit(0);
}
echo "\nPress Ctrl+C to quit.\n\n";
}
exit(0);
case 'connections':
if (is_file(static::$_statisticsFile)) {
@unlink(static::$_statisticsFile);
}
// Master process will send SIGIO signal to all child processes.
posix_kill($master_pid, SIGIO);
// Waiting amoment.
usleep(500000);
// Display statisitcs data from a disk file.
@readfile(static::$_statisticsFile);
exit(0);
case 'restart':
case 'stop':
if ($command2 === '-g') {
static::$_gracefulStop = true;
$sig = SIGTERM;
static::log("Workerman[$start_file] is gracefully stoping ...");
} else {
static::$_gracefulStop = false;
$sig = SIGINT;
static::log("Workerman[$start_file] is stoping ...");
}
// Send stop signal to master process.
$master_pid && posix_kill($master_pid, $sig);
// Timeout.
$timeout = 5;
$start_time = time();
// Check master process is still alive?
while (1) {
$master_is_alive = $master_pid && posix_kill($master_pid, 0);
if ($master_is_alive) {
// Timeout?
if (!static::$_gracefulStop && time() - $start_time >= $timeout) {
static::log("Workerman[$start_file] stop fail");
exit;
}
// Waiting amoment.
usleep(10000);
continue;
}
// Stop success.
static::log("Workerman[$start_file] stop success");
if ($command === 'stop') {
exit(0);
}
if ($command2 === '-d') {
static::$daemonize = true;
}
break;
}
break;
case 'reload':
if($command2 === '-g'){
$sig = SIGQUIT;
}else{
$sig = SIGUSR1;
}
posix_kill($master_pid, $sig);
exit;
default :
exit($usage);
}
}

}启动workerman的work类<?php
/**
* Created by PhpStorm.
* Date: 2018/3/9
* Time: 14:35
*/
namespace app\controller;

class Work extends Controller
{

public function index()
{
// 创建一个Worker监听2345端口,使用http协议通讯
$http_worker = new BaseWork();

// 启动4个进程对外提供服务
$http_worker->count = 1;
// 接收到浏览器发送的数据时回复hello world给浏览器
$http_worker->onMessage = function($connection, $data)
{
// 向浏览器发送hello world
$connection->send('hello world');
};
// 运行worker
BaseWork::runAll();

}

这样在你的框架的根目录 就可以命令行模式执行了 
php index.php  /work start
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  workerman php 框架