定制 bazilio/yii2-async 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bazilio/yii2-async

最新稳定版本:0.2.0

Composer 安装命令:

composer require bazilio/yii2-async

包简介

Provides translucent api for moving large tasks out of request context

README 文档

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

Provides translucent api for moving large tasks out of request response

Install: php composer.phar require bazilio/yii2-async:dev-master

Requirments:
Using with AMQP:

php composer.phar require pdezwart/php-amqp:dev-master

main.php:

'components' => [ 'async' => [ 'class' => 'bazilio\async\AsyncComponent', 'transportClass' => 'bazilio\async\transports\AsyncAmqpTransport', 'transportConfig' => [ 'host' => 'localhost', 'login' => 'guest', 'password' => 'guest', 'vhost' => 'yii', 'exchangeName' => 'yii' ] ] ]
Using with Redis:

php composer.phar require yiisoft/yii2-redis:*

main.php:

'components' => [ 'redis' => [ 'class' => 'yii\redis\Connection', 'hostname' => 'localhost', 'port' => 6379, 'database' => 0, 'dataTimeout' => -1, // important for daemon and blocking queries ], 'async' => [ 'class' => 'bazilio\async\AsyncComponent', 'transportClass' => 'bazilio\async\transports\AsyncRedisTransport', 'transportConfig' => [ 'connection' => 'redis', ] ] ]
Using with MySQL (probably any sql, but tested only with mysql)

main.php:

'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2advenced', 'username' => 'root', 'password' => '', 'charset' => 'utf8', ], 'async' => [ 'class' => 'bazilio\async\AsyncComponent', 'transportClass' => 'bazilio\async\transports\AsyncMysqlTransport', 'transportConfig' => [ 'connection' => 'db', ] ] ]

Apply migrations:

./yii migrate/up --migrationPath=@vendor/bazilio/yii2-async/migrations
Usage:

Create and send:

Test class example:

class DownloadTask extends AsyncTask { public $url; public $file; public static $queueName = 'downloads'; public function execute() { return file_put_contents($this->file, file_get_contents($this->url)); } } // create task $task = new DownloadTask(['url' => 'http://localhost/', 'file' => '/tmp/localhost.html']); \Yii::$app->async->sendTask($task);

Or call external method:

$task = new AsyncExecuteTask([ 'class' => 'common\components\MyDownloaderComponent', 'method' => 'download', 'arguments' => ['url' => 'http://localhost/', 'file' => '/tmp/localhost.html'] ]); $task::$queueName = 'downloads'; if (YII_ENV !== 'prod') { $task->execute(); } else { Yii::$app->async->sendTask($task); }

Execute:

Bash way:

Fill console config:

'controllerMap' => [ 'async-worker' => [ 'class' => 'bazilio\async\commands\AsyncWorkerCommand', ], ],

Run:

# Process and exit on finish ./yii async-worker/execute downloads # Process and wait for new tasks (only redis) ./yii async-worker/daemon downloads

Code way:

while ($task = \Yii::$app->async->receiveTask('downloads')) { if ($task->execute()) { \Yii::$app->async->acknowledgeTask($task); } }

For more code examples look into tests:

Runing tests:
vendor/bin/codecept run 

Or in Docker:

./test.sh 

统计信息

  • 总下载量: 47.81k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 62
  • 点击次数: 2
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 62
  • Watchers: 9
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固