mirror of
https://github.com/e107inc/e107.git
synced 2025-01-17 04:38:27 +01:00
Refactoring for Cron Sheduler.
This commit is contained in:
parent
843bbf5fc3
commit
4472766959
237
cron.php
Normal file → Executable file
237
cron.php
Normal file → Executable file
@ -1,220 +1,41 @@
|
||||
#!/usr/bin/php -q
|
||||
<?php
|
||||
/*
|
||||
+ ----------------------------------------------------------------------------+
|
||||
|| e107 website system
|
||||
|
|
||||
| Copyright (C) 2008-2014 e107 Inc
|
||||
| http://e107.org
|
||||
|
|
||||
|
|
||||
| Released under the terms and conditions of the
|
||||
| GNU General Public License (http://gnu.org).
|
||||
|
|
||||
*/
|
||||
|
||||
// Usage: [full path to this script]cron.php --u=admin --p=password // use your admin login.
|
||||
// test
|
||||
/**
|
||||
* e107 website system
|
||||
*
|
||||
* Copyright (C) 2008-2017 e107 Inc (e107.org)
|
||||
* Released under the terms and conditions of the
|
||||
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
|
||||
*
|
||||
* @file
|
||||
* Handles incoming requests to fire off regularly-scheduled tasks (cron jobs).
|
||||
*
|
||||
* @example
|
||||
* Using wget:
|
||||
* /usr/bin/wget -O - -q http://example.com/cron.php?token=TOKEN > /dev/null 2>&1
|
||||
* Using curl:
|
||||
* /usr/bin/curl --silent --compressed http://example.com/cron.php?token=TOKEN > /dev/null 2>&1
|
||||
* Using lynx:
|
||||
* /usr/bin/lynx -source http://example.com/cron.php?token=TOKEN > /dev/null 2>&1
|
||||
* Using PHP:
|
||||
* /usr/bin/php -q /var/www/example.com/cron.php token=TOKEN
|
||||
* /usr/bin/php -q /var/www/example.com/cron.php TOKEN
|
||||
* Using as Shell script:
|
||||
* /var/www/example.com/cron.php token=TOKEN
|
||||
* /var/www/example.com/cron.php TOKEN
|
||||
*/
|
||||
|
||||
$_E107['cli'] = true;
|
||||
$_E107['debug'] = false;
|
||||
$_E107['no_online'] = true;
|
||||
$_E107['no_forceuserupdate'] = true;
|
||||
$_E107['no_menus'] = true;
|
||||
$_E107['allow_guest'] = true; // allow crons to run while in members-only mode.
|
||||
$_E107['allow_guest'] = true; // allow crons to run while in members-only mode.
|
||||
$_E107['no_maintenance'] = true;
|
||||
|
||||
// we allow theme init as cron jobs might need to access current theme templates (e.g. custom email templates)
|
||||
require_once(realpath(dirname(__FILE__) . "/class2.php"));
|
||||
require_once(e_HANDLER . "cron_class.php");
|
||||
|
||||
|
||||
require_once(realpath(dirname(__FILE__)."/class2.php"));
|
||||
|
||||
|
||||
$pwd = ($_E107['debug'] && $_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : trim($_SERVER['argv'][1]);
|
||||
|
||||
if(!empty($_GET['token']))
|
||||
{
|
||||
$pwd = e107::getParser()->filter($_GET['token']);
|
||||
}
|
||||
else
|
||||
{
|
||||
$pwd = str_replace('token=','',$pwd);
|
||||
}
|
||||
|
||||
if(($pref['e_cron_pwd'] != $pwd) || empty($pref['e_cron_pwd']))
|
||||
{
|
||||
|
||||
if(!empty($pwd))
|
||||
{
|
||||
require_once(e_HANDLER."mail.php");
|
||||
|
||||
$message = "Your Cron Schedule is not configured correctly. Your passwords do not match.
|
||||
<br /><br />
|
||||
Sent from cron: ".$pwd."<br />
|
||||
Stored in e107: ".$pref['e_cron_pwd']."<br /><br />
|
||||
You should regenerate the cron command in admin and enter it again in your server configuration.
|
||||
";
|
||||
|
||||
$message .= "<h2>Debug Info</h2>";
|
||||
$message .= "<h3>_SERVER</h3>";
|
||||
$message .= print_a($_SERVER,true);
|
||||
$message .= "<h3>_ENV</h3>";
|
||||
$message .= print_a($_ENV,true);
|
||||
$message .= "<h3>_GET</h3>";
|
||||
$message .= print_a($_GET,true);
|
||||
|
||||
sendemail($pref['siteadminemail'], "e107 - Cron Schedule Misconfigured.", $message, $pref['siteadmin'],$pref['siteadminemail'], $pref['siteadmin']);
|
||||
}
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
// e107::getCache()->CachePageMD5 = '_';
|
||||
@file_put_contents(e_CACHE.'cronLastLoad.php',time());
|
||||
|
||||
|
||||
|
||||
// from the plugin directory:
|
||||
// realpath(dirname(__FILE__)."/../../")."/";
|
||||
|
||||
$list = array();
|
||||
|
||||
$sql = e107::getDb();
|
||||
if($sql->select("cron",'cron_function,cron_tab','cron_active =1'))
|
||||
{
|
||||
while($row = $sql->fetch())
|
||||
{
|
||||
list($class,$function) = explode("::",$row['cron_function'],2);
|
||||
$key = $class."__".$function;
|
||||
|
||||
$list[$key] = array(
|
||||
'path' => $class,
|
||||
'active' => 1,
|
||||
'tab' => $row['cron_tab'],
|
||||
'function' => $function,
|
||||
'class' => $class
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// foreach($pref['e_cron_pref'] as $func=>$cron)
|
||||
// {
|
||||
// if($cron['active']==1)
|
||||
// {
|
||||
// $list[$func] = $cron;
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
if($_E107['debug'] && $_SERVER['QUERY_STRING'])
|
||||
{
|
||||
echo "<h1>Cron Lists</h1>";
|
||||
print_a($list);
|
||||
}
|
||||
|
||||
require_once(e_HANDLER."cron_class.php");
|
||||
|
||||
|
||||
$cron = new CronParser();
|
||||
|
||||
require_once(e_HANDLER."mail.php");
|
||||
foreach($list as $func=>$val)
|
||||
{
|
||||
$cron->calcLastRan($val['tab']);
|
||||
$due = $cron->getLastRanUnix();
|
||||
|
||||
if($_E107['debug'])
|
||||
{
|
||||
echo "<br />Cron: ".$val['function'];
|
||||
}
|
||||
|
||||
if($due > (time()-45))
|
||||
{
|
||||
if($_E107['debug']) { echo "<br />Running Now...<br />path: ".$val['path']; }
|
||||
|
||||
if(($val['path']=='_system') || is_readable(e_PLUGIN.$val['path']."/e_cron.php"))
|
||||
{
|
||||
if($val['path'] != '_system') // this is correct.
|
||||
{
|
||||
include_once(e_PLUGIN.$val['path']."/e_cron.php");
|
||||
}
|
||||
|
||||
$classname = $val['class']."_cron";
|
||||
if(class_exists($classname, false))
|
||||
{
|
||||
$obj = new $classname;
|
||||
if(method_exists($obj,$val['function']))
|
||||
{
|
||||
// $mes->add("Executing config function <b>".$key." : ".$method_name."()</b>", E_MESSAGE_DEBUG);
|
||||
if($_E107['debug']) { echo "<br />Method Found: ".$classname."::".$val['function']."()"; }
|
||||
|
||||
// Exception handling
|
||||
$methodname = $val['function'];
|
||||
$status = false;
|
||||
try
|
||||
{
|
||||
$status = $obj->$methodname();
|
||||
}
|
||||
catch (Exception $e)
|
||||
{
|
||||
$errorMData = $e->getFile().' '.$e->getLine();
|
||||
$errorMData .= "\n\n".$e->getCode().''.$e->getMessage();
|
||||
$errorMData .= "\n\n".implode("\n", $e->getTrace());
|
||||
//TODO log error in admin log. Pref for sending email to Administator
|
||||
sendemail($pref['siteadminemail'], $pref['siteadmin'].": Cron Schedule Exception", $errorMData, $pref['siteadmin'],$pref['siteadminemail'], $pref['siteadmin']);
|
||||
}
|
||||
// $status = call_user_func(array($obj,$val['function']));
|
||||
|
||||
// If task returns value which is not boolean (bc), it'll be used as a message (send email, logs)
|
||||
if($status && true !== $status)
|
||||
{
|
||||
//TODO log error in admin log. Pref for sending email to Administator
|
||||
// echo "\nerror running the function ".$func.".\n"; // log the error.
|
||||
if($_E107['debug']) { echo "<br />Method returned message: [{$classname}::".$val['function'].'] '.$status; }
|
||||
sendemail($pref['siteadminemail'], $pref['siteadmin'].": Cron Schedule Task Report", "Method returned message: [{$classname}::".$val['function'].'] '.$status, $pref['siteadmin'], $pref['siteadminemail'], $pref['siteadmin']);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if($_E107['debug']) { echo "<br />Couldn't find method: ".$val['function']; }
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if($_E107['debug']) { echo "<br />Couldn't find class: ".$classname; }
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
// echo "Cron Unix = ". $cron->getLastRanUnix();
|
||||
// echo "<br />Now = ".time();
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// echo "<br />Cron '$cron_str0' last due at: " . date('r', $cron->getLastRanUnix()) . "<p>";
|
||||
// $cron->getLastRan() returns last due time in an array
|
||||
// print_a($cron->getLastRan());
|
||||
// echo "Debug:<br />" . nl2br($cron->getDebug());
|
||||
/*
|
||||
$cron_str1 = "3 12 * * *";
|
||||
if ($cron->calcLastRan($cron_str1))
|
||||
{
|
||||
echo "<p>Cron '$cron_str1' last due at: " . date('r', $cron->getLastRanUnix()) . "<p>";
|
||||
print_r($cron->getLastRan());
|
||||
}
|
||||
else
|
||||
{
|
||||
echo "Error parsing";
|
||||
}
|
||||
echo "Debug:<br />" . nl2br($cron->getDebug());
|
||||
*/
|
||||
|
||||
exit;
|
||||
?>
|
||||
$cron = new cronScheduler();
|
||||
$cron->run();
|
||||
|
@ -920,4 +920,324 @@ class CronParser
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
||||
|
||||
|
||||
/**
|
||||
* Class cronScheduler.
|
||||
*
|
||||
* @see cron.php
|
||||
*
|
||||
* TODO:
|
||||
* - Log error in admin log.
|
||||
* - Pref for sending email to Administrator.
|
||||
* - LANs
|
||||
*/
|
||||
class cronScheduler
|
||||
{
|
||||
|
||||
/**
|
||||
* Cron parser class.
|
||||
*
|
||||
* @var \CronParser.
|
||||
*/
|
||||
private $cron;
|
||||
|
||||
/**
|
||||
* Debug mode.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $debug;
|
||||
|
||||
/**
|
||||
* System preferences.
|
||||
*
|
||||
* @var array|mixed
|
||||
*/
|
||||
private $pref;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
global $_E107, $pref;
|
||||
|
||||
$this->cron = new CronParser();
|
||||
$this->debug = $_E107['debug'];
|
||||
$this->pref = $pref;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs all cron jobs.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
$valid = $this->validateToken();
|
||||
|
||||
if(!$valid)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
@file_put_contents(e_CACHE . 'cronLastLoad.php', time());
|
||||
|
||||
// Get active cron jobs.
|
||||
$cron_jobs = $this->getCronJobs(true);
|
||||
|
||||
if($this->debug && $_SERVER['QUERY_STRING'])
|
||||
{
|
||||
echo "<h1>Cron Lists</h1>";
|
||||
print_a($cron_jobs);
|
||||
}
|
||||
|
||||
foreach($cron_jobs as $job)
|
||||
{
|
||||
$this->runJob($job);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a cron job.
|
||||
*
|
||||
* @param array $job
|
||||
* Contains the current cron job. Each element is an array with following
|
||||
* properties:
|
||||
* - 'path' string '_system' or plugin name.
|
||||
* - 'active' int 1 if active, 0 if inactive
|
||||
* - 'tab' string cron tab
|
||||
* - 'function' string function name
|
||||
* - 'class' string class name
|
||||
*
|
||||
* @return bool $status
|
||||
*/
|
||||
public function runJob($job)
|
||||
{
|
||||
$status = false;
|
||||
|
||||
if(empty($job['active']))
|
||||
{
|
||||
return $status;
|
||||
}
|
||||
|
||||
// Calculate the last due time before this moment.
|
||||
$this->cron->calcLastRan($job['tab']);
|
||||
$due = $this->cron->getLastRanUnix();
|
||||
|
||||
if($this->debug)
|
||||
{
|
||||
echo "<br />Cron: " . $job['function'];
|
||||
}
|
||||
|
||||
if($due <= (time() - 45))
|
||||
{
|
||||
return $status;
|
||||
}
|
||||
|
||||
if($job['path'] != '_system' && !is_readable(e_PLUGIN . $job['path'] . "/e_cron.php"))
|
||||
{
|
||||
return $status;
|
||||
}
|
||||
|
||||
if($this->debug)
|
||||
{
|
||||
echo "<br />Running Now...<br />path: " . $job['path'];
|
||||
}
|
||||
|
||||
// This is correct.
|
||||
if($job['path'] != '_system')
|
||||
{
|
||||
include_once(e_PLUGIN . $job['path'] . "/e_cron.php");
|
||||
}
|
||||
|
||||
$class = $job['class'] . "_cron";
|
||||
|
||||
if(!class_exists($class, false))
|
||||
{
|
||||
if($this->debug)
|
||||
{
|
||||
echo "<br />Couldn't find class: " . $class;
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
$obj = new $class;
|
||||
|
||||
if(!method_exists($obj, $job['function']))
|
||||
{
|
||||
if($this->debug)
|
||||
{
|
||||
echo "<br />Couldn't find method: " . $job['function'];
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
if($this->debug)
|
||||
{
|
||||
echo "<br />Method Found: " . $class . "::" . $job['function'] . "()";
|
||||
}
|
||||
|
||||
// Exception handling.
|
||||
$method = $job['function'];
|
||||
|
||||
try
|
||||
{
|
||||
$status = $obj->$method();
|
||||
} catch(Exception $e)
|
||||
{
|
||||
$msg = $e->getFile() . ' ' . $e->getLine();
|
||||
$msg .= "\n\n" . $e->getCode() . '' . $e->getMessage();
|
||||
$msg .= "\n\n" . implode("\n", $e->getTrace());
|
||||
|
||||
$mail = array(
|
||||
'to_mail' => $this->pref['siteadminemail'],
|
||||
'to_name' => $this->pref['siteadmin'],
|
||||
'from_mail' => $this->pref['siteadminemail'],
|
||||
'from_name' => $this->pref['siteadmin'],
|
||||
'message' => $msg,
|
||||
'subject' => 'e107 - Cron Schedule Exception',
|
||||
);
|
||||
|
||||
$this->sendMail($mail);
|
||||
}
|
||||
|
||||
// If task returns value which is not boolean (BC), it will be used as a
|
||||
// message (send email, logs).
|
||||
if($status && true !== $status)
|
||||
{
|
||||
if($this->debug)
|
||||
{
|
||||
echo "<br />Method returned message: [{$class}::" . $job['function'] . '] ' . $status;
|
||||
}
|
||||
|
||||
$msg = 'Method returned message: [{' . $class . '}::' . $job['function'] . '] ' . $status;
|
||||
|
||||
$mail = array(
|
||||
'to_mail' => $this->pref['siteadminemail'],
|
||||
'to_name' => $this->pref['siteadmin'],
|
||||
'from_mail' => $this->pref['siteadminemail'],
|
||||
'from_name' => $this->pref['siteadmin'],
|
||||
'message' => $msg,
|
||||
'subject' => 'e107 - Cron Schedule Task Report',
|
||||
);
|
||||
|
||||
$this->sendMail($mail);
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate Cron Token.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function validateToken()
|
||||
{
|
||||
$pwd = ($this->debug && $_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : trim($_SERVER['argv'][1]);
|
||||
|
||||
if(!empty($_GET['token']))
|
||||
{
|
||||
$pwd = e107::getParser()->filter($_GET['token']);
|
||||
}
|
||||
else
|
||||
{
|
||||
$pwd = str_replace('token=', '', $pwd);
|
||||
}
|
||||
|
||||
if(($this->pref['e_cron_pwd'] != $pwd) || empty($this->pref['e_cron_pwd']))
|
||||
{
|
||||
if(!empty($pwd))
|
||||
{
|
||||
$msg = "Your Cron Schedule is not configured correctly. Your passwords do not match.";
|
||||
$msg .= "<br /><br />";
|
||||
$msg .= "Sent from cron: " . $pwd;
|
||||
$msg .= "<br />";
|
||||
$msg .= "Stored in e107: " . $this->pref['e_cron_pwd'];
|
||||
$msg .= "<br /><br />";
|
||||
$msg .= "You should regenerate the cron command in admin and enter it again in your server configuration.";
|
||||
|
||||
$msg .= "<h2>" . "Debug Info" . "</h2>";
|
||||
$msg .= "<h3>_SERVER</h3>";
|
||||
$msg .= print_a($_SERVER, true);
|
||||
$msg .= "<h3>_ENV</h3>";
|
||||
$msg .= print_a($_ENV, true);
|
||||
$msg .= "<h3>_GET</h3>";
|
||||
$msg .= print_a($_GET, true);
|
||||
|
||||
$mail = array(
|
||||
'to_mail' => $this->pref['siteadminemail'],
|
||||
'to_name' => $this->pref['siteadmin'],
|
||||
'from_mail' => $this->pref['siteadminemail'],
|
||||
'from_name' => $this->pref['siteadmin'],
|
||||
'message' => $msg,
|
||||
'subject' => 'e107 - Cron Schedule Misconfigured',
|
||||
);
|
||||
|
||||
$this->sendMail($mail);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get available Cron jobs.
|
||||
*
|
||||
* @param bool $only_active
|
||||
* Set to TRUE for active cron jobs.
|
||||
*
|
||||
* @return array
|
||||
* Array contains cron jobs.
|
||||
*/
|
||||
public function getCronJobs($only_active = false)
|
||||
{
|
||||
$list = array();
|
||||
|
||||
$sql = e107::getDb();
|
||||
|
||||
$where = '1';
|
||||
|
||||
if($only_active === true)
|
||||
{
|
||||
$where = 'cron_active = 1';
|
||||
}
|
||||
|
||||
if($sql->select("cron", 'cron_function,cron_tab,cron_active', $where))
|
||||
{
|
||||
while($row = $sql->fetch())
|
||||
{
|
||||
list($class, $function) = explode("::", $row['cron_function'], 2);
|
||||
$key = $class . "__" . $function;
|
||||
|
||||
$list[$key] = array(
|
||||
'path' => $class,
|
||||
'active' => $row['cron_active'],
|
||||
'tab' => $row['cron_tab'],
|
||||
'function' => $function,
|
||||
'class' => $class,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to send email message.
|
||||
*
|
||||
* @param array $mail
|
||||
*/
|
||||
public function sendMail($mail)
|
||||
{
|
||||
require_once(e_HANDLER . "mail.php");
|
||||
sendemail($mail['to_mail'], $mail['subject'], $mail['message'], $mail['to_name'], $mail['from_mail'], $mail['from_name']);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user