Open Source Billing

This commit is contained in:
Deon George
2013-10-10 13:44:53 +11:00
commit b02d70adf0
2344 changed files with 392978 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class provides Admin TASK management
*
* @package Task
* @category Controllers/Admin
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Controller_Admin_Task extends Controller_TemplateDefault_Admin {
protected $secure_actions = array(
'listlog'=>TRUE,
);
/**
* Show a list of tasks run
*/
public function action_listlog() {
Block::add(array(
'title'=>_('Task Log'),
'body'=>Table::display(
ORM::factory('Task_Log')->order_by('id','DESC')->find_all(),
25,
array(
'id'=>array('label'=>'ID','url'=>URL::link('admin','task/view/')),
'date_orig'=>array('label'=>'Date'),
'task->display("name")'=>array('label'=>'Task'),
'result'=>array('label'=>'Result'),
'message'=>array('label'=>'Message'),
),
array(
'page'=>TRUE,
)),
));
}
}
?>

View File

@@ -0,0 +1,107 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Tasks
*
* @package Task
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Task extends ORM_OSB {
protected $_display_filters = array(
'date_run'=>array(
array('Config::datetime',array(':value')),
),
);
public function run($force=FALSE) {
$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 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 **/
public function list_active() {
$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

@@ -0,0 +1,23 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class supports Task Logging
*
* @package Task
* @category Models
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Model_Task_Log extends ORM_OSB {
protected $_belongs_to = array(
'task'=>array(),
);
protected $_display_filters = array(
'date_orig'=>array(
array('Config::datetime',array(':value')),
),
);
}
?>

View File

@@ -0,0 +1,35 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* List all Active Tasks defined in the DB
*
* @package Task
* @category Tasks
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Task_Listactive extends Task {
protected function _execute(array $params) {
$output = '';
$output .= sprintf('%2s %30s %21s %21s %40s',
'ID','Command','Last Run','Next Run','Description');
$output .= "\n";
foreach (ORM::factory('Task')->list_active() 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";
};
echo $output;
}
}
?>

View File

@@ -0,0 +1,46 @@
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Run active tasks according to their CRON schedule
*
* @package Task
* @category Tasks
* @author Deon George
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
*/
class Task_Task_Run extends Task {
protected function _execute(array $params) {
if ($params['id']) {
$to = ORM::factory('Task',$params['id']);
if ($to->loaded()) {
if (! $to->status)
throw new Minion_Exception_InvalidTask('Task :task (:name) NOT active',array(':task'=>$params['id'],':name'=>$to->name));
if (! Kohana::$config->load('debug')->task_sim)
$to->run($params['force']);
else
printf('Would Run task: (%s) %s',$params['id'],$to->name);
echo "\n";
} else
throw new Minion_Exception_InvalidTask('Unknown task :task',array(':task'=>$params['id']));
} else {
$tlo = ORM::factory('Task');
$t = time();
foreach ($tlo->list_active() as $to)
if ($to['next'] < $t) {
if (! Kohana::$config->load('debug')->task_sim)
$to['task']->run();
else
printf('Would Run task: (%s) %s',$to['task']->id,$to['task']->name);
echo "\n";
}
}
}
}
?>