PHPackages                             webtechnick/cakephp-queue-plugin - 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. [Queues &amp; Workers](/categories/queues)
4. /
5. webtechnick/cakephp-queue-plugin

ActiveCakephp-plugin[Queues &amp; Workers](/categories/queues)

webtechnick/cakephp-queue-plugin
================================

CakePHP Queue Plugin - Complete tool to background and schedule your time consuming tasks. Queue up almost anything from CakePHP shells, models, and actions to standard php commands or even shell commands.

2394111[1 issues](https://github.com/webtechnick/CakePHP-Queue-Plugin/issues)[1 PRs](https://github.com/webtechnick/CakePHP-Queue-Plugin/pulls)PHP

Since Nov 30Pushed 6y ago4 watchersCompare

[ Source](https://github.com/webtechnick/CakePHP-Queue-Plugin)[ Packagist](https://packagist.org/packages/webtechnick/cakephp-queue-plugin)[ RSS](/packages/webtechnick-cakephp-queue-plugin/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (1)DependenciesVersions (1)Used By (0)

CakePHP Queue Plugin
====================

[](#cakephp-queue-plugin)

- Author: Nick Baker
- Version: 1.0.0
- License: MIT
- Website:

Features
--------

[](#features)

Complete tool to background and schedule your time consuming tasks. Queue up almost anything from CakePHP shells, models, and actions to standard php commands or even shell commands.

Changelog
---------

[](#changelog)

- 1.0.0 Initial release

Install
-------

[](#install)

Clone the repository into your `app/Plugin/Queue` directory:

```
git clone https://github.com/webtechnick/CakePHP-Queue-Plugin.git app/Plugin/Queue

```

Or you can install via

Load the plugin in your `app/Config/bootstrap.php` file:

```
CakePlugin::load('Queue');

```

Run the schema into your database to install the required tables.

```
cake schema create --plugin Queue

```

Setup
-----

[](#setup)

Create the file `app/Config/queue.php` with the defaults from `app/Plugin/Queue/Config/queue.php.default`:

```
$config = array(
	'Queue' => array(
		'log' => true, //logs every task run in a log file.
		'limit' => 1, //limit how many queues can run at a time. (default 1).
		'allowedTypes' => array( //restrict what type of command can be queued.
			1, //model
			2, //shell
			3, //url
			//4, //php_cmd
			//5, //shell_cmd
		),
		'archiveAfterExecute' => true, //will archive the task once finished executing for quicker queues
		'cache' => '+5 minute', //how long to cache cpu usage and list. (false disables cache)
		'cores' => '8', //number of cpu cores to correctly gauge current CPU load.
	)
);

```

Cron Setup (optional)
---------------------

[](#cron-setup-optional)

Once you start adding things to your queue you need to process it. You can do it a few ways.

1. By setting up a cron (recommended)

    crontab -e \*/1 \* \* \* \* /path/to/app/Plugin/Queue/scripts/process\_queue.sh

**Note:** The cron script assumes Console/cake is executable from the root of your app directory.

2. By processing the Queue via the built in shell

    cake Queue.queue process
3. By processing the Queue via the built in library

    App::uses('Queue','Queue.Lib'); Queue::process();
4. By navigating to the queue plugin admin web interface and processing the Queue (feature not available yet)

Quick Start Guide (Shell)
-------------------------

[](#quick-start-guide-shell)

Add a Task to the queue.

```
$ cake Queue.queue add "Queue.QueueTask::find('first')"

Task succesfully added.
525387a1-2dd0-4100-a48f-4f4be017215a queued model
	Command: Queue.QueueTask::find('first')
	Priority: 100

```

Process the Queue.

```
$ cake Queue.queue process

Processing Queue.
Success.

```

View the Task added.

```
$ cake Queue.queue view 525387a1-2dd0-4100-a48f-4f4be017215a

525387a1-2dd0-4100-a48f-4f4be017215a finished model
Command: Queue.QueueTask::find('first')
Priority: 100
Executed on Monday 7th of October 2013 10:19:10 PM. And took 0 ms.
Result: {"QueueTask":{"id":"525387a1-2dd0-4100-a48f-4f4be017215a","user_id":null,"created":"2013-10-07 22:18:41","modified":"2013-10-07 22:19:10","executed":null,"scheduled":null,"scheduled_end":null,"reschedule":null,"start_time":"1381205950","end_time":null,"cpu_limit":null,"is_restricted":false,"priority":"100","status":"2","type":"1","command":"Queue.QueueTask::find('first')","result":null,"execution_time":null,"type_human":"model","status_human":"in progress"}}

```

Quick Start Guide (Library)
---------------------------

[](#quick-start-guide-library)

Adding a Task to the queue.

```
App::uses('Queue', 'Queue.Lib');
$task = Queue::add("Queue.QueueTask::find('first')");
/* $task =
	'QueueTask' => array(
		'priority' => (int) 100,
		'command' => 'Queue.QueueTask::find('first')',
		'type' => (int) 1,
		'scheduled' => null,
		'scheduled_end' => null,
		'reschedule' => null,
		'cpu_limit' => null,
		'modified' => '2013-10-07 22:22:36',
		'created' => '2013-10-07 22:22:36',
		'user_id' => null,
		'id' => '5253888c-ae18-4ffd-991a-4436e017215a'
	) */

```

Process the queue.

```
$result = Queue::process();
/* $result will be boolean true */

```

View the Task.

```
$task = Queue::view('5253888c-ae18-4ffd-991a-4436e017215a');
/* $task is the string representation, same as queue view. Not as useful */

$task = Queue::findById('5253888c-ae18-4ffd-991a-4436e017215a');
/* $task is now an associative array, much more useful.
array(
	'id' => '52538a36-df1c-4186-a50a-4076e017215a',
	'user_id' => null,
	'created' => '2013-10-07 22:29:42',
	'modified' => '2013-10-07 22:29:42',
	'executed' => '2013-10-07 22:29:42',
	'scheduled' => null,
	'scheduled_end' => null,
	'reschedule' => null,
	'start_time' => '1381206582',
	'end_time' => '1381206582',
	'cpu_limit' => null,
	'is_restricted' => false,
	'priority' => '100',
	'status' => '3',
	'type' => '1',
	'command' => 'Queue.QueueTask::find('first')',
	'result' => '{"QueueTask":{"id":"524b0c44-a3a0-4956-8428-dc3ee017215a","user_id":null,"created":"2013-10-01 11:54:12","modified":"2013-10-01 11:54:12","executed":null,"scheduled":null,"scheduled_end":null,"reschedule":null,"start_time":null,"end_time":null,"cpu_limit":null,"is_restricted":false,"priority":"100","status":"1","type":"1","command":"SomeModel::action(\"param\",\"param2\")","result":"","execution_time":null,"type_human":"model","status_human":"queued"}}',
	'execution_time' => '0',
	'type_human' => 'model',
	'status_human' => 'finished'
)	*/

```

Adding Tasks to Queue Types and Commands
----------------------------------------

[](#adding-tasks-to-queue-types-and-commands)

Once you've decided how you're going to process your queue it's time to start adding tasks to your queue.

To add tasks use the built in shell or library.

```
Queue::add($command, $type, $options = array());
cake Queue.queue add "command" -t type

```

There are *5 types* of commands you can use. The default is *model*.

1. **Model** : Model function to execute. Examples:

    \#Command strings Model::action() Plugin.Model::action("param1","pararm2")

    \#Adding the command to the queue. Queue::add("Plugin.Model::action('param1','pararm2')"); cake Queue.queue add "Plugin.Model::action('param1','pararm2')"

    ```
     Models are setup via ClassRegistry::init() and methods executed on the model object as public method calls.

    ```
2. **Shell** : CakePHP shell to execute. Examples:

    \#Command strings ClearCache.clear\_cache shell\_command -f flag arg1 arg2

    \#Adding the command to the queue. Queue::add("shell\_command -f flag arg1 arg2", 'shell'); cake Queue.queue add "shell\_command -f flag arg1 arg2" -t shell
3. **Url** : A URL to requestAction. Example:

    \#Command string /path/to/url

    \#Adding the command to the queue. Queue::add("/path/to/url", 'url'); cake Queue.queue add "/path/to/url" -t url
4. **php\_cmd** : PHP Command, a simple or complex php command. Examples:

    \#Command strings 3 + 5 mail('','subject','message')

    \#Adding the command to the queue. Queue::add("mail('','subject','message')", 'php\_cmd'); cake Queue.queue add "mail('','subject','message')" -t php\_cmd
5. **shell\_cmd** : Basic Bash shell command. Examples:

    \#Command string echo 'hello' &amp;&amp; echo 'world'

    \#Adding the command to the queue. Queue::add("echo 'hello' &amp;&amp; echo 'world'", 'shell\_cmd'); cake Queue.queue add "echo 'hello' &amp;&amp; echo 'world'" -t shell\_cmd

**NOTE** `php_cmd` and `shell_cmd` are not allowed by default. You have to turn them on in `app/Config/queue.php`.

Viewing Your Queue
------------------

[](#viewing-your-queue)

You can see what is in the queue at any given time and what is up next by calling `Queue::next($limit);` or the shell

```
cake Queue.queue next 10

Retrieving Queue List.
1) 52537d35-95d0-48aa-a48d-416de017215a queued url
	Command: /path/to/url
	Priority: 100
	Restricted By:
		Start: 2013-10-11 03:00:00
		CPU
