thinkPHP6安装workerman命令: composer require topthink/think-worker 第一步,创建一个自定义命令类文件,运行指令 php think make:command Spider spide
thinkPHP6安装workerman命令:
composer require topthink/think-worker
第一步,创建一个自定义命令类文件,运行指令
php think make:command Spider spider
会生成一个app\command\Spider
命令行指令类,我们修改内容如下:
setName('spider') ->addArgument('status', Argument::OPTIONAL, "status") ->addArgument('controller_name', Argument::OPTIONAL, "controller_name/controller_name") ->addArgument('mode', Argument::OPTIONAL, "d") ->setDescription('spider control'); } protected function execute(Input $input, Output $output) { //获得status参数,即think自定义指令中的第一个参数,缺省报错 $status = $input->getArgument('status'); if(!$status){ $output->writeln('pelase input control command , like start'); exit; } //获得控制器名称 $controller_str = $input->getArgument('controller_name'); //获得模式,d为wokerman的后台模式(生产环境) $mode = $input->getArgument('mode'); //分析控制器参数,如果缺省或为all,那么运行所有注册的控制器 $controller_list = $this->controller_names; if($controller_str != '' && $controller_str != 'all' ) { $controller_list = explode('/',$controller_str); } //重写mode参数,改为wokerman接受的参数 if($mode == 'd'){ $mode = '-d'; } if($mode == 'g'){ $mode = '-g'; } //将wokerman需要的参数传入到其parseCommand方法中,此方法在start类中重写 Start::$argvs = [ 'think', $status, $mode ]; $output->writeln('start running spider'); $programs_ob_list = []; //实例化需要运行的控制器 foreach ($controller_list as $c_key => $controller_name) { $class_name = 'app\\'.$this->model_name.'\controller\\'.$controller_name; $programs_ob_list[] = new $class_name(); } //将控制器的相关回调参数传到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(); }}
例如我们创建一个定时器的命令app\server\controller创建WEBhookTimer.php
ion 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; } }
执行start命令行
php think spider start
执行stop命令
php think spider stop
执行全部进程命令
php think spider start all d
在app\command\Spider.php文件
public $controller_names = ['WebhookTimer','其他方法','其他方法'];
其他方法 就是app\server\controller下创建的其他类文件方法
完结
Start.php文件
socket ?: $this->protocol . '://' . $this->host . ':' . $this->port, $this->context); } 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 [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); } }}
来源地址:https://blog.csdn.net/weixin_37521589/article/details/123484929
--结束END--
本文标题: thinkphp6整合workerman教程
本文链接: https://lsjlt.com/news/390991.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0