2011-08-16 02:27:19 +00:00
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class supports Tasks
|
|
|
|
*
|
|
|
|
* @package OSB
|
|
|
|
* @subpackage Task
|
|
|
|
* @category Models
|
|
|
|
* @author Deon George
|
|
|
|
* @copyright (c) 2010 Open Source Billing
|
|
|
|
* @license http://dev.osbill.net/license.html
|
|
|
|
*/
|
2012-12-10 21:48:30 +00:00
|
|
|
class Model_Task extends ORM_OSB {
|
2011-08-16 02:27:19 +00:00
|
|
|
protected $_display_filters = array(
|
|
|
|
'date_run'=>array(
|
|
|
|
array('Config::datetime',array(':value')),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
public function run() {
|
|
|
|
$r = rand(0,9999);
|
2012-11-09 23:13:57 +00:00
|
|
|
$tlo = ORM::factory('Task_Log');
|
2011-08-16 02:27:19 +00:00
|
|
|
$tlo->task_id = $this->id;
|
|
|
|
|
|
|
|
if (! $this->loaded())
|
|
|
|
$tlo->message = sprintf('Unknown Task ID %s',$this->id);
|
|
|
|
|
|
|
|
elseif (! $this->status)
|
|
|
|
$tlo->message = sprintf('Task %s is not active',$this->id);
|
|
|
|
|
|
|
|
elseif ($this->running)
|
|
|
|
$tlo->message = sprintf('Task %s is already running',$this->id);
|
|
|
|
|
|
|
|
elseif (! preg_match('/\//',$this->command))
|
|
|
|
$tlo->message = sprintf('Task %s uses the old configuration, ignoring :command',$this->id,$this->command);
|
|
|
|
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
// Get a lock
|
|
|
|
$this->running = 1;
|
|
|
|
$this->running_host = $r;
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
// Check we are the winning host to run this task
|
2011-09-28 06:46:22 +00:00
|
|
|
// @todo We need to test that the lock is not stale
|
2011-08-16 02:27:19 +00:00
|
|
|
$this->reload();
|
|
|
|
if ($this->running_host != $r)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch ($this->type) {
|
|
|
|
case 0:
|
|
|
|
$r = Request::factory($this->command)->execute();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new Kohana_Exception('Unknown task type :type',array(':type'=>$this->type));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear our lock and update the last run time
|
|
|
|
$this->date_run = time();
|
|
|
|
$this->running = 0;
|
|
|
|
$this->running_host = NULL;
|
|
|
|
|
|
|
|
$tlo->result = 0;
|
|
|
|
$tlo->message = $r->body();
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
$tlo->result = $e->getCode();
|
2012-01-02 01:35:47 +00:00
|
|
|
$tlo->message = Kohana_Exception::text($e);
|
2011-08-25 23:21:39 +00:00
|
|
|
|
2012-01-02 01:35:47 +00:00
|
|
|
$this->running = 1;
|
|
|
|
$this->running_host = 'ERROR';
|
2011-08-16 02:27:19 +00:00
|
|
|
}
|
2012-01-02 01:35:47 +00:00
|
|
|
|
|
|
|
$this->save();
|
2011-08-16 02:27:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->log)
|
|
|
|
$tlo->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** LIST FUNCTIONS **/
|
|
|
|
|
|
|
|
public function list_active() {
|
|
|
|
$return = array();
|
|
|
|
|
2012-08-01 12:43:33 +00:00
|
|
|
foreach ($this->_where_active()->find_all() as $to) {
|
2011-08-16 02:27:19 +00:00
|
|
|
$ct = sprintf('%s %s %s %s %s',$to->int_min,$to->int_hour,$to->int_month_day,$to->int_month,$to->int_week_day);
|
|
|
|
|
|
|
|
$c = new Cron($ct,$to->command);
|
|
|
|
$return[$to->id]['task'] = $to;
|
|
|
|
$return[$to->id]['next'] = $c->next($to->date_run);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function list_next() {
|
|
|
|
$return = array();
|
|
|
|
|
|
|
|
foreach ($this->list_active() as $v)
|
|
|
|
if ((! $return OR $v['next']<$return['next']) AND ! $v['task']->running)
|
|
|
|
$return = $v;
|
|
|
|
|
|
|
|
return array($return['task']->id=>$return);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|