yii2-async

Provides translucent api & queues for moving large tasks out of request context with SQL, Redis or AMQP.

Github星跟蹤圖

yii2-async

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

主要指標

概覽
名稱與所有者bazilio91/yii2-async
主編程語言PHP
編程語言PHP (語言數: 3)
平台
許可證MIT License
所有者活动
創建於2014-07-08 10:18:18
推送於2020-04-09 22:36:51
最后一次提交2017-04-17 03:36:28
發布數18
最新版本名稱0.2.0 (發布於 )
第一版名稱0.0.1 (發布於 )
用户参与
星數62
關注者數9
派生數18
提交數36
已啟用問題?
問題數10
打開的問題數0
拉請求數2
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?