issue #131: adding timeout setting to cronjob as well

This commit is contained in:
Pepijn Over 2014-11-06 10:52:30 +01:00
parent a646928a70
commit a2753c6348
5 changed files with 54 additions and 40 deletions

View File

@ -14,6 +14,7 @@ not yet released
* #144: Updated Brazilian translation.
* #146: Updated Russian translation.
* #147: Updated Spanish translation.
* #131: Allow URL and timeout to be passed as argument to the cronjob.
v3.1.0 (released August 7, 2014)

View File

@ -32,13 +32,35 @@ if(!psm_is_cli()) {
die('This script can only be run from the command line.');
}
psm_set_cli_uri();
$cron_timeout = PSM_CRON_TIMEOUT;
// parse a couple of arguments
if(!empty($_SERVER['argv'])) {
foreach ($_SERVER['argv'] as $argv) {
$argi = explode('=', ltrim($argv, '--'));
if(count($argi) !== 2) {
continue;
}
switch($argi[0]) {
case 'uri':
define('PSM_BASE_URL', $argi[1]);
break;
case 'timeout':
$cron_timeout = intval($argi[1]);
break;
}
}
}
// prevent cron from running twice at the same time
// however if the cron has been running for X mins, we'll assume it died and run anyway
// if you want to change PSM_CRON_TIMEOUT, have a look in src/includes/psmconfig.inc.php.
// or you can provide the --timeout=x argument
$time = time();
if(psm_get_conf('cron_running') == 1 && ($time - psm_get_conf('cron_running_time') < PSM_CRON_TIMEOUT)) {
if(
psm_get_conf('cron_running') == 1
&& $cron_timeout > 0
&& ($time - psm_get_conf('cron_running_time') < $cron_timeout)
) {
die('Cron is already running. Exiting.');
}
if(!defined('PSM_DEBUG') || !PSM_DEBUG) {

View File

@ -94,6 +94,14 @@ Please note that some distros have user-specific crontabs (e.g. Debian). If that
The update script has been designed to prevent itself from running multiple times. It has a maximum timeout of 10 minutes.
After that the script is assumed dead and the cronjob will run again.
If you want to change the 10 minutes timeout, find the constant "PSM_CRON_TIMEOUT" in src/includes/psmconfig.inc.php.
You can also provide it as an argument (in seconds!). The following example would change to timeout to 10 seconds::
php status.cron.php --timeout=10
By default, no URLs are generated for notifications created in the cronjob.
To specify the base url to your monitor installation, use the "--uri" argument, like so::
php status.cron.php --uri="http://www.phpservermonitor.org/mymonitor/"
Troubleshooting

View File

@ -484,12 +484,15 @@ function psm_build_sms() {
* @return string
*/
function psm_build_url($params = array(), $urlencode = true, $htmlentities = true) {
$url = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
// on Windows, dirname() adds both back- and forward slashes (http://php.net/dirname).
// for urls, we only want the forward slashes.
$url .= dirname($_SERVER['SCRIPT_NAME']) . '/';
$url = str_replace('\\', '', $url);
if(defined('PSM_BASE_URL') && PSM_BASE_URL !== null) {
$url = PSM_BASE_URL;
} else {
$url = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'];
// on Windows, dirname() adds both back- and forward slashes (http://php.net/dirname).
// for urls, we only want the forward slashes.
$url .= dirname($_SERVER['SCRIPT_NAME']) . '/';
$url = str_replace('\\', '', $url);
}
if($params != null) {
$url .= '?';
@ -548,32 +551,6 @@ function psm_is_cli() {
return (!isset($_SERVER['SERVER_SOFTWARE']) || php_sapi_name() == 'cli');
}
/**
* Set _server vars from the cli argument --uri=
* Example: php cron/status.cron.php --uri="http://www.phpservermonitor.org/"
*/
function psm_set_cli_uri() {
foreach ($_SERVER['argv'] as $argv) {
if (0 === strpos($argv, '--uri=')) {
$uriArray = parse_url(substr($argv, 6));
if (!empty($uriArray['scheme'])) {
$_SERVER['REQUEST_SCHEME'] = $uriArray['scheme'];
$_SERVER['SERVER_PORT'] = ($uriArray['scheme'] == 'https') ? 443 : 80;
}
if (!empty($uriArray['host'])) {
$_SERVER['HTTP_HOST'] = $uriArray['host'];
}
if (!empty($uriArray['port'])) {
$_SERVER['SERVER_PORT'] = $uriArray['port'];
}
if (!empty($uriArray['path'])) {
$_SERVER['SCRIPT_NAME'] = $uriArray['path'];
}
break;
}
}
}
###############################################
#
# Debug functions

View File

@ -66,7 +66,7 @@ define('PSM_UPDATE_INTERVAL', 7 * 24 * 60 * 60);
*
* This constant will be used in the login and the registration class.
*/
define("PSM_LOGIN_HASH_COST_FACTOR", "10");
define('PSM_LOGIN_HASH_COST_FACTOR', '10');
/**
* Configuration for: Cookies
@ -83,9 +83,9 @@ define("PSM_LOGIN_HASH_COST_FACTOR", "10");
* COOKIE_DOMAIN: The domain where the cookie is valid for, like '.mydomain.com'
* COOKIE_SECRET_KEY: Put a random value here to make your app more secure. When changed, all cookies are reset.
*/
define("PSM_LOGIN_COOKIE_RUNTIME", 1209600);
define("PSM_LOGIN_COOKIE_DOMAIN", null);
define("PSM_LOGIN_COOKIE_SECRET_KEY", "4w900de52e3ap7y77y8675jy6c594286");
define('PSM_LOGIN_COOKIE_RUNTIME', 1209600);
define('PSM_LOGIN_COOKIE_DOMAIN', null);
define('PSM_LOGIN_COOKIE_SECRET_KEY', '4w900de52e3ap7y77y8675jy6c594286');
/**
* Number of seconds the reset link is valid after sending it to the user.
@ -93,7 +93,7 @@ define("PSM_LOGIN_COOKIE_SECRET_KEY", "4w900de52e3ap7y77y8675jy6c594286");
define('PSM_LOGIN_RESET_RUNTIME', 3600);
/**
* Number of seconds the cron is supposedly dead and we will run another cron anyway.
* Number of seconds the cron is supposedly dead and we will run another cron anyway. Set to 0 to disable.
*/
define('PSM_CRON_TIMEOUT', 600);
@ -111,4 +111,10 @@ define('PSM_THEME', 'default');
/**
* Clone URL for the Pushover.net service.
*/
define('PSM_PUSHOVER_CLONE_URL', 'https://pushover.net/apps/clone/php_server_monitor');
define('PSM_PUSHOVER_CLONE_URL', 'https://pushover.net/apps/clone/php_server_monitor');
/**
* By defining the PSM_BASE_URL, you will force the psm_build_url() to use this.
* Useful for cronjobs if it cannot be auto-detected.
*/
//define('PSM_BASE_URL', null);