bazilio/yii2-async

提供透明的API,用于将大型任务移出请求上下文

安装次数: 42,174

依赖项: 3

建议者: 0

安全性: 0

星级: 63

关注者: 9

分支: 18

开放问题: 0

类型:yii2-extension

0.2.0 2016-04-01 10:26 UTC

This package is auto-updated.

Last update: 2024-09-10 08:15:48 UTC


README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

提供透明的API,用于将大型任务移出请求响应

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

需求
使用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'
        ]
    ]
]
使用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',
        ]
    ]
]
使用MySQL(可能任何SQL,但仅在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',
        ]
    ]
]

应用迁移

./yii migrate/up --migrationPath=@vendor/bazilio/yii2-async/migrations
用法

创建和发送

测试类示例

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);

或调用外部方法

$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);
}

执行

通过Bash方式

填充控制台配置

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

运行

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

通过代码方式

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

更多代码示例请查看测试

运行测试
vendor/bin/codecept run

或在Docker中

./test.sh