New CakePHP “multitask” Plugin

What is multitask?

Multitask is a CakePHP plugin designed as a proof of concept task manager for non-interactive tasks.

It consists of:

  1. MultitaskerShell - a daemon that manages the tasks/threads
  2. MultitaskQueuedTask - a model for adding queued tasks
  3. ThreadedTask - a base class for your tasks

What does it do?

The multitasker shell acts as a daemon, getting a task from a model, executing it, and updating its status when done.

It could be used for executing long-running tasks in the background, such as tasks involving significant network activity or encoding video, etc.

Installation & Usage

  1. Copy the "multitask" folder into the plugins folder of your CakePHP app.
  2. Import the database schema from config/multitask.sql
  3. Run "multitasker" shell from the command line.

cd cake/console/
./cake multitasker -app ../../app

Then add some tasks into your database and it will execute them. :)

Here is some code that will add a task into the queue. Look in vendors/shells/tasks/echo.php for the task being executed.

$this->MultitaskQueuedTask = ClassRegistry::init('Multitask.MultitaskQueuedTask');

$task = array(
    'task' => 'echo',
    'data' => 'Hello, World!',
);

$this->MultitaskQueuedTask->create($task, true);
$this->MultitaskQueuedTask->save();


$task = array(
    'task' => 'echo',
    'method' => 'delayed',
    'data' => array('duration' => 3, 'message' => 'That was a nice sleep!'),
);

$this->MultitaskQueuedTask->create($task, true);
$this->MultitaskQueuedTask->save();

Known Issues

I've not used this in production and don't recommend you do either! Heck, I haven't used it aside from a few small tests, so use with caution.

The plugin is in its elementary stages, and doesn't handle scripting errors, task progress, task dependencies, task priorities, etc.

Requirements

PHP needs to be configured with pcntl and shmop extensions, as it uses PHP_Fork to handle threading.

I have only tested it on PHP5. Please let me know if you get it working on PHP4.

Windows Compatibility

Although the pcntl and shmop extensions don't run on Windows, I have added "linear threading" (ie. no threading) so you can test the functionality of your tasks on Windows.

Where can I get it?

UPDATE: I've now pushed it to github - http://github.com/ifunk/Multitask for easy downloading...

CakePHP 1.2 Sessions and SWFUpload

Have you ever used SWFUpload to upload more than one file at once without refreshing the page? Of course you have! If you haven't, then I suggest you go read up about it here and here before continuing with this tutorial.

One issue many people run into when using SWFUpload with CakePHP is that sessions are often lost in the upload process. Fortunately there is a way to prevent these incessant session-less side-effects.

Read more

Sorting with Set::sort() in CakePHP 1.2

You probably already know that CakePHP's Set class is one of the most useful additions to CakePHP 1.2. What may have slipped through the cracks is that you can now sort arrays very easily using a new method - Set::sort().

Read more

Drag and drop using Ext JS with the CakePHP Tree Behavior

If you've ever needed to store nested or recursive data you'll realise how much of a pain it can be. Fortunately for us cake bakers we no longer need to shy away from these data structures to maintain our sanity. With CakePHP's Tree Behavior you can easily add this functionality to any of your models!

Getting data in and out of our tree models is fairly easy using the methods provided, but re-ordering existing data can be frustrating without a GUI. Enter stage left... Ext JS!

This tutorial will explain how to use the Ext JS Tree component to allow you to re-order your tree data using drag-and-drop operations.

See it in action!

Ext JS Tree Drag Example

Read more