Added Tasks to KH

This commit is contained in:
Deon George
2011-08-16 12:27:19 +10:00
parent f272bc254d
commit 4c9b214ff7
53 changed files with 773 additions and 170 deletions

View File

@@ -0,0 +1,59 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides OSB task running capabilities.
*
* @package OSB
* @subpackage Task
* @category Controllers/Admin
* @author Deon George
* @copyright (c) 2010 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Task_Task extends Controller_Task {
/**
* List all tasks
*/
public function action_list() {
$output = '';
$to = ORM::factory('task');
$tm = 'list_'.$this->request->param('id');
if (! method_exists($to,$tm))
throw new Kohana_Exception('Unknown Task List command :command',array(':command'=>$mode));
$output .= sprintf('%2s %30s %21s %21s %40s',
'ID','Command','Last Run','Next Run','Description');
$output .= "\n";
foreach ($to->$tm() as $t) {
$output .= sprintf('%2s %30s %21s %21s %40s',
$t['task']->id,
$t['task']->command,
$t['task']->display('date_run'),
Config::datetime($t['next']),
$t['task']->display('description')
);
$output .= "\n";
};
$this->response->body($output);
}
public function action_run() {
if ($id = $this->request->param('id')) {
$to = ORM::factory('task',$id);
$to->run();
} else {
$tlo = ORM::factory('task');
$t = time();
foreach ($tlo->list_active() as $to)
if ($to['next'] < $t)
$to['task']->run();
}
}
}
?>

View File

@@ -0,0 +1,107 @@
<?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();
}
}
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);
}
}
?>

View File

@@ -0,0 +1,20 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Task Logging
*
* @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_Log extends ORMOSB {
protected $_display_filters = array(
'date_orig'=>array(
array('Config::datetime',array(':value')),
),
);
}
?>

View File

@@ -57,8 +57,8 @@ class task extends OSB_module {
printf("%s: Selecting Job [%s] (%s)\n",__METHOD__,$result['command'],$this->id);
$task = array();
$task['start'] = (int)$result['date_start'];
$task['end'] = (int)$result['date_expire'];
$task['start'] = 0;
$task['end'] = 0;
$task['lastrun'] = (int)$result['date_run'];
$task['cron'] = sprintf('%s %s %s %s %s',
$result['int_min'],