This plugin is a simple wrapper around the Queuesadilla library, providing tighter integration with the CakePHP framework.
The only officially supported method of installing this plugin is via composer.
View on
Packagist,
and copy the json snippet for the latest version into your project’s
composer.json
. Eg, v. 0.1.7 would look like this:
{
"require": {
"josegonzalez/cakephp-queuesadilla": "0.1.7"
}
}
You need to enable the plugin your config/bootstrap.php
file:
<?php
Plugin::load('Josegonzalez/CakeQueuesadilla');
If you are already using Plugin::loadAll();
, then this is not
necessary.
Once loaded, you’ll need to consume the configuration in your
config/bootstrap.php
like so:
<?php
use Josegonzalez\CakeQueuesadilla\Queue\Queue;
Configure::load('Queuesadilla');
Queue::config(Configure::consume('Queuesadilla'));
This plugin can be configured via your config/app.php
. It works similar
to how all other CakePHP engine-based libraries work (Cache, Email, Log), and
as such you can have multiple backends under different names. Here is an example
config stanza:
/**
* Configures the Queuesadilla engine to read from mysql as it's database
* The `url` parameter is a datasource name, as supported by CakePHP and other frameworks.
*
* Some examples:
* - beanstalkd: beanstalk://127.0.0.1:11300?queue=default&timeout=1
* - in memory: memory:///?queue=default&timeout=1
* - mysql: mysql://travis@127.0.0.1:3306/database_name?queue=default&timeout=1
* - /dev/null: null:///?queue=default&timeout=1
* - redis: redis://travis@127.0.0.1:6379/0?queue=default&timeout=1
* - synchronous: synchronous:///?queue=default&timeout=1
* - postgres: pgsql://postgres@127.0.0.1:5432/database_name?queue=default
*/
'Queuesadilla' => [
'default' => [
'url' => env('QUEUESADILLA_DEFAULT_URL', ''),
],
],
Note that the config array is passed as settings to the queueing engine. Please refer to the Queuesadilla docs for more information on how each engine can be configured.
Please Note:
If your job callback is performing a database involved operation, you should check if the CakePHP database connection is still alive. It can happen that your database connection timed out, when no jobs were acknowledged for a long time. A way to come around this, is to disconnect the CakePHP connection every time a job succeeded or failed.
To do so, create a subclass of the QueuesadillaShell
and implement two event listeners inside the getWorker
method.
You can easily do that by baking a shell with bin/cake bake shell MyQueuesadilla
command and alter the created
class as below:
<?php
namespace App\Shell;
use Cake\Datasource\ConnectionManager;
use Josegonzalez\CakeQueuesadilla\Shell\QueuesadillaShell;
/**
* MyQueuesadilla shell command.
*/
class MyQueuesadillaShell extends QueuesadillaShell
{
/**
* @param \josegonzalez\Queuesadilla\Engine\Base $engine
* @param \Psr\Log\LoggerInterface $logger
* @return \josegonzalez\Queuesadilla\Worker\Base
*/
public function getWorker($engine, $logger)
{
$worker = parent::getWorker($engine, $logger);
$worker->attachListener('Worker.job.success', function ($event) {
ConnectionManager::get('default')->disconnect();
});
$worker->attachListener('Worker.job.failure', function ($event) {
ConnectionManager::get('default')->disconnect();
});
return $worker;
}
}
You can start a queue off the jobs
mysql table:
# ensure everything is migrated and the jobs table exists
bin/cake migrations migrate --plugin Josegonzalez/CakeQueuesadilla
# default queue
bin/cake queuesadilla
# also the default queue
bin/cake queuesadilla --queue default
# some other queue
bin/cake queuesadilla --queue some-other-default
# use a different config
bin/cake queuesadilla --config other
Need to queue something up?
<?php
use Josegonzalez\CakeQueuesadilla\Queue\Queue;
// a function in the global scope
function some_job($job) {
var_dump($job->data());
}
// uses the 'default' engine
Queue::push('some_job', [
'id' => 7,
'message' => 'hi'
]);
// uses the 'other' engine
Queue::push('some_job', [
'id' => 7,
'message' => 'hi'
], ['config' => 'other']);
// uses the 'default' engine
// on the 'slow' queue
Queue::push('some_job', [
'id' => 7,
'message' => 'hi'
], ['config' => 'other', 'queue' => 'slow']);
?>
You can also add the Josegonzalez\CakeQueuesadilla\Traits\QueueTrait
to any class in order to have a protected push
method added to the class so that you can do $this->push()
.
See here for more information on defining jobs.