Download the PHP package cvo-technologies/cakephp-gearman without Composer
On this page you can find all versions of the php package cvo-technologies/cakephp-gearman. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cvo-technologies/cakephp-gearman
More information about cvo-technologies/cakephp-gearman
Files in cvo-technologies/cakephp-gearman
Package cakephp-gearman
Short Description A gearman plugin for CakePHP 3
License MIT
Homepage https://github.com/cvo-technologies/cakephp-gearman
Informations about the package cakephp-gearman
CakePHP Gearman plugin
Gearman task offloading for CakePHP 3.
Requirements
- PHP 5.5.9+
- CakePHP 3.2+
- Gearman Job Server
- Gearman PHP extension
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 Gearman Job Server.
Installation
Install the plugin using Composer:
Now load the plugin by either running this shell command:
or by manually adding the following line to config/bootstrap.php
:
Lastly, add a new Gearman
configuration section to (most likely) app.php
:
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:
Usage
Using this plugin comes down to:
- Configuring your task(s)
- Starting the
WorkerShell
on your local system - Offloading tasks from within your application code by using the
execute()
function found in theJobAwareTrait
To start the WorkerShell
so it will listen for incoming tasks run the
following command on your local system:
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:
Then add the following worker configuration to your existing EmailTransporter
configuration section (most likely found in app.php
):
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.
If things went well you should see the worker providing feedback on tasks being processed shown below:
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
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:
- include the
JobAwareTrait
in your application code - 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:
- PHPUnit and CakePHP Code Sniffer tests pass
- Code Coverage does not decrease