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...

Comments

15 Responses to “New CakePHP “multitask” Plugin”

  1. Adrian on April 1st, 2009 12:54 pm

    Thanks a lot!
    Some questions if I can:
    When I run multitasker appers two process with "ps axwww | grep cake.php", thats ok?
    Then when I add tasks the processes multiplies by two... and only died with CTRL-C in multitasker
    Can I not run multitasker, I meen, run once cron by hour an run the scheduled tasks?
    Thanks again!
    PD: Sorry for my english

  2. Adam on April 1st, 2009 1:09 pm

    @Adrian: Although theoretically you could schedule it to run once at hour on cron, it was originally designed to work as a daemon that is executed upon startup, and keeps running continuously in the background.

    It would be fairly straightforward to change the behaviour so it exits immediately after it completes all of its tasks, just change sleep(1); to exit; in the MultitaskerShell.

    I personally haven't experienced the multiplying processes you mentioned, but it's been a little while since I played with this so anythings possible. If you seriously want to use the shell, let me know and I will look into it further.

  3. Adrian (Hardian) on April 3rd, 2009 1:54 pm

    @Adam: Thanks for all!
    I'm using your plugin to process uploaded files.
    Forget about duplicate processes, I have to reserach about it.
    I add tickets in the chaw.
    Thanks again!

  4. Anonymous on November 27th, 2009 9:48 am

    Can I have a download link?

  5. Adam on November 27th, 2009 10:10 am

    @Anonymous: I've pushed it now to github - check the URL in the post.

  6. merovinq on May 8th, 2010 1:18 am

    Nice plugin, this is exactly what I was looking for. Kudos to you sir!