umer936/cakephp-rabbitmq 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

umer936/cakephp-rabbitmq

最新稳定版本:v4.0

Composer 安装命令:

composer require umer936/cakephp-rabbitmq

包简介

A RabbitMQ plugin for CakePHP 4

README 文档

README

I no longer use this plugin in favor of https://github.com/dereuromark/cakephp-queue

Additionally, there is https://github.com/cakephp/queue which features a lot more transports, including AMQP Bunny (core of RabbitMQ): https://php-enqueue.github.io/transport

See also: https://github.com/FriendsOfCake/awesome-cakephp#queue for more options

This plugin will still live on in case someone needs RabbitMQ to just work in 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 (likely don't need the options array but for some reason I added it at some point)

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

CakePHP RabbitMQ plugin

Build Status StyleCI Status Coverage Status Total Downloads License

RabbitMQ plugin for CakePHP 4.

Requirements

Why use this plugin?

Use this plugin to drastically reduce page load times by offloading time consuming processes (like sending emails and resizing uploaded images) to a CLI consumer using messages in RabbitMQ. Could also be used to communicate with other systems or, for example, log lintes.

Installation

Install the plugin using Composer:

composer require 0100dev/cakephp-rabbitmq

Now load the plugin by either running this shell command:

bin/cake plugin load DevApp/RabbitMQ --bootstrap

or by manually adding the following line to config/bootstrap.php:

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

Lastly, add a new Gearman configuration section to (most likely) app.php:

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

        ]
    ]

Optional: system verification

Before proceeding, you might want to verify that the Gearman Job Server is actually up and running on your local system.

On Ubuntu systems running sudo netstat -peanut | grep gearman should produce something similar to:

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

Usage

Using this plugin comes down to:

  1. Configuring your task(s)
  2. Starting the WorkerShell on your local system
  3. Offloading tasks from within your application code by using the execute() function found in the JobAwareTrait

To start the WorkerShell so it will listen for incoming tasks run the following command on your local system:

bin/cake consumer

Built-in Tasks

Email Task

This plugin comes with a built-in email task that allows you to start offloading emails using the worker instantly.

To enable the email task first add the following job to your Gearman configuration section:

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

Then add the following worker configuration to your existing EmailTransporter configuration section (most likely found in app.php):

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

Now all you need to do is use this EmailTransporter in your application when sending emails and it will automatically offload all email sending to the built-in task using the EmailTransporter defined in the transport key. E.g.

$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');

If things went well you should see the worker providing feedback on tasks being processed shown below:

Worker feedback

Creating your own tasks

1. Create the Task

As an example we will create the following SleepTask that:

  • will be used as a Gearman job
  • must be created in src/Shell/Task/SleepTask.php
  • must contain a main() function
<?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
        );
    }
}

Please note that the plugin will take care of arrays and objects. When you submit an array in the task, you will receive an array in the workload.

2. Start using the task

To start using the task:

  1. include the JobAwareTrait in your application code
  2. use the $this->execute function to pass the job to Gearman

Please note that the execute() method takes the following parameters:

  • $name: name of the job (task in cakephp)
  • $workload: mixed, can be either an array, string, int or everything else
  • $background: boolean, true to run in background
  • $priority: Gearman::PRIORITY_NORMAL, _LOW, _NORMAL or _HIGH

Patches & Features

Before submitting a PR please make sure that:

Bugs & Feedback

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

统计信息

  • 总下载量: 38
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固