PHPackages                             alan/yii2-swoole-async - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. alan/yii2-swoole-async

ActiveLibrary[Framework](/categories/framework)

alan/yii2-swoole-async
======================

yii2 framwork swoole async task

2.0.0(4y ago)024Apache-2.0PHP

Since Jun 1Pushed 4y ago1 watchersCompare

[ Source](https://github.com/wzzjjboy/yii2-swoole-async)[ Packagist](https://packagist.org/packages/alan/yii2-swoole-async)[ RSS](/packages/alan-yii2-swoole-async/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (8)Versions (4)Used By (0)

Yii2 SWOOLE ASYNC
=================

[](#yii2-swoole-async)

1. 特性
2. 安装

    composer require alan/yii2-swoole-async:v2.0.0
3. 配置

    > 1.0.x有消费者卡死的Bug, 解决的办法是升级了swoole到4.5.\*的版本，使用Pool代替原来的swoole\_service。
    >
    > 关于1.0.0升级到2.0的配置的变更，由于2.x采用Yii2框架自身的日志组件又要在消费任务的时候刷新LogId, 所以需要替换文件类，配置层级 composents =&gt; logs =&gt; targets =&gt; class: yii2\\mq\_task\\components\\FileTarget

    建议添加到common\\config\\main-local.php文件添加

    ```
    //测试环境配置
    'dcache_data_center' => [
            'url' => 'http://xxxxxxxxxxxxx',
            'key' => 'xxxxxxxxx',
    ],
    //正式环境配置
    'dcache_data_center' => [
        'url' => 'https://xxxxxxxxxxx',
       'key' => 'xxxxxxxxxxxxxxxxx',
    ],
    ```

    - 添加dcache表对应的配置(此配置用于查询表结构，用于ORM操作)

    ```
    ['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
         ],
     ],
    ]]
    ```

    - 添加task表

        ```
        CREATE TABLE `gpi_async_tasks` (
          `task_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '任务ID',
          `job_id` varchar(100) NOT NULL DEFAULT '' COMMENT 'Job ID',
          `task_b_id` varchar(100) NOT NULL DEFAULT '' COMMENT '任务业务ID',
          `task_type` int(11) NOT NULL DEFAULT 0 COMMENT '任务类型：1|Timed 2|Delay 3|Async',
          `task_name` varchar(20) NOT NULL DEFAULT '' COMMENT '任务名称',
          `task_class` varchar(100) NOT NULL DEFAULT '' COMMENT '任务类名',
          `task_data` varchar(500) NOT NULL DEFAULT '' COMMENT '任务数据JSON',
          `task_rule` varchar(500) NOT NULL DEFAULT '' COMMENT '任务规则JSON',
          `task_status` smallint(6) NOT NULL DEFAULT '0' COMMENT '状态 0|未完成 1|已完成',
          `run_count` int(11) NOT NULL DEFAULT '0' COMMENT '任务执行次数',
          `task_over` int(11) NOT NULL DEFAULT '0' COMMENT '任务结束 0|未结束 1|已结束',
          `output` varchar(200) NOT NULL DEFAULT '' COMMENT '执行中的输出',
          `finish_at` datetime DEFAULT NULL COMMENT '任务完成时间',
          `created_at` datetime NOT NULL COMMENT '创建时间',
          `updated_at` datetime NOT NULL COMMENT '更新时间',
          PRIMARY KEY (`task_id`),
          KEY `idx_job_id` ()
          KEY `task_status` (`task_status`,`task_over`) USING BTREE
        ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='定时任务记录表';
        ```
    - 添加启动脚本

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

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 50% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~96 days

Total

3

Last Release

1614d ago

Major Versions

v1.0.0 → 2.0.02021-11-23

### Community

Maintainers

![](https://www.gravatar.com/avatar/c0834b401e5bb45e8f76303cd0284840de2a2c5f1c6443857174b81cf111666e?d=identicon)[wzzjjboy](/maintainers/wzzjjboy)

---

Top Contributors

[![lzcmaro](https://avatars.githubusercontent.com/u/15357931?v=4)](https://github.com/lzcmaro "lzcmaro (1 commits)")[![wzzjjboy](https://avatars.githubusercontent.com/u/13265064?v=4)](https://github.com/wzzjjboy "wzzjjboy (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/alan-yii2-swoole-async/health.svg)

```
[![Health](https://phpackages.com/badges/alan-yii2-swoole-async/health.svg)](https://phpackages.com/packages/alan-yii2-swoole-async)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.6k509.9M17.0k](/packages/laravel-framework)[shopware/platform

The Shopware e-commerce core

3.3k1.5M3](/packages/shopware-platform)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

595.2M386](/packages/shopware-core)[fancyecommerce/fec

fancy ecommerce

2212.0k2](/packages/fancyecommerce-fec)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
