2017 © Pedro Peláez
 

yii2-extension yii2-swoole-async

yii2异步任务扩展

image

mevyen/yii2-swoole-async

yii2异步任务扩展

  • Tuesday, August 1, 2017
  • by mevyen
  • Repository
  • 4 Watchers
  • 46 Stars
  • 171 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 11 Forks
  • 0 Open issues
  • 3 Versions
  • 16 % Grown

The README.md

yii2 Swoole Async

yii2异步任务扩展:基于swoole和yii2-console的异步任务实现,配置简洁,与Yii2无缝集成,只需要安装swoole扩展即可使用, (*1)

swoole版本要求:>=1.8.1, (*2)

实现原理

1、客户端发送异步console任务执行请求, (*3)

2、服务端响应请求,并立即返回, (*4)

3、服务端发起task执行任务,执行请求携带的所有任务, (*5)

适用场景

需要客户端触发的耗时请求,客户端无需等待返回结果, (*6)

安装

composer require mevyen/yii2-swoole-async "dev-master"

如何使用

1、新增配置文件params_swoole.php, (*7)

return [
    'swooleAsync' => [
        'host'             => '127.0.0.1',      //服务启动IP
        'port'             => '9512',           //服务启动端口
        'process_name'     => 'swooleServ',     //服务进程名
        'open_tcp_nodelay' => '1',              //启用open_tcp_nodelay
        'daemonize'        => '1',              //守护进程化
        'worker_num'       => '2',              //work进程数目
        'task_worker_num'  => '2',              //task进程的数量
        'task_max_request' => '10000',          //work进程最大处理的请求数
        'task_tmpdir'      => '/tmp/task',       //设置task的数据临时目录
        'log_file'         => '/tmp/log/http.log', //指定swoole错误日志文件
        'client_timeout'   => '20',              //client链接服务器时超时时间(s)
        'pidfile'          => '/tmp/y.pid',          //服务启动进程id文件保存位置

        //--以上配置项均来自swoole-server的同名配置,可随意参考swoole-server配置说明自主增删--
        'log_size'         => 204800000,             //运行时日志 单个文件大小
        'log_dir'          => '/tmp/log',            //运行时日志 存放目录
    ]
];

2、在console主配置文件(main.php)中增加controllerMap, (*8)

'controllerMap' => [
    'swooleasync' => [
        'class' => 'mevyen\swooleAsync\SwooleAsyncController',
    ],
],

3、在站点主配置文件(main.php)中增加components, (*9)

'components' => [
    'swooleasync' => [
        'class' => 'mevyen\swooleAsync\SwooleAsyncComponent',
    ]
]

4、服务管理, (*10)

//启动
/path/to/yii/application/yii swooleasync/run start

//重启
/path/to/yii/application/yii swooleasync/run restart

//停止
/path/to/yii/application/yii swooleasync/run stop

//查看状态
/path/to/yii/application/yii swooleasync/run stats

//查看进程列表
/path/to/yii/application/yii swooleasync/run list

5、执行任务, (*11)

namespace console\controllers;
use yii\console\Controller;

class TestController extends Controller 
{  
    public function actionSwooleasync(){
        $data = [
            "data":[
                [
                    "a" => "test/mail",
                    "p" => ["测试邮件1","测试邮件2"]
                ],
                ...
            ],
            "finish" => [
                [
                    "a" => "test/mail",
                    "p" => ["测试邮件回调1","测试邮件回调2"]
                ],
                ...
            ]
        ];
        \Yii::$app->swooleasync->async(json_encode($data));
    }

    public function actionMail($a='',$b=''){
        echo $a.'-'.$b;
    }  
}

6、无人值守, (*12)

服务启动脚本自带服务检测功能,因此可以将启动脚本配置为crontab用以自动保活服务, (*13)

* * * * * /path/to/yii/application/yii swooleasync/run start >> /var/log/console-app.log 2>&1

7、demo使用方法, (*14)

将站点目录指向/demo/test/backend/web 即可, (*15)

The Versions

01/08 2017

dev-master

9999999-dev

yii2异步任务扩展

  Sources   Download

BSD-4-Clause

The Requires

 

by Avatar mevyen

extension yii2

25/07 2017

1.1.0

1.1.0.0

yii2异步任务扩展

  Sources   Download

BSD-4-Clause

The Requires

 

by Avatar mevyen

extension yii2

30/09 2016

1.0.1

1.0.1.0

yii2异步任务扩展

  Sources   Download

BSD-4-Clause

The Requires

 

by Avatar mevyen

extension yii2