Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
try curl requests several times
Browse files Browse the repository at this point in the history
  • Loading branch information
arrilot committed Mar 17, 2019
1 parent 859a2f3 commit ef4f37e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 33 deletions.
57 changes: 57 additions & 0 deletions src/Checks/Check.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Arrilot\BitrixSystemCheck\Monitorings\DataStorage;
use Arrilot\BitrixSystemCheck\Exceptions\SkipCheckException;
use RuntimeException;

abstract class Check
{
Expand Down Expand Up @@ -147,4 +148,60 @@ protected function inConsole()
{
return php_sapi_name() === 'cli';
}

/**
* Attempt to run $callable $times times with $interval seconds interval until first successful attempt.
*
* @param callable $callable
* @param int $times
* @param int $interval - in seconds
* @return mixed
*/
protected function attempt(callable $callable, $times, $interval = 1)
{
$lastError = '';
foreach (range(1, $times) as $i) {
try {
return $callable($i, $times);
} catch (RuntimeException $e) {
$lastError = $e->getMessage();
sleep($interval);
}
}

$this->logError($lastError);

return null;
}

/**
* @param string $url
* @param null|string $basicAuth
* @return null|array
*/
protected function getCurlInfo($url, $basicAuth = null)
{
return $this->attempt(function () use ($url, $basicAuth) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

if ($basicAuth) {
curl_setopt($ch, CURLOPT_USERPWD, $basicAuth);
}

$result = curl_exec($ch);
if (!$result) {
$this->logError('При curl запросе к '. $url . ' произошла ошибка ' . curl_error($ch));
curl_close($ch);
return false;
}
$info = curl_getinfo($ch);
curl_close($ch);
return $info;
}, 3);
}
}
21 changes: 5 additions & 16 deletions src/Checks/Custom/HttpsRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Arrilot\BitrixSystemCheck\Checks\Custom;

use Arrilot\BitrixSystemCheck\Checks\Check;
use RuntimeException;

class HttpsRedirect extends Check
{
Expand Down Expand Up @@ -35,27 +36,15 @@ public function name()
*/
public function run()
{
$ch = curl_init();
$url = str_replace('https://', 'http://', $this->mainPage);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

if ($this->basicAuth) {
curl_setopt($ch, CURLOPT_USERPWD, $this->basicAuth);
$info = $this->getCurlInfo($url, $this->basicAuth);
if (is_null($info)) {
return false;
}

$result = curl_exec($ch);
if (!$result) {
$this->logError('При curl запросе к '. $url . ' произошла ошибка ' . curl_error($ch));
curl_close($ch);
if (is_null($info)) {
return false;
}
$info = curl_getinfo($ch);
curl_close($ch);

if ($info['http_code'] !== 301 && $info['http_code'] !== 302) {
$this->logError('При curl запросе к '. $url . ' получен код ответа ' . $info['http_code'] . ' вместо 301/302');
Expand Down
20 changes: 3 additions & 17 deletions src/Checks/Custom/WwwRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Arrilot\BitrixSystemCheck\Checks\Custom;

use Arrilot\BitrixSystemCheck\Checks\Check;
use RuntimeException;

class WwwRedirect extends Check
{
Expand Down Expand Up @@ -43,29 +44,14 @@ public function name()
*/
public function run()
{
$ch = curl_init();
$url = $this->reverse
? str_replace(['http://www.', 'https://www.'], ['http://', 'https://'], $this->mainPage)
: str_replace(['http://', 'https://'], ['http://www.', 'https://www.'], $this->mainPage);

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10000);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

if ($this->basicAuth) {
curl_setopt($ch, CURLOPT_USERPWD, $this->basicAuth);
}

$result = curl_exec($ch);
if (!$result) {
$this->logError('При curl запросе к '. $url . ' произошла ошибка ' . curl_error($ch));
curl_close($ch);
$info = $this->getCurlInfo($url, $this->basicAuth);
if (is_null($info)) {
return false;
}
$info = curl_getinfo($ch);
curl_close($ch);

if ($info['http_code'] !== 301 && $info['http_code'] !== 302) {
$this->logError('При curl запросе к '. $url . ' получен код ответа ' . $info['http_code'] . ' вместо 301/302');
Expand Down

0 comments on commit ef4f37e

Please sign in to comment.