Enhancements to Task

This commit is contained in:
Deon George 2013-05-02 19:52:19 +10:00
parent 04ebda2aaa
commit e73ff93a70
8 changed files with 162 additions and 122 deletions

View File

@ -23,7 +23,49 @@ class Menu {
if (empty($result[$mmo->id])) if (empty($result[$mmo->id]))
$result[$mmo->id] = $mmo; $result[$mmo->id] = $mmo;
return static::collapse($result);
}
private static function collapse(array $array) {
$result = array();
foreach ($array as $item) {
if (isset($result[$item->module->name])) {
if (! is_array($result[$item->module->name]))
$result[$item->module->name] = array($result[$item->module->name]);
array_push($result[$item->module->name],$item);
continue;
} else {
$result[$item->module->name] = $item;
}
}
return $result; return $result;
} }
public static function ul(array $result,array $append=NULL,$sub=FALSE) {
$output = $sub ? '<ul class="dropdown-menu">' : '<ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">';
foreach ($result as $k => $v)
if (is_array($v)) {
$output .= sprintf('<li class="dropdown-submenu">%s%s',HTML::anchor('#',$k,array('nocg'=>TRUE)),static::ul($v,NULL,TRUE).'</li>');
} else
$output .= '<li>'.HTML::anchor($v->url(),$v->menu_display(),array('tabindex'=>-1,'nocg'=>TRUE)).'</li>';
if ($append) {
$output .= '<li class="divider"></li>';
foreach ($append as $k => $v)
$output .= sprintf('<li>%s</li>',HTML::anchor($k,$v,array('nocg'=>TRUE)));
}
$output .= '</ul>';
return $output;
}
} }
?> ?>

View File

@ -10,6 +10,8 @@
* @license http://dev.osbill.net/license.html * @license http://dev.osbill.net/license.html
*/ */
class Controller_Task_Invoice extends Controller_Task { class Controller_Task_Invoice extends Controller_Task {
public $auto_render = FALSE;
/** /**
* Email a list of invoice balances * Email a list of invoice balances
* *

View File

@ -9,19 +9,21 @@
* @copyright (c) 2009-2013 Open Source Billing * @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html * @license http://dev.osbill.net/license.html
*/ */
class Controller_Admin_Task extends Controller_TemplateDefault_Admin { class Controller_Admin_Task extends Controller_Task {
protected $auth_required = TRUE;
protected $secure_actions = array( protected $secure_actions = array(
'listlog'=>TRUE, 'log'=>TRUE,
); );
/** /**
* Show a list of tasks run * Show a list of tasks run
*/ */
public function action_listlog() { public function action_log() {
Block::add(array( Block::factory()
'title'=>_('Task Log'), ->title(_('Task Log'))
'body'=>Table::display( ->body(Table::display(
ORM::factory('Task_Log')->order_by('id','DESC')->find_all(), ORM::factory('Task_Log')->find_all(),
25, 25,
array( array(
'id'=>array('label'=>'ID','url'=>URL::link('admin','task/view/')), 'id'=>array('label'=>'ID','url'=>URL::link('admin','task/view/')),
@ -32,8 +34,7 @@ class Controller_Admin_Task extends Controller_TemplateDefault_Admin {
), ),
array( array(
'page'=>TRUE, 'page'=>TRUE,
)), )));
));
} }
} }
?> ?>

View File

@ -0,0 +1,15 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Admin TASK management
*
* @package Task
* @category Controllers
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Task extends Controller_TemplateDefault {
protected $auth_required = FALSE;
}
?>

View File

@ -16,92 +16,17 @@ class Model_Task extends ORM_OSB {
), ),
); );
public function run($force=FALSE) { private function _cron() {
$r = rand(0,9999); return new Cron(sprintf('%s %s %s %s %s',$this->int_min,$this->int_hour,$this->int_month_day,$this->int_month,$this->int_week_day),$this->command);
$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 AND ! $force)
$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
// @todo We need to test that the lock is not stale
$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();
$tlo->message = Kohana_Exception::text($e);
$this->running = 1;
$this->running_host = 'ERROR';
}
$this->save();
}
if ($this->log)
$tlo->save();
} }
/** LIST FUNCTIONS **/ /**
* Calculate next run date
*/
public function next_run($format=FALSE) {
$x = $this->_cron()->next($this->date_run);
public function list_active() { return $format ? Config::datetime($x) : $x;
$result = array();
foreach ($this->_where_active()->find_all() 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);
$result[$to->id]['task'] = $to;
$result[$to->id]['next'] = $c->next($to->date_run);
}
return $result;
}
public function list_next() {
$result = array();
foreach ($this->list_active() as $v)
if ((! $result OR $v['next']<$result['next']) AND ! $v['task']->running)
$result = $v;
return array($result['task']->id=>$result);
} }
} }
?> ?>

