This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/modules/task/classes/model/task.php

112 lines
2.5 KiB
PHP
Raw Normal View History

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
*/
class Model_Task extends ORMOSB {
protected $_display_filters = array(
'date_run'=>array(
array('Config::datetime',array(':value')),
),
);
public function run() {
$r = rand(0,9999);
$tlo = ORM::factory('task_log');
$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
$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;
$this->save();
$tlo->result = 0;
$tlo->message = $r->body();
}
catch (Exception $e) {
$tlo->result = $e->getCode();
$tlo->message = $e->getMessage();
2011-08-25 23:21:39 +00:00
$this->running = 0;
$this->running_host = NULL;
$this->save();
2011-08-16 02:27:19 +00:00
}
}
if ($this->log)
$tlo->save();
}
/** LIST FUNCTIONS **/
private function _list_active() {
return $this->where('status','=',1)->find_all();
}
public function list_active() {
$return = array();
foreach ($this->_list_active() as $to) {
$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);
}
}
?>