umer936/cakephp-rabbitmq

此软件包已被废弃且不再维护。未建议替代软件包。

CakePHP 4 的 RabbitMQ 插件

安装: 36

依赖项: 0

建议者: 0

安全: 0

星级: 0

观察者: 1

分支: 6

类型:cakephp-plugin

v4.0 2022-06-30 15:56 UTC

This package is not auto-updated.

Last update: 2024-03-07 20:02:32 UTC


README

我不再使用此插件,而是选择使用https://github.com/dereuromark/cakephp-queue

此外,还有https://github.com/cakephp/queue,它具有许多传输方式,包括 AMQP Bunny(RabbitMQ 的核心):https://php-enqueue.github.io/transport

更多信息:请参阅https://github.com/FriendsOfCake/awesome-cakephp#queue

此插件将继续存在,以防有人需要 RabbitMQ 在 CakePHP 4 中正常运行

composer.json

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/umer936/cakephp-rabbitmq.git"
        }
    ],
    
    
    "autoload": {
        "psr-4": {
            "DevApp\\RabbitMQ\\": "./vendor/umer936/cakephp-rabbitmq/src/"
        }
    },
composer require umer936/cakephp-rabbitmq

Application.php(可能不需要选项数组,但不知何故我在某个时候添加了它)

        $this->addPlugin('DevApp/RabbitMQ', [
            'bootstrap' => true,
            'path' => ROOT . DS . 'vendor' . DS . 'umer936' . DS . 'cakephp-rabbitmq' . DS,
        ]);

CakePHP RabbitMQ 插件

Build Status StyleCI Status Coverage Status Total Downloads License

CakePHP 4 的 RabbitMQ 插件。

需求

为什么使用此插件?

使用此插件可以通过将耗时的过程(如发送电子邮件和调整上传的图片大小)卸载到使用 RabbitMQ 消息的 CLI 消费者来显著减少页面加载时间。也可以用于与其他系统通信,例如记录 lintes。

安装

使用 Composer 安装插件

composer require 0100dev/cakephp-rabbitmq

现在通过运行以下 shell 命令加载插件

bin/cake plugin load DevApp/RabbitMQ --bootstrap

或者手动将以下行添加到 config/bootstrap.php

$this->addPlugin('DevApp/RabbitMQ', ['bootstrap' => true]);

最后,向(最可能是)app.php 中添加一个新的 Gearman 配置部分

    'Gearman' => [
        'Servers' => [
            '127.0.0.1:4730'
        ],
        'Jobs' => [

        ]
    ]

可选:系统验证

在继续之前,您可能希望验证 Gearman Job Server 是否确实在您的本地系统上运行。

在 Ubuntu 系统上运行 sudo netstat -peanut | grep gearman 应该会产生类似以下内容

tcp      0     0 127.0.0.1:4730     0.0.0.0:*     LISTEN     0     9727     625/gearmand
tcp6     0     0 ::1:4730           :::*          LISTEN     0     9726     625/gearmand

用法

使用此插件主要是

  1. 配置您的任务
  2. 在本地系统上启动 WorkerShell
  3. 通过使用 JobAwareTrait 中找到的 execute() 函数,从应用程序代码中卸载任务

要启动 WorkerShell 以使其侦听传入的任务,请在本地系统上运行以下命令

bin/cake consumer

内置任务

电子邮件任务

此插件附带一个内置电子邮件任务,允许您立即使用工作器开始卸载电子邮件。

要启用电子邮件任务,请首先将以下作业添加到您的 Gearman 配置部分

    'Jobs' => [
        'className' => 'Email'
    ]

然后添加以下工作器配置到现有的 EmailTransporter 配置部分(最可能在 app.php 中找到)

'worker' => [
    'className' => 'CvoTechnologies/Gearman.Worker',
    'transport' => 'default',
    'background' => true
]

现在您只需在应用程序中发送电子邮件时使用此 EmailTransporter,它将自动将所有电子邮件发送卸载到在 transport 键中定义的内置任务。例如:

$email = new Email('default');
$res = $email->from(['you@example.com' => 'Your Site'])
    ->to('recipient@sexample.com')
    ->subject('Testing cakephp-gearman built-in EmailTask')
    ->send('Your message');

如果一切顺利,您应该会看到工作器提供以下正在处理任务的反馈

Worker feedback

创建您自己的任务

1. 创建任务

例如,我们将创建以下 SleepTask,它将用作 Gearman 作业

  • 将用作 Gearman 作业
  • 必须创建在 src/Shell/Task/SleepTask.php
  • 必须包含一个 main() 函数
<?php
namespace DevApp\RabbitMQ\Shell\Task;

use Cake\Console\Shell;

class SleepTask extends Shell
{

    public function main($workload, GearmanJob $job)
    {
        $job->sendStatus(0, 3);

        sleep($workload['timeout']);

        $job->sendStatus(1, 3);

        sleep($workload['timeout']);

        $job->sendStatus(2, 3);

        sleep($workload['timeout']);

        return array(
            'total_timeout' => $workload['timeout'] * 3
        );
    }
}

请注意,插件将负责数组和对象。当你在任务中提交一个数组时,你将在工作负载中收到一个数组。

2. 开始使用任务

要开始使用任务

  1. 在您的应用程序代码中包含 JobAwareTrait
  2. 使用 $this->execute 函数将作业传递给 Gearman

请注意,execute() 方法接受以下参数

  • $name:作业名称(cakephp中的任务)
  • $workload:混合类型,可以是数组、字符串、整数或其他任何类型
  • $background:布尔值,为 true 时在后台运行
  • $priority:Gearman::PRIORITY_NORMAL、_LOW、_NORMAL 或 _HIGH

补丁和功能

在提交PR之前,请确保以下内容

错误和反馈

http://github.com/0100Dev/cakephp-rabbtmq/issues