PHP code example of alan / yii2-swoole-async
1. Go to this page and download the library: Download alan/yii2-swoole-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/ */
alan / yii2-swoole-async example snippets
//测试环境配置
'dcache_data_center' => [
'url' => 'http://xxxxxxxxxxxxx',
'key' => 'xxxxxxxxx',
],
//正式环境配置
'dcache_data_center' => [
'url' => 'https://xxxxxxxxxxx',
'key' => 'xxxxxxxxxxxxxxxxx',
],
['db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=xxxxxxxxx;port=3307;dbname=xxxxxxxxx',
'username' => 'xxxxxxxxxx',
'password' => 'xxxxxxxxxxxx',
'charset' => 'utf8',
'tablePrefix' => 'xxxxxx',
'commandClass' => 'xxxxxxxxxxxxxxx',
],
'swoole' => [
'class' => 'yii2\swoole_async\basic\Swoole',
'host' => '127.0.0.1', //beanstalkd host
'port' => '11300', //beanstalkd port
],
'request' => [ //添加行为
'as beforeAction' => [
'class' => \yii2\swoole_async\components\LogIDBehavior::class, //替换
'name' => 'swooleTask',
],
],
'components' => [ //替换console的日志类
'log' => [
[
'class' => 'yii2\swoole_async\components\FileTarget', //替换
'levels' => ['warning', 'info','error'],
'categories' =>['server'],
'exportInterval' => 1, //这里注重,太大日志则不能及时刷入文件,太小会影响性能
'logVars' => [],
'logFile' => __DIR__ . '/../runtime/logs/server_'. date("ymd") .'.log',
'maxFileSize' => 1024 * 500,//以kb为单位的
'maxLogFiles' =>5
],
],
]]
use Yii;
use yii\console\Controller;
use yii2\swoole_async\basic\Swoole;
use yii\base\InvalidConfigException;
use yii2\swoole_async\tasks\TestAsyncAsyncTask;
class SwooleController extends Controller {
public function actionHello() {
print_r("hello");
}
/**
* @return Swoole
* @throws InvalidConfigException
*/
public function getSwoole(){
/** @var Swoole $swoole */
return Yii::$app->get("swoole");
}
/**
* 启动swoole
* @throws InvalidConfigException
*/
public function actionStart() {
$this->getSwoole()->start();
}
/**
* 停止
* @throws InvalidConfigException
*/
public function actionStop()
{
$this->getSwoole()->stop();
}
/**
* 查询状态
* @throws InvalidConfigException
*/
public function actionStatus()
{
$this->getSwoole()->status();
}
/**
* 重启扫热服务
* @throws InvalidConfigException
*/
public function actionReload()
{
$this->getSwoole()->reload();
}
/**
* 重启服务
* @throws InvalidConfigException
*/
public function actionRestart()
{
$this->getSwoole()->restart();
}
public function actionAsyncTask()
{
TestAsyncAsyncTask::publish();
}
}
php yii swoole/start
use yii2\swoole_async\basic\AsyncTask;
class TestAsyncAsyncTask extends AsyncTask
{
public $rule = [
'type' => 'async',
];
/**
* @param mixed $data
* @return bool
*/
public function consume($data): bool
{
// TODO: Implement consume() method.
print_r(__METHOD__);
print_r($data);
return true;
}
}
TestAsyncAsyncTask::publish();