PhpTaskDaemon: Defining Tasks

Last blog posts I introduced the PhpTaskDaemon project and provided a current state of alternatives for running php in the background. This time I will provide the basic concept for creating PhpTaskDaemon. First of all I start with some (useful) examples for running php in the background. Before implementing the example using the PhpTaskDaemon project I will list one or more alternative ways to implement the task.

PhpTaskDaemon provides a more generic solution for defining and running php background. A single command is provided to start and monitor multiple tasks with different behaviour. The task abstract class of the project only defines how tasks are loaded from a queue and how a single task will be execute. The class also provides methods to inform the daemon about its current activities. The way task queues and loaded and executed will be handled by a manager. The project contains different default managers like a interval, cron, gearman and forked manager.

The daemon
The PhpTaskDaemon project includes a single script to start a daemon for running multiple tasks. By default it uses a single configuration and log file. This script is also capable of viewing the current activities of the daemon.

Defining the task class
The PhpTaskDaemon project includes an abstract Task class and interface for used implementing tasks. The interface only defines two methods: loadQueue and executeTask. The first method is used by the manager for loading a queue. The second method executes a single task once. It is used by the manager to execute all task in the queue. The manager class defines if these tasks are executed in sequentially, in parallel or as gearman worker. The abstract class provides a set of methods to inform the daemon about the current status during the execution of a task.

{code type=php}
class Dew_Daemon_Task_Example
extends Dew_Daemon_Task_Abstract
implements Dew_Daemon_Task_Interface {

static protected $_managerType =
Dew_Daemon_Manager_Abstract::PROCESS_TYPE_INTERVAL;

public function loadTasks() {
}
public function executeTasks() {
}
}
{/code}

The managers
The project is distributed with a set of default managers. The interval manager is the default. It loads all tasks, executes them sequentially and then waits for a number of seconds before restarting. The cron manager works the same, except it waits untill the cron time definition matches the current time. The forked manager can be used to run tasks in parallel. The cron manager loads all tasks and forks the process x times to execute tasks. When all tasks are finished it waits for a predefined amount of seconds before reloading the queue. The fourth manager can be used to start gearman workers. This manager does not use the loadTasks method, because the queue is handled by the gearman job server.  To change a task to run with a certain manager the following line can be used:

{code type=php}
static protected $_managerType =
Dew_Daemon_Manager_Abstract::PROCESS_TYPE_CRON;
{/code}

Simple task example
The most basic example, also included in the package, loads a set of tasks randomly and executes a single task by sleeping for a certain amount of time.

Loading a task queue
{code type=php}
public function loadTasks() {
$queue = array();
if (count($queue)==0) {
for ($i=0; $i<rand(0,10); $i++) {
array_push($queue, array(‘taskId’ => $i, ‘sleepTime’ => rand(1000, 500000)));
}
}
return $queue;
}
{/code}

Executing a single task
{code type=php}
public function executeTask() {
$inputData = $this->getTaskInput();
$randomString = substr(md5(uniqid()), 0,10);

for ($i=1; $i<10; $i++) {
usleep($inputData[‘sleepTime’]);
$this->updateMemoryTask(($i*10), ‘Task data: ‘ . $randomString);
}
return true;
}
{/code}

Blog posts in this series
This blog post is an item in a series of blog posts about the development of the PhpTaskDaemon project. Follow the real progress of the project on the GitHub project page. The following blog posts of this series have been published in the past:
* An introduction of the PhpTaskDaemon project
* The current state of running PHP scripts using the command line
* Defining Tasks (this post)

Next time I will blog about the following aspects of the PlugWeb system.
* Running and monitoring the daemon
* Building managers: shared memory, semaphores and sockets
* Creating a small web front end for monitoring the daemon

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Share/Bookmark
This entry was posted in Algemeen, PHP, PhpTaskDaemon, Projects and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *