Kohana Queue Module Documentation
The Kohana Queue Module was created by SocialAmp. It is released under the BSD license as detailed in the license.txt file included in each release.
Architecture
The Kohana Queue Module runs on a client server methodology. A daemon server runs in the background on your application server scanning a database for Kohana jobs to run. When it finds a job to run, it loads the calling Kohana application, runs the task, and deletes it from the queue table.
The Kohana Queue Module has a simple API for applications to add jobs to the queue. It has job priorities so urgent jobs can run before non-essential jobs.
Any Kohana application on your server can use the Module. Just add it to the $config['modules'] array as described in the main Kohana Documentation.
The Daemon
The daemon is a stand-alone Kohana CLI application.
Configuration
Besides the regular Kohana config files, there is one custom config file that needs to be customized, if you wish to change the default values, application/config/queue.php. The options are:
- scan_delay: Sets the scan time of the queue daemon in seconds. The default is to scan every two seconds. If you have a busy server, you may want to make this one second.
- parallel_operations: This is the number of jobs the daemon will run at once. The default is 3. If your server is very fast or has lots of long jobs, you may want to increase this.
- table_name: The database table name of the queue
- validate_methods: Set whether to validate class methods before adding them to the queue.
In addition, you will need to set the following config files, database.php, log.php
Running The Daemon
To run the daemon on your server, change to the directory you installed the module, and run php index.php "application_queue/run". Alternatively, you could set this up as a startup item based on your system.
The Module
The module has one method to add jobs the queue:
Task_Queue::add()
This method adds a task to the queue. It has the following parameters.
- $controller_name - The full classname of the controller being called.
- $method_name - The full method name of the above class.
- $parameters - an associative array of parameters to pass to the method. Keys are the variable names of the method, and the values are the values.
- $priority - The priority number of this task. 1 is lowest, and 10 is the highest priority.
- $application_directory - The absolute path to the application directory of the class you are calling. Useful if you are processing tasks from code in a module.
Below is some example code to use the Module.
for ($i = 0; $i <= 20; $i++) {
$value = rand(0, 11); if (Task_Queue::add('Application_Queue_Controller', 'sleep', array('value' => $value), $value))}echo '<p>Task added to the queue</p>';elseecho '<p>Could not add the task to the queue</p>';
This would add the sleep() method from the Application_Queue_Controller class to the queue, with a random sleep value.