Poor mans Cron Component March 27, 2006
Posted by rossoft in CakePHP.trackback
You must hack it a bit, because I use some constants and ‘configuracion’ component for storing the interval dates.
Usage: in beforeFilter() of AppController:
$this->cron->add(’/mycontroller/cleanup’);
<?php
/*
* Cron component
* Executes tasks after rendering the output to the user at
* certains time intervals.
*
* @author RosSoft
* @version 0.1
* @license MIT
*
*/
class CronComponent extends Object
{
var $controller;
var $tareas=array();
var $components=array(’configuracion’,'requestHandler’);
function startup(&$controller)
{
$this->controller=$controller;
}
/**
* Adds a task to the list
* @param string $tarea CakeURL of the action (/controller/action)
*/
function add($accion)
{
$this->tareas[]=$accion;
}
function __destruct()
{
if (CONFIG_CRON_INTERVAL) //if 0 then disabled
{
$lastrun=$this->configuracion->value(’CONFIG_CRON_LASTRUN’);
$nextrun = $lastrun + 60 * CONFIG_CRON_INTERVAL;
if (time()>$nextrun)
{
flush();
if (! isset($this->requestHandler)
|| ! $this->requestHandler->isAjax())
{
$this->_execute_tasks();
}
}
}
}
/**
* Executed after flushed the page to the user, it the
* call isn’t ajax.
* It must call the programmed tasks if the timeout has
* expired.
*/
function _execute_tasks()
{
//a near future date for auto error recovering
$this->configuracion->update(’CONFIG_CRON_LASTRUN’,time() - CONFIG_CRON_RETRY_INTERVAL);
foreach ($this->tareas as $t)
{
@$this->controller->requestAction($t);
}
$this->configuracion->update(’CONFIG_CRON_LASTRUN’,time()+CONFIG_CRON_INTERVAL);
}
}
I dont suppose you can give an example of what the configuation file looked like? thanks
or maybe the whole configuration component
just after some ideas