return success or error if called via HTTP as HTTP status

This commit is contained in:
Ralf Becker 2012-05-08 21:47:49 +00:00
parent d236f4531e
commit 30e86a8195
2 changed files with 77 additions and 58 deletions

View File

@ -1,4 +1,3 @@
#!/usr/bin/php -q
<?php <?php
/** /**
* API - Timed Asynchron Services for eGroupWare * API - Timed Asynchron Services for eGroupWare
@ -13,12 +12,14 @@
* @version $Id$ * @version $Id$
*/ */
$_REQUEST['domain'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 'default'; if (!isset($_REQUEST['domain'])) $_REQUEST['domain'] = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1] : 'default';
$path_to_egroupware = realpath(dirname(__FILE__).'/../..'); // need to be adapted if this script is moved somewhere else $path_to_egroupware = realpath(dirname(__FILE__).'/../..'); // need to be adapted if this script is moved somewhere else
// remove the comment from one of the following lines to enable loging // remove the comment from one of the following lines to enable loging
// define('ASYNC_LOG','C:\\async.log'); // Windows // define('ASYNC_LOG','C:\\async.log'); // Windows
// define('ASYNC_LOG','/tmp/async.log'); // Linux, Unix, ... // define('ASYNC_LOG','/tmp/async.log'); // Linux, Unix, ...
// ini_set('error_log','/var/lib/egroupware/cron.log'); // log regular errors and error_log only
if (defined('ASYNC_LOG')) if (defined('ASYNC_LOG'))
{ {
$msg = date('Y/m/d H:i:s ').$_REQUEST['domain'].": asyncservice started\n"; $msg = date('Y/m/d H:i:s ').$_REQUEST['domain'].": asyncservice started\n";
@ -26,21 +27,25 @@
fwrite($f,$msg); fwrite($f,$msg);
fclose($f); fclose($f);
} }
$GLOBALS['egw_info']['flags'] = array( $GLOBALS['egw_info']['flags'] = array(
'currentapp' => 'login', 'currentapp' => 'login',
'noapi' => True // this stops header.inc.php to include phpgwapi/inc/function.inc.php 'noapi' => True // this stops header.inc.php to include phpgwapi/inc/function.inc.php
); );
if (!is_readable($path_to_egroupware.'/header.inc.php')) if (!is_readable($path_to_egroupware.'/header.inc.php'))
{ {
echo $msg = "asyncservice.php: Could not find '$path_to_egroupware/header.inc.php', exiting !!!\n"; $msg = "asyncservice.php: Could not find '$path_to_egroupware/header.inc.php', exiting !!!\n";
if (isset($_SERVER['HTTP_HOST']))
{
header("HTTP/1.1 500 $msg");
}
if (defined('ASYNC_LOG')) if (defined('ASYNC_LOG'))
{ {
$f = fopen(ASYNC_LOG,'a+'); $f = fopen(ASYNC_LOG,'a+');
fwrite($f,$msg); fwrite($f,$msg);
fclose($f); fclose($f);
} }
exit(1); die($msg);
} }
include($path_to_egroupware.'/header.inc.php'); include($path_to_egroupware.'/header.inc.php');
unset($GLOBALS['egw_info']['flags']['noapi']); unset($GLOBALS['egw_info']['flags']['noapi']);
@ -48,22 +53,32 @@
$db_type = $GLOBALS['egw_domain'][$_REQUEST['domain']]['db_type']; $db_type = $GLOBALS['egw_domain'][$_REQUEST['domain']]['db_type'];
if (!isset($GLOBALS['egw_domain'][$_REQUEST['domain']]) || empty($db_type)) if (!isset($GLOBALS['egw_domain'][$_REQUEST['domain']]) || empty($db_type))
{ {
echo $msg = "asyncservice.php: Domain '$_REQUEST[domain]' is not configured or renamed, exiting !!!\n"; $msg = "asyncservice.php: Domain '$_REQUEST[domain]' is not configured or renamed, exiting !!!\n";
if (isset($_SERVER['HTTP_HOST']))
{
header("HTTP/1.1 500 $msg");
}
if (defined('ASYNC_LOG')) if (defined('ASYNC_LOG'))
{ {
$f = fopen(ASYNC_LOG,'a+'); $f = fopen(ASYNC_LOG,'a+');
fwrite($f,$msg); fwrite($f,$msg);
fclose($f); fclose($f);
} }
exit(1); die($msg);
} }
include(EGW_API_INC.'/functions.inc.php'); include(EGW_API_INC.'/functions.inc.php');
$num = ExecMethod('phpgwapi.asyncservice.check_run','crontab'); $num = ExecMethod('phpgwapi.asyncservice.check_run',isset($_REQUEST['run_by']) ? $_REQUEST['run_by'] : 'crontab');
$msg = date('Y/m/d H:i:s ').$_REQUEST['domain'].': '.($num === false ? 'An error occured: can not obtain semaphore!' : $msg = date('Y/m/d H:i:s ').$_REQUEST['domain'].': '.($num === false ? 'An error occured: can not obtain semaphore!' :
($num ? "$num job(s) executed" : 'Nothing to execute'))."\n\n"; ($num ? "$num job(s) executed" : 'Nothing to execute'))."\n\n";
if (isset($_SERVER['HTTP_HOST']))
{
header($num === false ? "HTTP/1.1 500 Can NOT obtain semaphore" : "HTTP/1.1 200 ".($num ? "$num job(s) executed" : 'Nothing to execute'));
}
// if the following comment got removed, you will get an email from cron for every check performed (*nix only) // if the following comment got removed, you will get an email from cron for every check performed (*nix only)
//echo $msg; //echo $msg;
@ -73,4 +88,4 @@
fwrite($f,$msg); fwrite($f,$msg);
fclose($f); fclose($f);
} }
$GLOBALS['egw']->common->egw_exit(); common::egw_exit();

View File

@ -387,7 +387,7 @@ class asyncservice
*/ */
function check_run($run_by='') function check_run($run_by='')
{ {
flush(); if ($run_by === 'fallback') flush();
if (!$this->last_check_run(True,False,$run_by)) if (!$this->last_check_run(True,False,$run_by))
{ {
@ -473,7 +473,11 @@ class asyncservice
*/ */
function read($id=0,$cols='*',$offset=False,$append='ORDER BY async_next',$num_rows=0) function read($id=0,$cols='*',$offset=False,$append='ORDER BY async_next',$num_rows=0)
{ {
if (!is_array($id) && (strpos($id,'%') !== False || strpos($id,'_') !== False)) if ($id === '%')
{
$where = "async_id != '##last-check-run##'";
}
elseif (!is_array($id) && (strpos($id,'%') !== False || strpos($id,'_') !== False))
{ {
$id = $this->db->quote($id); $id = $this->db->quote($id);
$where = "async_id LIKE $id AND async_id != '##last-check-run##'"; $where = "async_id LIKE $id AND async_id != '##last-check-run##'";