这是Laravel框架调用Workerman
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Workerman\Worker;
use PHPSocketIO\SocketIO;
use Config;
class Workerman extends Command
{
use ConfirmableTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'workerman:server {action} {-d|daemon=default}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Run Workerman Server.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
global $argv, $argc;
$action = $this->argument('action');
if (is_null($this->option('daemon'))) {
$argv = ;
$argc = 3;
} else {
$argv = ;
$argc = 2;
}
switch ($action) {
case 'start':
$port = Config::get('workerman.server.port');
$events = Config::get('workerman.events');
$server = new SocketIO($port);
foreach ($events as $event) {
new $event($server);
}
Worker::runAll();
break;
case 'stop':
Worker::stopAll();
break;
case 'status':
$this->info(Worker::getStatus());
break;
default:
$this->error($action . ' action does not exist, try one one thoses : start, stop, status');
break;
}
}
}
我用 php artisan workerman:server start -d 可以正常启动,但是用 php artisan workerman:server stop 却无法停掉workerman进程。
启动脚本里执行Worker::stopAll()并不能停止workerman,Worker::stopAll()只有在运行workerman的主进程里执行才有效,外部其它进程执行没有效果。
停止Workerman的流程是
1、找到Workerman主进程pid
2、给pid发送SIGINT信号(posix_kill(SIGINT, pid))
3、workerman主进程收到SIGINT信号后自身执行Worker::stopAll()完成服务停止。
这三个步骤workerman内部应封装好,调用流程是
1、设置$argv = 'stop';
2、运行Worker::runAll();