Home

jonnyjon edited this page Sep 13, 2010 · 6 revisions
Clone this wiki locally

Task Queue (for Kohana php 3)

Introduction

Task Queue is a task-based queue module for KO3. It was based on the Task_Queue module (a KO2 task queue on a RDBMS datastore) and MangoQueue.

Requirements

Compiled php with additional ./configure —enable-pcntl

You may also need to install pcntl:
- download php source

  cd /php.../ext/pcntl
  phpize
  ./configure
  make
  make install

Create a new database table:

#
# Table structure for table 'tasks'
#

CREATE TABLE /*!32312 IF NOT EXISTS*/ `tasks` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `route` varchar(50) default NULL,
  `uri` text,
  `e` tinyint(1) unsigned NOT NULL default '0',
  `priority` tinyint(2) unsigned default NULL,
  `created` int(11) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

A Task Based Queue

A php server daemon is run through php cli (command line) that spawns parrallel processes to complete any tasks it finds in the database.

Each task simply consists of a route and uri:

  Request::factory( Route::get( $task->route )->uri( $task->uri ) )->execute();

Usage: Adding tasks

Adding a task to the queue, is simple:

  task_queue::add_task('route', 'controller', 'action', 'id', $priority);

Of course, the keys of the URI array should match the keys you have specified in the route you’re requesting.

Usage: Executing tasks

When adding a task, you specify what route/controller/action/id should is requested. You have to build the controllers yourself. You can opt to have your controller extend Controller_CLI. This way, the controller will only be accessiible by CLI (and not from the web).

  class Controller_MyQueue extends Controller_Cli {
    
    	public function action_action1($id)
    	{
    		// do something
    	}
    }

Usage: Running the queue daemon

After setting some preferences in the daemon config file, running the daemon is simple, from the root command line run:

  php index.php --uri=daemon

or you can start/restart/stop the daemon from any controller:

  task_queue::daemon_start(true);
  task_queue::daemon_restart();
  task_queue::daemon_stop();