Skip to content

Commit

Permalink
Background calls
Browse files Browse the repository at this point in the history
* New functions: runScriptSafe, getURLBackground
* callMethodSafe updated to be running in background
  • Loading branch information
sergejey committed Jun 22, 2017
1 parent 9fdab98 commit 7c07681
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
7 changes: 7 additions & 0 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@


if ($_GET['part_load']) {

$res=array();
$res['TITLE']='';
$res['CONTENT']='';
$res['NEED_RELOAD']=1;

$cut_begin='<div id="partLoadContent">';
$cut_begin_index=mb_strpos($result, $cut_begin);
Expand All @@ -99,6 +103,9 @@
$res['CONTENT']='';
$res['NEED_RELOAD']=1;
}
if (preg_match('/<title>(.+?)<\/title>/is',$result,$m)) {
$res['TITLE']=$m[1];
}
} else {
$res['CONTENT']='';
$res['NEED_RELOAD']=1;
Expand Down
52 changes: 39 additions & 13 deletions lib/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,32 @@ function runScript($id, $params = '')
return $sc->runScript($id, $params);
}

function runScriptSafe($id, $params = '') {
$current_call='script.'.$id;
$call_stack=array();
if (isset($_GET['m_c_s']) && is_array($_GET['m_c_s'])) {
$call_stack = $_GET['m_c_s'];
}
if (in_array($current_call,$call_stack)) {
$call_stack[]=$current_call;
DebMes("Warning: cross-linked call of ".$current_call."\nlog:\n".implode(" -> \n",$call_stack));
return 0;
}
$call_stack[]=$current_call;
$data=array(
'script'=>$id,
'm_c_s'=>$call_stack
);
$url=BASE_URL.'/objects/?'.http_build_query($data);
if (is_array($params)) {
foreach($params as $k=>$v) {
$url.='&'.$k.'='.urlencode($v);
}
}
$result = getURLBackground($url,0);
return $result;
}

/**
* Summary of callScript
* @param mixed $id ID
Expand All @@ -707,6 +733,10 @@ function callScript($id, $params = '')
runScript($id, $params);
}

function getURLBackground($url, $cache = 0, $username = '', $password = '') {
getURL($url, $cache, $username, $password, true);
}

/**
* Summary of getURL
* @param mixed $url Url
Expand All @@ -715,7 +745,7 @@ function callScript($id, $params = '')
* @param mixed $password Password (default '')
* @return mixed
*/
function getURL($url, $cache = 0, $username = '', $password = '')
function getURL($url, $cache = 0, $username = '', $password = '', $background = false)
{
$cache_file = ROOT . 'cached/urls/' . preg_replace('/\W/is', '_', str_replace('http://', '', $url)) . '.html';

Expand All @@ -738,6 +768,11 @@ function getURL($url, $cache = 0, $username = '', $password = '')
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // bad style, I know...
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);

if ($background) {
//curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);

This comment has been minimized.

Copy link
@olehs

olehs Sep 11, 2017

Contributor

1 миллисекунда???

}

if ($username != '' || $password != '')
{
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
Expand Down Expand Up @@ -780,22 +815,13 @@ function getURL($url, $cache = 0, $username = '', $password = '')

$result = curl_exec($ch);

$endTime=getmicrotime();
//DebMes('Geturl finished for '.$url.' (Time taken: '.round($endTime-$startTime,2).')', 'geturl');

if (Defined('GETURL_WARNING_TIMEOUT') && ((int)GETURL_WARNING_TIMEOUT>0)) {
$warning_timeout=GETURL_WARNING_TIMEOUT;
} else {
$warning_timeout = 5;
}

if (curl_errno($ch)) {
$errorInfo = curl_error($ch);
$info = curl_getinfo($ch);
$callSource=debug_backtrace()[1]['function'];
DebMes("Geturl to $url (source ".$callSource.") finished with error: \n".$errorInfo."\n".json_encode($info));
} elseif (($endTime-$startTime)>$warning_timeout) {
DebMes("Warning: geturl to $url is pretty slow (".round($endTime-$startTime,2)."s)");
$backtrace = debug_backtrace();
$callSource = $backtrace[1]['function'];
DebMes("GeturlBackground to $url (source ".$callSource.") finished with error: \n".$errorInfo."\n".json_encode($info));
}
curl_close($ch);

Expand Down
3 changes: 1 addition & 2 deletions modules/objects/objects.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,7 @@ function callMethodSafe($name,$params = 0) {
$url.='&'.$k.'='.urlencode($v);
}
}

$result = getURL($url,0);
$result = getURLBackground($url,0);
return $result;
}

Expand Down

0 comments on commit 7c07681

Please sign in to comment.