PHP code example of bazilio / yii2-async
1. Go to this page and download the library: Download bazilio/yii2-async 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/ */
bazilio / yii2-async example snippets
'components' => [
'async' => [
'class' => 'bazilio\async\AsyncComponent',
'transportClass' => 'bazilio\async\transports\AsyncAmqpTransport',
'transportConfig' => [
'host' => 'localhost',
'login' => 'guest',
'password' => 'guest',
'vhost' => 'yii',
'exchangeName' => 'yii'
]
]
]
'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',
]
]
]
'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);
}
'controllerMap' => [
'async-worker' => [
'class' => 'bazilio\async\commands\AsyncWorkerCommand',
],
],
while ($task = \Yii::$app->async->receiveTask('downloads')) {
if ($task->execute()) {
\Yii::$app->async->acknowledgeTask($task);
}
}