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
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
相关文章推荐
- Repeater使用:绑定时 结合 前台JS及后台共享方法
- 网络编辑器FCKeditor和网络公式编辑器WebEQ结合使用的方法
- TextBox和Calendar结合使用(使用ajax控件的简单方法)
- 海康威视采集卡结合opencv使用(两种方法)
- Jquery(Ajax)load使用方法 结合radio切换视图
- 面向对象方法和结构化方法比较,形式化方法的实际运用困难,及如何结合使用这三种
- 将 JavaScript 与 ASP.NET 2.0 结合使用的简便方法
- Ninject 在 Winform、 Asp.net MVC中结合EntityFramework的使用( 注入同一个DbContext的使用方法) - 原创
- 线程池 submit 方法结合 Callable Future 的使用
- 结合Github使用git管理代码的简便方法
- Mysql中DATE_SUB 使用方法结合查询一天内,一周内,一月内的信息实例讲解
- xmemcached与spring结合使用时连接超时问题解决方法(转)
- scoket应用描述-结合c与Per两种使用方法
- 润乾报表与google地图结合使用方法
- 记录:树形控件的一些使用方法(和sql server 结合显示)
- 海康威视采集卡结合opencv使用(两种方法)-转
- 网络编辑器FCKeditor和网络公式编辑器WebEQ结合使用的方法(转)
- JS 替换和时间插件的结合使用方法
- Repeater使用:绑定时 结合 前台JS及后台共享方法
- 使用AJAX的方法将TextBox控件和Calendar控件结合使用,点击TextBox后弹出Calendar控件