您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“thinkphp6中如何使用workerman”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“thinkphp6中如何使用workerman”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。
thinkphp6整合workerman教程
thinkphp6安裝workerman命令:
composer require topthink/think-worker
第一步,創(chuàng)建一個(gè)自定義命令類文件,運(yùn)行指令。
php think make:command Spider spider
會(huì)生成一個(gè)app\command\Spider
命令行指令類,我們修改內(nèi)容如下:
<?php
namespace app\command;
// tp指令特性使用的功能
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;
// 引用項(xiàng)目的基類,該類繼承自worker
use app\server\controller\Start;
/**
* 指令類
* 在此定義指令
* 再次啟動(dòng)多個(gè)控制器
* @var mixed
*/
class Spider extends Command
{
/**
* 注冊模塊名稱
* 使用命令會(huì)啟動(dòng)該模塊控制器
* @var mixed
*/
public $model_name = 'server';
/**
* 注冊控制器名稱
* 使用命令啟動(dòng)相關(guān)控制器
* @var mixed
*/
public $controller_names = ['WebhookTimer'];
/**
* configure
* tp框架自定義指令特性
* 注冊命令參數(shù)
* @return mixed
*/
protected function configure()
{
$this->setName('spider')
->addArgument('status', Argument::OPTIONAL, "status")
->addArgument('controller_name', Argument::OPTIONAL, "controller_name/controller_name")
->addArgument('mode', Argument::OPTIONAL, "d")
->setDescription('spider control');
/**
* 以上設(shè)置命令格式為:php think spider [status] [controller_name/controller_name] [d]
* think 為thinkphp框架入口文件
* spider 為在框架中注冊的命令,上面setName設(shè)置的
* staus 為workerman框架接受的命令
* controller_name/controller_name 為控制器名稱,以正斜線分割,執(zhí)行制定控制器,為空或缺省則啟動(dòng)所有控制器,控制器列表在controller_name屬性中注冊
* d 最后一個(gè)參數(shù)為wokerman支持的-d-g參數(shù),但是不用加-,直接使用d或者g
* php think spider start collect/SendMsg
*/
}
/**
* execute
* tp框架自定義指令特性
* 執(zhí)行命令后的邏輯
* @param mixed $input
* @param mixed $output
* @return mixed
*/
protected function execute(Input $input, Output $output)
{
//獲得status參數(shù),即think自定義指令中的第一個(gè)參數(shù),缺省報(bào)錯(cuò)
$status = $input->getArgument('status');
if(!$status){
$output->writeln('pelase input control command , like start');
exit;
}
//獲得控制器名稱
$controller_str = $input->getArgument('controller_name');
//獲得模式,d為wokerman的后臺(tái)模式(生產(chǎn)環(huán)境)
$mode = $input->getArgument('mode');
//分析控制器參數(shù),如果缺省或?yàn)閍ll,那么運(yùn)行所有注冊的控制器
$controller_list = $this->controller_names;
if($controller_str != '' && $controller_str != 'all' )
{
$controller_list = explode('/',$controller_str);
}
//重寫mode參數(shù),改為wokerman接受的參數(shù)
if($mode == 'd'){
$mode = '-d';
}
if($mode == 'g'){
$mode = '-g';
}
//將wokerman需要的參數(shù)傳入到其parseCommand方法中,此方法在start類中重寫
Start::$argvs = [
'think',
$status,
$mode
];
$output->writeln('start running spider');
$programs_ob_list = [];
//實(shí)例化需要運(yùn)行的控制器
foreach ($controller_list as $c_key => $controller_name) {
$class_name = 'app\\'.$this->model_name.'\controller\\'.$controller_name;
$programs_ob_list[] = new $class_name();
}
//將控制器的相關(guān)回調(diào)參數(shù)傳到workerman中
foreach (['onWorkerStart', 'onConnect', 'onMessage', 'onClose', 'onError', 'onBufferFull', 'onBufferDrain', 'onWorkerStop', 'onWorkerReload'] as $event) {
foreach ($programs_ob_list as $p_key => $program_ob) {
if (method_exists($program_ob, $event)) {
$programs_ob_list[$p_key]->$event = [$program_ob,$event];
}
}
}
Start::runAll();
}
}
例如我們創(chuàng)建一個(gè)定時(shí)器的命令app\server\controller創(chuàng)建WebhookTimer.php
<?php
namespace app\server\controller;
use Workerman\Worker;
use \Workerman\Lib\Timer;
use think\facade\Cache;
use think\facade\Db;
use think\Request;
class WebhookTimer extends Start
{
public $host = '0.0.0.0';
public $port = '9527';
public $name = 'webhook';
public $count = 1;
public function onWorkerStart($worker)
{
Timer::add(2, array($this, 'webhooks'), array(), true);
}
public function onConnect()
{
}
public function onMessage($ws_connection, $message)
{
}
public function onClose()
{
}
public function webhooks()
{
echo 11;
}
}
執(zhí)行start命令行
php think spider start
執(zhí)行stop命令
php think spider stop
執(zhí)行全部進(jìn)程命令
php think spider start all d
在app\command\Spider.php文件
public $controller_names = ['WebhookTimer','其他方法','其他方法'];
其他方法 就是app\server\controller下創(chuàng)建的其他類文件方法
完結(jié)
Start.php文件
<?php
namespace app\server\controller;
use Workerman\Worker;
class Start extends Worker
{
public static $argvs = [];
public static $workerHost;
public $socket = '';
public $protocol = 'http';
public $host = '0.0.0.0';
public $port = '2346';
public $context = [];
public function __construct()
{
self::$workerHost = parent::__construct($this->socket ?: $this->protocol . '://' . $this->host . ':' . $this->port, $this->context);
}
/**
* parseCommand
* 重寫wokerman的解析命令方法
* @return mixed
*/
public static function parseCommand()
{
if (static::$_OS !== OS_TYPE_LINUX) {
return;
}
// static::$argvs;
// Check static::$argvs;
$start_file = static::$argvs[0];
$available_commands = array(
'start',
'stop',
'restart',
'reload',
'status',
'connections',
);
$usage = "Usage: php yourfile <command> [mode]\nCommands: \nstart\t\tStart worker in DEBUG mode.\n\t\tUse mode -d to start in DAEMON mode.\nstop\t\tStop worker.\n\t\tUse mode -g to stop gracefully.\nrestart\t\tRestart workers.\n\t\tUse mode -d to start in DAEMON mode.\n\t\tUse mode -g to stop gracefully.\nreload\t\tReload codes.\n\t\tUse mode -g to reload gracefully.\nstatus\t\tGet worker status.\n\t\tUse mode -d to show live status.\nconnections\tGet worker connections.\n";
if (!isset(static::$argvs[1]) || !in_array(static::$argvs[1], $available_commands)) {
if (isset(static::$argvs[1])) {
static::safeEcho('Unknown command: ' . static::$argvs[1] . "\n");
}
exit($usage);
}
// Get command.
$command = trim(static::$argvs[1]);
$command2 = isset(static::$argvs[2]) ? static::$argvs[2] : '';
// 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') {
static::safeEcho("\33[H\33[2J\33(B\33[m", true);
}
// Echo status data.
static::safeEcho(static::formatStatusData());
if ($command2 !== '-d') {
exit(0);
}
static::safeEcho("\nPress Ctrl+C to quit.\n\n");
}
exit(0);
case 'connections':
if (is_file(static::$_statisticsFile) && is_writable(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.
if(is_readable(static::$_statisticsFile)) {
readfile(static::$_statisticsFile);
}
exit(0);
case 'restart':
case 'stop':
if ($command2 === '-g') {
static::$_gracefulStop = true;
$sig = SIGTERM;
static::log("Workerman[$start_file] is gracefully stopping ...");
} else {
static::$_gracefulStop = false;
$sig = SIGINT;
static::log("Workerman[$start_file] is stopping ...");
}
// 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 :
if (isset($command)) {
static::safeEcho('Unknown command: ' . $command . "\n");
}
exit($usage);
}
}
}
讀到這里,這篇“thinkphp6中如何使用workerman”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。