PHP code example of alan / yii2-mq-task

1. Go to this page and download the library: Download alan/yii2-mq-task library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alan / yii2-mq-task example snippets


     'db' => [ //默认db配置
         'class' => 'yii\db\Connection',
         'dsn' => 'mysql:host=host;port=port;dbname=dbname',
         'username' => 'username',
         'password' => 'password',
         'charset' => 'utf8',
         'tablePrefix' => 'tablePrefix',
         'commandClass' => 'yii2\mq_task\components\DbCommand',
      ],
     'redis' => [	//redis配置
         'class' => 'yii2\mq_task\components\RedisConnection',
         'hostname' => 'hostname',
         'port' => 'port',
         'database' => 1, 
         'password' => 'password'
     ],
     'invoiceRedisEvent' => [ //mq_task的名字
         'class'         => 'console\mqTask\InvoiceRedisEvent',
         'host'          => 'host',
         'port'          => 'port',
         'username'      => 'username',
         'password'      => 'password',
         'exchange_name' => 'exchange_name',
         'queue_name'    => 'queue_name',
         'routing_key'   => 'routing_key',
     ],
     'messageQueue'      => [
             'class'     => 'yii2\mq_task\basic\MQEngine',
     		 'processNamePrefix' => "cloudMq",
             'log'       		=> [
                 'class'    	=> 'yii2\mq_task\basic\Log',
                 'category'  => 'mq_task',
             ],
             'tasks'     => [
                 'invoiceRedisEvent' => 5, //要处理的mq_task和对应的进程数
             ]
     ],
     'request' => [
       	'as beforeAction' => [
         	'class' => \yii2\mq_task\components\LogIDBehavior::class, //替换
         	'name'  => 'console',
       ]
     ],  
     //替换console的日志类
     'components' => [
     	'log' => [
         	[
             'class' => 'yii2\mq_task\components\FileTarget', //替换
             'levels' => ['warning', 'info','error'],
             'categories' =>['server'],
             'exportInterval' => 1, //这里注重,太大日志则不能及时刷入文件,太小会影响性能
             'logVars' => [],
             'logFile' => __DIR__ . '/../runtime/logs/server_'. date("ymd") .'.log',
             'maxFileSize' => 1024 * 1024,//日志大小1G,以kb为单位的
             'maxLogFiles'=>5
           ],
       ],
     ]
     

     namespace console\controllers;
     
     use Yii;
     use yii\console\Controller;
     use yii2\mq_task\basic\MQEngine;
     
     class MqController extends Controller
     {
         /**
          * @return MQEngine
          * @throws \yii\base\InvalidConfigException
          */
         public function getMQ(){
             return Yii::$app->get("messageQueue");
      }
     
      /**
          * 启动MQ
          */
         public function actionStart()
         {
             $this->getMQ()->start();
         }
     
         /**
          * 停止MQ
          */
         public function actionStop()
         {
     
             $this->getMQ()->stop();
         }
     
         /**
          * MQ状态查询
          */
         public function actionStatus()
         {
             $this->getMQ()->status();
         }
     
         /**
          * 服务热重启
          */
         public function actionReload()
         {
             $this->getMQ()->reload();
         }
     
         /**
          * 重启服务
          */
         public function actionRestart()
         {
             $this->getMQ()->restart();
         }
     }
     

     php yii mq/start
     

        php yii mq/stop
        

           php yii mq/reload
           

           php yii mq/restart
           

          php yii mq/status
          

     namespace console\mqTask;
     
     
     use yii2\mq_task\basic\Task;
     
     class InvoiceRedisEvent extends Task {
     
         /**
          * @param array $data
          * @return bool
          */
         public function consume(array $data): bool {
             // TODO: Implement consume() method.
             print_r($data);//在这里处理任务
             return true;
         }
     }