tp5注册swoole 自定义命令行,TP5使用swoole做异常任务示例代码

根据tp 介绍:
https://www.kancloud.cn/manual/thinkphp5/235129
我们注册一个swoole的自定义命令
php think swoser

服务端代码SwooleServ .php:

<?php
/**
 * swoole通用服务
 */

namespace app\home\swoole;


use app\console\common\ServerCommand;
use app\home\controller\AmazonExport;
use app\home\controller\AmazonEx;
use think\Log;
use think\console\Input;
use think\console\Output;

//继承Server基类
class SwooleServ extends ServerCommand
{
    
    

//实际业务的接口地址前缀
    private $swooleTaskPath = '\\app\home\\controller\\';
    const HOST = '127.0.0.1';
    const LIVE_PORT = 8811;
    const CHART_PORT = 8812;


    protected function execute(Input $input, Output $output)
    {
    
    
        $this->serv = new \Swoole\Server(self::HOST, self::LIVE_PORT);
        $this->serv->set(array(
            'worker_num' => 2,
            'daemonize' => 1,
            'max_request' => 10000,
            'task_worker_num' => 4,
            "task_ipc_mode " => 3,
        ));
        $this->serv->on('Receive', array($this, 'onReceive'));
        $this->serv->on('Task', array($this, 'onTask'));
        $this->serv->on('Finish', array($this, 'onFinish'));
        $this->serv->start();
    }

    protected function configure()
    {
    
    
        #此为tp5特有的cli命名模式
        $this->setName('swoser')->setDescription('swoole通用服务');
    }

    public function onReceive($serv, $fd, $from_id, $data)
    {
    
    


        $serv->task($data);
    }

    public function onTask($serv, $task_id, $src_worker_id, $data)
    {
    
    
        if (is_array($data)) {
    
    
            file_put_contents(ROOT_PATH . 'public/fba_record.log', date('Y-m-d H:i:s') . json_encode($data) . '1111' . PHP_EOL, 8);
        } else {
    
    
            file_put_contents(ROOT_PATH . 'public/fba_record.log', date('Y-m-d H:i:s') . $data . PHP_EOL, 8);
        }
        $data = trim($data);
        $data = json_decode($data, true);
        switch ($data['type']) {
    
    
            //异步汇出
            case 'AmazonExport':
                $className = $this->swooleTaskPath . $data['class'];
                $action = $data['action'];
                return call_user_func_array(array(
                    new $className(),
                    $action
                ), array($data['data']));
                break;

            case 'Test':
                $taskClass = $this->swooleTaskPath . 'wechat\\' . $data['type'] . 'Task';
                call_user_func_array(array(
                    new $taskClass(),
                    'index'
                ), array(
                    $serv,
                    $data['data']
                ));
                break;

            default:
                return [
                    'type' => 'undefided',
                    'status' => false
                ];
        }


    }

    public function onFinish($serv, $task_id, $data)
    {
    
    

    }
}

调用方式:

$redis = new Redis();
$redis->hSet('hash', $export_record_id, json_encode($params));
$data = [
   'type' => 'AmazonExport',
   'class' => 'AmazonEx',
   'action' => 'getExcelInfoDataMsg117',
   'data' => $export_record_id
];
\app\swoole\Client::$port = 8811;
$client = new \app\swoole\Client();
$client->connect();
$client->send(json_encode($data));
$client->close();

客户端代码Client.php

<?php

namespace app\swoole;

class Client
{
    
    
    private $client;
    //端口号
    static public $port = 9501;

    public function __construct()
    {
    
    
        $this->client = new \swoole_client(SWOOLE_SOCK_TCP);
    }

    public function connect()
    {
    
    
        if (!$this->client->connect("127.0.0.1", self::$port, 1)) {
    
    
            throw new \Exception(sprintf('Swoole Error: %s', $this->client->errCode));
        }
    }

    public function send($data)
    {
    
    
        if ($this->client->isConnected()) {
    
    
            if (!is_string($data)) {
    
    
                $data = json_encode($data);
            }

            return $this->client->send($data);
        } else {
    
    
            throw new Exception('Swoole Server does not connected.');
        }
    }

    public function close()
    {
    
    
        $this->client->close();
    }
}


在linux 中执行:php think swoser

在这里插入图片描述
这里一共有8个进程
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42433970/article/details/114880836
tp5
今日推荐