View File

@ -10,6 +10,10 @@
* @license http://dev.osbill.net/license.html * @license http://dev.osbill.net/license.html
*/ */
class Model_Task_Log extends ORM_OSB { class Model_Task_Log extends ORM_OSB {
protected $_sorting = array(
'id'=>'DESC',
);
protected $_belongs_to = array( protected $_belongs_to = array(
'task'=>array(), 'task'=>array(),
); );

View File

@ -11,25 +11,20 @@
*/ */
class Task_Task_Listactive extends Task { class Task_Task_Listactive extends Task {
protected function _execute(array $params) { protected function _execute(array $params) {
$output = ''; $header = "%2s %30s %21s %21s %40s\n";
$output .= sprintf('%2s %30s %21s %21s %40s', $output = sprintf($header,'ID','Command','Last Run','Next Run','Description');
'ID','Command','Last Run','Next Run','Description');
$output .= "\n";
foreach (ORM::factory('Task')->list_active() as $t) { foreach (ORM::factory('Task')->list_active() as $to)
$output .= sprintf('%2s %30s %21s %21s %40s', $output .= sprintf($header,
$t['task']->id, $to->id,
$t['task']->command, $to->command,
$t['task']->display('date_run'), $to->display('date_run'),
Config::datetime($t['next']), $to->next_run(TRUE),
$t['task']->display('description') $to->display('description')
); );
$output .= "\n"; return $output;
};
echo $output;
} }
} }
?> ?>

View File

@ -18,29 +18,85 @@ class Task_Task_Run extends Task {
if (! $to->status) if (! $to->status)
throw new Minion_Exception_InvalidTask('Task :task (:name) NOT active',array(':task'=>$params['id'],':name'=>$to->name)); throw new Minion_Exception_InvalidTask('Task :task (:name) NOT active',array(':task'=>$params['id'],':name'=>$to->name));
if (! Kohana::$config->load('debug')->task_sim) Kohana::$config->load('debug')->task_sim ? printf("Would Run task: (%s) %s\n",$to->id,$to->name) : $this->_run($to,$params['force']);
$to->run($params['force']);
else
printf('Would Run task: (%s) %s',$params['id'],$to->name);
echo "\n";
} else } else
throw new Minion_Exception_InvalidTask('Unknown task :task',array(':task'=>$params['id'])); throw new Minion_Exception_InvalidTask('Unknown task :task',array(':task'=>$params['id']));
} else { } else {
$tlo = ORM::factory('Task');
$t = time(); $t = time();
foreach ($tlo->list_active() as $to) foreach (ORM::factory('Task')->list_active() as $to)
if ($to['next'] < $t) { if ($to->next_run() < $t)
if (! Kohana::$config->load('debug')->task_sim) Kohana::$config->load('debug')->task_sim ? printf("Would Run task: (%s) %s\n",$to->id,$to->name) : $this->_run($to);
$to['task']->run();
else
printf('Would Run task: (%s) %s',$to['task']->id,$to['task']->name);
echo "\n";
}
} }
} }
private function _run(Model_Task $to,$force=FALSE) {
$r = rand(0,9999);
$tlo = ORM::factory('Task_Log');
$tlo->task_id = $to->id;
if ($to->running AND ! $force)
$tlo->message = sprintf('Task %s is already running',$to->id);
else {
try {
// Get a lock
$to->running = 1;
$to->running_host = $r;
$to->save();
// Check we are the winning host to run this task
// @todo We need to test that the lock is not stale
$to->reload();
if ($to->running_host != $r)
return;
switch ($to->type) {
case 1:
$r = Minion_Task::factory(array('site'=>Minion_CLI::options('site'),'task'=>$to->command))->execute();
$to->running = 0;
$to->running_host = NULL;
$tlo->message = $r;
$tlo->result = 0;
break;
case 0:
$r = Request::factory($to->command)->execute();
if ($r->status() == 200) {
$to->running = 0;
$to->running_host = NULL;
}
$tlo->message = $r->body();
$tlo->result = $r->status();
break;
default:
throw new Kohana_Exception('Unknown task type :type',array(':type'=>$to->type));
}
// Clear our lock and update the last run time
$to->date_run = time();
} catch (Exception $e) {
$tlo->result = $e->getCode();
$tlo->message = Kohana_Exception::text($e);
$to->running = 1;
$to->running_host = 'ERROR';
}
$to->save();
}
if ($to->log)
$tlo->save();
}
} }
?> ?>