OSB enhancements to date
This commit is contained in:
@@ -27,52 +27,64 @@
|
||||
*
|
||||
* @package AgileBill
|
||||
* @subpackage Module:Task
|
||||
* @todo Change the debug printing to use central debugging calls ie: C_alert
|
||||
*/
|
||||
class task extends OSB_module {
|
||||
# Schedule Task ID
|
||||
private $id = null;
|
||||
# Does this task need authorisation
|
||||
private $noauth = false;
|
||||
|
||||
/**
|
||||
* Run all scheduled tasks
|
||||
*
|
||||
* @uses cron;
|
||||
*/
|
||||
public function run_all() {
|
||||
include_once(PATH_INCLUDES.'cron/cron.inc.php');
|
||||
$cron = new cron;
|
||||
|
||||
# Ensure that tasks complete and dont hang on running=1
|
||||
set_time_limit(2*60*60);
|
||||
|
||||
# Loop through the tasks:
|
||||
global $VAR;
|
||||
foreach ($this->sql_GetRecords(array('where'=>'(running=0 OR running IS NULL) AND status=1')) as $result) {
|
||||
# Initialise the task
|
||||
$this->id = $result['id'];
|
||||
$this->noauth = false;
|
||||
|
||||
$db = &DB();
|
||||
$result = $db->Execute(sqlSelect($db,'task','*','(running=0 OR running IS NULL) AND status=1'));
|
||||
if ($result && $result->RecordCount() > 0) {
|
||||
include_once(PATH_INCLUDES.'cron/cron.inc.php');
|
||||
$cron = new cron;
|
||||
if ($this->debug)
|
||||
printf("%s: Selecting Job [%s] (%s)\n",__METHOD__,$result['command'],$this->id);
|
||||
|
||||
while (! $result->EOF) {
|
||||
$_r = false;
|
||||
$_s = (int) $result->fields['date_start'];
|
||||
$_e = (int) $result->fields['date_expire'];
|
||||
$_l = (int) $result->fields['date_run'];
|
||||
$_c = sprintf('%s %s %s %s %s',
|
||||
$result->fields['int_min'],
|
||||
$result->fields['int_hour'],
|
||||
$result->fields['int_month_day'],
|
||||
$result->fields['int_month'],
|
||||
$result->fields['int_week_day']);
|
||||
$_n = (int) time();
|
||||
if (! $_l > 0)
|
||||
$_l = $_n-86400*365;
|
||||
$task = array();
|
||||
$task['start'] = (int)$result['date_start'];
|
||||
$task['end'] = (int)$result['date_expire'];
|
||||
$task['lastrun'] = (int)$result['date_run'];
|
||||
$task['cron'] = sprintf('%s %s %s %s %s',
|
||||
$result['int_min'],
|
||||
$result['int_hour'],
|
||||
$result['int_month_day'],
|
||||
$result['int_month'],
|
||||
$result['int_week_day']);
|
||||
$task['now'] = (int)time();
|
||||
|
||||
# Verify it has not expired:
|
||||
if ($_s <= $_n || $_s == '' || $_s == '0') {
|
||||
# Verify it is past the start date:
|
||||
if ($_e >= $_n || $_e == '' || $_e == '0') {
|
||||
# Verify that it is time to run:
|
||||
if ($cron->due($_l,$_n,$_c)) {
|
||||
# Run the task:
|
||||
$this->id = $result->fields['id'];
|
||||
$this->run($VAR);
|
||||
}
|
||||
# Default the last run time, if it isnt set
|
||||
if (! $task['lastrun'] > 0)
|
||||
$task['lastrun'] = $task['now']-86400*365;
|
||||
|
||||
# Verify it has not expired:
|
||||
if ($task['start'] <= $task['now'] || $task['start'] == '' || $task['start'] == '0') {
|
||||
# Verify it is past the start date:
|
||||
if ($task['end'] >= $task['now'] || $task['end'] == '' || $task['end'] == '0') {
|
||||
# Verify that it is time to run:
|
||||
if ($cron->due($task['lastrun'],$task['now'],$task['cron'])) {
|
||||
# Run the task:
|
||||
if ($this->debug)
|
||||
printf("%s: Running Job [%s] (%s)\n",__METHOD__,$result['command'],$this->id);
|
||||
|
||||
$this->run($this->VAR);
|
||||
}
|
||||
}
|
||||
$result->MoveNext();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -83,97 +95,93 @@ class task extends OSB_module {
|
||||
* @uses task_log
|
||||
*/
|
||||
public function run($VAR) {
|
||||
global $VAR,$C_auth,$_ENV,$_SERVER,$_COOKIE,$C_list;
|
||||
global $VAR,$C_auth,$_ENV,$_SERVER,$_COOKIE,$C_list,$C_method;
|
||||
$db = &DB();
|
||||
|
||||
$noauth = false;
|
||||
$debug_out = false;
|
||||
# Check if we are on a console, we'll debug output if we are
|
||||
if (isset($_ENV['S']) ||
|
||||
(isset($_ENV['SESSIONNAME']) && $_ENV['SESSIONNAME'] == 'Console') ||
|
||||
(isset($_SERVER['SESSIONNAME']) && $_SERVER['SESSIONNAME'] == 'Console') ||
|
||||
(isset($_SERVER['CLIENTNAME']) && $_SERVER['CLIENTNAME'] == 'Console') ||
|
||||
empty($_COOKIE)) {
|
||||
$debug_out = true;
|
||||
$noauth = true;
|
||||
|
||||
$this->debug = true;
|
||||
$this->noauth = true;
|
||||
|
||||
# Can this task be run without authorisation
|
||||
} elseif($C_auth->auth_method_by_name('task','run')) {
|
||||
$noauth = true;
|
||||
|
||||
} else {
|
||||
$noauth = false;
|
||||
$this->noauth = true;
|
||||
}
|
||||
|
||||
if (isset($this->id))
|
||||
$id = $this->id;
|
||||
elseif (isset($VAR['id']))
|
||||
$id = $VAR['id'];
|
||||
else
|
||||
return;
|
||||
if (! isset($this->id)) {
|
||||
if (isset($VAR['id']))
|
||||
$this->id = $VAR['id'];
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
# Get task details
|
||||
$db = &DB();
|
||||
$result = $db->Execute(sqlSelect($db,'task','*',array('id'=>$id)));
|
||||
|
||||
if (! $result || $result->RecordCount() == 0)
|
||||
return;
|
||||
|
||||
$type = $result->fields['type'];
|
||||
$cmd = $result->fields['command'];
|
||||
$log = $result->fields['log'];
|
||||
if (! $task = $this->sql_GetRecords(array('where'=>array('id'=>$this->id))))
|
||||
return false;
|
||||
$task = array_shift($task);
|
||||
|
||||
# Record task running
|
||||
$db->Execute(sqlUpdate($db,'task',array('running'=>1),array('id'=>$id)));
|
||||
$db->Execute(sqlUpdate($db,'task',array('running'=>1),array('id'=>$this->id)));
|
||||
|
||||
if ($this->debug)
|
||||
printf("%s: Running Task [%s]\n",__METHOD__,$task['command']);
|
||||
|
||||
# Run task
|
||||
switch ($type) {
|
||||
switch ($task['type']) {
|
||||
# Internal function
|
||||
case 0:
|
||||
# Internal function:
|
||||
global $C_method;
|
||||
$arr = explode(":",$cmd);
|
||||
$cm = explode(':',$task['command']);
|
||||
|
||||
if ($noauth) {
|
||||
# run from console, no auth req
|
||||
$C_method->exe_noauth($arr[0],$arr[1]);
|
||||
} else {
|
||||
# run from web, auth required
|
||||
$C_method->exe($arr[0],$arr[1]);
|
||||
}
|
||||
if ($this->noauth)
|
||||
$C_method->exe_noauth($cm[0],$cm[1]);
|
||||
else
|
||||
$C_method->exe($cm[0],$cm[1]);
|
||||
|
||||
if ($C_method->result)
|
||||
$result = 1;
|
||||
else
|
||||
$result = 0;
|
||||
|
||||
@$message = $C_method->error;
|
||||
$message = isset($C_method->error) ? $C_method->error : '';
|
||||
|
||||
break;
|
||||
|
||||
# Run System Command:
|
||||
case 1:
|
||||
$message = `$cmd`;
|
||||
$message = `{$task['command']}`;
|
||||
$result = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
printf('ERROR: Unknown task type [%s]',$type);
|
||||
printf('ERROR: Unknown task type [%s]',$result['type']);
|
||||
}
|
||||
|
||||
# Update last run date & flip run toggle
|
||||
$db->Execute(sqlUpdate($db,'task',array('running'=>0,'date_run'=>time()),array('id'=>$id)));
|
||||
$db->Execute(sqlUpdate($db,'task',array('running'=>0,'date_run'=>time()),array('id'=>$this->id)));
|
||||
|
||||
# Store task log if required
|
||||
if ($log && $C_list->is_installed('task_log')) {
|
||||
if ($task['log'] && $C_list->is_installed('task_log')) {
|
||||
include_once(PATH_MODULES.'task_log/task_log.inc.php');
|
||||
$tl = new task_log;
|
||||
|
||||
$VAR['task_log_task_id'] = $id;
|
||||
$VAR['task_log_result'] = $result;
|
||||
$VAR['task_log_message'] = $message;
|
||||
if (! isset($VAR['_page_current']))
|
||||
$VAR['_page_current'] = '';
|
||||
$result = $tl->add($VAR);
|
||||
$tl->add(array(
|
||||
'task_log_task_id'=>$this->id,
|
||||
'task_log_result'=>$result,
|
||||
'task_log_message'=>$message,
|
||||
'_page_current'=>isset($VAR['_page_current']) ? $VAR['_page_current'] : '',
|
||||
'_noredirect'=>1
|
||||
));
|
||||
}
|
||||
|
||||
# If admin, print success message
|
||||
if (DEFAULT_ADMIN_THEME == SESS_THEME) {
|
||||
global $C_translate,$C_debug;
|
||||
|
||||
$C_debug->alert($C_translate->translate('true','',''));
|
||||
}
|
||||
}
|
||||
|
@@ -135,13 +135,13 @@
|
||||
</running>
|
||||
</field>
|
||||
|
||||
<!-- Methods for this class, and the fields they have access to, if applicable. -->
|
||||
<!-- Methods for this class, and the fields they have access to, if applicable -->
|
||||
<method>
|
||||
<add>date_start,date_expire,date_last,name,description,log,type,command,int_min,int_hour,int_month_day,int_month,int_week_day</add>
|
||||
<update>id,site_id,status,date_start,date_expire,date_last,name,description,log,type,command,int_min,int_hour,int_month_day,int_month,int_week_day,running</update>
|
||||
<delete>id,site_id,date_start,date_expire,date_last,name,description,log,type,command,int_min,int_hour,int_month_day,int_month,int_week_day,running</delete>
|
||||
<view>id,site_id,status,date_start,date_expire,date_last,name,description,log,type,command,int_min,int_hour,int_month_day,int_month,int_week_day,running</view>
|
||||
<search>id,site_id,status,date_start,date_expire,date_last,name,description,log,type,command,int_min,int_hour,int_month_day,int_month,int_week_day,running</search>
|
||||
<add>date_start,date_expire,name,description,type,command,int_min,int_hour,int_month_day,int_month,int_week_day</add>
|
||||
<update>id,name,status,log,date_start,date_expire,description,type,command,int_min,int_hour,int_month_day,int_month,int_week_day,running</update>
|
||||
<delete>id</delete>
|
||||
<view>id,name,date_last,date_run,status,log,date_start,date_expire,description,type,command,int_min,int_hour,int_month_day,int_month,int_week_day,running</view>
|
||||
<search>id,name,date_last,date_run,command,status</search>
|
||||
</method>
|
||||
|
||||
<!-- Method triggers -->
|
||||
|
@@ -14,6 +14,8 @@
|
||||
<notes><![CDATA[This module allows you to automate various tasks]]></notes>
|
||||
<!-- MODULE Parent, the parent node in the tree -->
|
||||
<parent>setup</parent>
|
||||
<!-- SUB Modules to install with this one -->
|
||||
<sub_modules></sub_modules>
|
||||
<!-- MODULE Type (core|base), core modules cannot be deleted, unrecognised types are ignored. -->
|
||||
<type>base</type>
|
||||
</module_properties>
|
||||
|
Reference in New Issue
Block a user