Skip to content
Permalink
Browse files

Removed "robot" logics and replaced with maintenance job execution

  • Loading branch information...
tvannini committed Sep 17, 2019
1 parent 29cee81 commit fce28b868a32b4b4d409af500232f08c8cd37363
Showing with 127 additions and 261 deletions.
  1. +45 −10 lib/jxapp.inc
  2. +0 −169 lib/jxcore.inc
  3. +0 −24 lib/jxfnx.inc
  4. +20 −20 lib/prgs/jxjobs.prf
  5. +23 −23 lib/prgs/jxjobs.prg
  6. +11 −0 lib/prgs/jxmaintenance.prf
  7. +12 −0 lib/prgs/jxmaintenance.prg
  8. +2 −1 lib/prgs/o2sys_iniconf.prf
  9. +14 −14 lib/prgs/o2sys_iniconf.prg
@@ -393,12 +393,10 @@ class o2_app {
// ______________________________ Login program execution and user options ___
$this->set_user($_REQUEST['user'], $_REQUEST['password']);
$this->exe_prg_start = ($this->prg_start ? true : false);
// ___________________________________ Register maintenance scheduled task ___
$robot = o2_robot::get_instance();
$robot->add_task("maintenance",
array($this, "maintenance"),
$this->maint_time);
$robot->run();
// ___________________________________________________ Maintenance Service ___
if ($this->maint_time && !isset($_REQUEST[o2_run_job::$run_word])) {
$this->check_maintenance();
}
}
// __________________________________________________ Commit open transactions ___
$this->commit_all(false);
@@ -877,10 +875,41 @@ class o2_app {
}
/**
* Check last maintenance execution and if needed creates and run maintenance batch
* job
*
*/
function check_maintenance() {
$app = $_SESSION['o2_app'];
$job = o2_job::get_by_name('jxmaintenance');
// __________________ Check maintenance job existance and create it if missing ___
if (!$job) {
$job = new o2_job(0,
'jxmaintenance',
'tools/jxmaintenance',
array(),
'Janox Maintenance Service',
ceil($app->maint_time / 24),
1,
'B');
$job_id = $job->create();
}
else {
$job_id = $job->id;
}
// ______________________________ Get last maintenance execution for this host ___
$last_exe = o2_run_job::get_last_job($job_id, '', '', 0, $app->host);
if (!$last_exe || (($last_exe->start + ($app->maint_time * 3600)) < time())) {
$job->run(false);
}
}
/**
* Executes maintenance service actions on application.
* This method is usually called by robot script (see o2_robot).
*
* TODO: Add execution/error logging
*
@@ -1309,7 +1338,7 @@ class o2_app {
$this->max_req_n = max($conf['max_req_n'], 1);
}
if (isset($conf['maint_time'])) {
$this->maint_time = max($conf['maint_time'], 1);
$this->maint_time = intval($conf['maint_time']);
}
if (isset($conf['doc'])) {
$this->doc_enabled = ($conf['doc'] ? true : false);
@@ -4734,9 +4763,14 @@ class o2_run_job {
* @param string $aspid
* @param string $user
* @param integer $sched_id
* @param string $host
* @return o2_run_job
*/
static function get_last_job($job_id, $aspid = '', $user = '', $sched_id = 0) {
static function get_last_job($job_id,
$aspid = '',
$user = '',
$sched_id = 0,
$host = '') {
$app = $_SESSION['o2_app'];
$tab = $app->get_table(self::$run_table);
@@ -4754,7 +4788,8 @@ class o2_run_job {
$aspfld."='".$aspid."')" :
'').
($user ? ' AND '.$f['run_user']."='".$user."'" : '').
($sched_id ? ' AND '.$f['run_sched_id']."=".$sched_id : '');
($sched_id ? ' AND '.$f['run_sched_id']."=".$sched_id : '').
($host ? ' AND '.$f['run_host']."='".$host."'" : '');
$orderby = $f['run_start_date'].' DESC,'.$f['run_start_time'].' DESC';
$run_job = o2_gateway::recordset($server->type,
$server->server,
@@ -7071,175 +7071,6 @@ class o2_dispatcher {
}
/**
* Virtual agent performing scheduled tasks
*
*/
class o2_robot {
private $tasks_list = array(); /* Registered tasks list with execution informations */
private $changed = false; /* If at last one task has been executed */
/**
* Add a new task, named $id, to be executed on $freq.
* Task must be registered on each execution.
*
* @param string $id Task unique identifier
* @param callback $callback Method to call on execution
* @param integer $freq Execution frequency in hours
*/
function add_task($id, $callback, $freq) {
$this->tasks_list[$id] = array('freq' => $freq, 'callback' => $callback);
}
/**
* Executes task callback.
* Method is executed by robot when timestamp gets to frequency.
*
* @param string $id Task to execute unique identifier
*/
private function exe_task($id) {
// _______________________________________________ Check if task is registered ___
if (isset($this->tasks_list[$id])) {
$task = $this->tasks_list[$id];
// _________________________________________ Check if callback is callable ___
if (is_callable($task['callback'])) {
call_user_func($task['callback']);
}
// ___________________________________________ ERROR: not a valid callback ___
else {
throw new o2_exception("Unknown action <i>".print_r($task['callback'], 1).
"</i> for scheduled task <i>".$id."</i>",
o2error_EXECUTION);
}
}
// ________________________________________ ERROR: not a valid registered task ___
else {
throw new o2_exception("Unknown scheduled task <i>".$id."</i>",
o2error_EXECUTION);
}
}
/**
* Checks $timestamp and if older than now ->exe_task($id).
* Method is intended to be used from within robot script.
*
* @param string $id Task to check unique identifier
* @param integer $timestamp Last execution timeout
*/
static function task($id, $timestamp) {
// ________________________________________________________ Get robot instance ___
if (!isset($GLOBALS['jxrobot'])) {
$GLOBALS['jxrobot'] = new o2_robot();
}
$robot = $GLOBALS['jxrobot'];
// _______________________________________________ Check if task is registered ___
if (isset($robot->tasks_list[$id])) {
$robot->tasks_list[$id]['managed'] = true;
// __________________________________________ Check if timeout has expired ___
if ($timestamp < (time() - ($robot->tasks_list[$id]['freq'] * 3600))) {
$robot->changed = true;
$robot->tasks_list[$id]['lastexe'] = time();
$robot->exe_task($id);
}
else {
$robot->tasks_list[$id]['lastexe'] = $timestamp;
}
}
}
/**
* Run robot to include script, execute tasks and produce new script
*
*/
function run() {
// ________________________________________________________ Set all flags down ___
$this->changed = false;
foreach ($this->tasks_list as $task_id => $task) {
$this->tasks_list[$task_id]['managed'] = false;
$this->tasks_list[$task_id]['lastexe'] = false;
}
$app = $_SESSION['o2_app'];
// ____________________________________ Include robot script and execute tasks ___
@include $app->dir_home."robot";
// ________________________________________ Check flags and produce new script ___
$this->end();
}
/**
* Executes end operations: check unmanaged tasks and create new script
*
*/
private function end() {
$app = $_SESSION['o2_app'];
$f_cont = "";
foreach ($this->tasks_list as $task_id => $task) {
if (!$task['managed']) {
$this->changed = true;
$task['lastexe'] = time();
$this->exe_task($task_id);
}
$f_cont.= "o2_robot::task('".$task_id."', ".$task['lastexe']."); /* ".
date("Y M d - H:i", $task['lastexe'])." */\n";
}
if ($this->changed) {
// ____________________________________________________ Write robot script ___
$f_cont = "<"."?php\n/**\n".
" * Janox Robot - Virtual Agent performing scheduled tasks\n".
" * PHP 5\n".
" *\n".
" * This file has been automatically generated.\n".
" *\n".
" * This file is part of Janox.\n".
" * This script contains Janox Robot Service calls.\n".
" * Scheduled tasks will be performed according with settings.\n".
" *\n".
" * @name jxrobot\n".
" * @package jxapp/robot\n".
" * @version 2.2\n".
" */\n".
$f_cont.
"return true;\n".
"?".">\n";
file_put_contents($app->dir_home."robot", $f_cont);
}
}
/**
* If no instance of robot is running create a new one and return it, else return
* existing instance.
*
* @return o2_robot
*/
static function get_instance() {
if (!isset($GLOBALS['jxrobot'])) {
$GLOBALS['jxrobot'] = new o2_robot();
}
return $GLOBALS['jxrobot'];
}
}
/**
* Janox Updates Distribution object class.
* This class check for new runtime/applications releases over the internet and manages
@@ -7377,30 +7377,6 @@ function o2dispatch_activate($id, $preserve = false) {
}
// ====================================== ROBOT ==========================================
/**
* Adds a task to be executed on a certain frequency: each time task is executed a time
* marker is reset to evaluate next frequency expiration.
*
* @package robot
* @param string $id Unique name for the task
* @param integer $freq Execution frequency in hours
* @param string $prg_name Program name to execute for task
*/
function o2robot_task($id, $freq, $prg_name) {
// ______________________________________________________ Register scheduled tasks ___
$robot = o2_robot::get_instance();
/*
// ___________________________________________________________________ Maintenance ___
$robot->add_task($id, array($this, "maintenance"), $this->maint_time);
$robot->run();
*/
}
// ================================= TREEVIEW CONTROL ====================================

0 comments on commit fce28b8

Please sign in to comment.
You can’t perform that action at this time.