class Bgtask_Manager

Class Bgtask_Manager is used for managing background tasks, sending signals to background processes, collecting statistics.

It is necessary to remember that by default, the information on tasks and signals is stored in the MEMORY-table of MySQL database. When working in the developer mode, the system collects statistics of database requests, which leads to memory leak in Zend_Db_Profiler. Disable the developer mode when analysing the progress of a background task implementation in order to learn the actual RAM usage.

class Bgtask_Manager

{

/**
* Instantiate an object
* @return Bgtask_Manager
*/

static public function getInstance();

/**
* Set up storage adapter for background tasks
* @param Bgtask_Storage $storage
*/

public function setStorage(Bgtask_Storage $storage);

/**
* Set up logger adapter
* @param Bgtask_Log $logger
*/

public function setLogger(Bgtask_Log $logger);

/**
* Get logger adapter
*/

public function getLogger();

/**
* Launch a background task
* @param string $launcher - const, task launcher
* @param string $task - task object class name
* @param array $config
*/

public function launch($launcher , $task , array $config)

/**
* Get a list of tasks
* @return array
*/

public function getList()

/**
* Get information on a task by its pid
* @param integer $pid
* @return array
*/

public function get($pid)

/**
* Sends a signal to a background process
* @param integer $pid
* @param integer $signal - const
* @return void
*/

public function signal($pid , $signal)

/**
* Set up task configuration
* @param integer $pid
* @param array $config
* @return void
*/

public function setConfig($pid , array $config)

/**
* Get task configuration
* @param integer $pid
* @return array
*/

public function getConfig($pid)

/**
* Kill a task
* @param integer $pid
* @return boolean
*/

public function kill($pid)

/**
* Get signals for a certain task by its pid
* @param integer $pid
* @param boolean $clean — optional, clean signals upon receival
* @return array();
*/

public function getSignals($pid , $clean = false)

/**
* Remove  signals for a certain task by its pid
* @param integer $pid
* @param array $sigId - optional
* @return array
*/

public function clearSignals($pid , $sigId = false)

/**
* Update task status
* @param integer $pid - identifier
* @param integer $opTotal - the number of expected operations
* @param integer $opFinished — the number of finished operations
* @param integer $status - a constant, status
*/

public function updateState($pid , $opTotal , $opFinished , $status)

/**
* Check if the task is running
* @param integer $pid
* @return boolean
*/

public function isLive($pid)

/**
* Finish (status ‘Finished’)
* @param integer $pid
*/

public function setFinished($pid)

/**
* Stop a task
* @param integer $pid
*/

public function setStoped($pid)

/**
* Define error when running a task
* @param integer $pid
*/

public function setError($pid)

/**
* Start running a task
* @param integer $pid
*/

public function setStarted($pid)

/**
* Add a task and get its identifier (pid)
* @param string $description
* @return integer
*/

public function addTaskRecord($description)

}

A simple example of starting a background task:

//initialize the task storage
$bgStorage = new Bgtask_Storage_Orm(Model::factory('bgtask') , Model::factory('Bgtask_Signal'));
//initialize logger
$logger = new Bgtask_Log_File('./.log/test'.date('YmdHis').'.txt');
// initialize the Background Task Manager
$tManager = Bgtask_Manager::getInstance();
// Set the storage and logger
$tManager->setStorage($bgStorage);
$tManager->setLogger($logger);
// Launch the tasl 'Task_Test'
$pid = $tManager->launch(Bgtask_Manager::LAUNCHER_JSON, 'Task_Test' , array());

comments powered by Disqus