diff --git a/.idea/php.xml b/.idea/php.xml index 335d7b7..dc0a9b3 100644 --- a/.idea/php.xml +++ b/.idea/php.xml @@ -34,7 +34,7 @@ - + \ No newline at end of file diff --git a/admin/ajax/threads.php b/admin/ajax/threads.php new file mode 100644 index 0000000..9b6604b --- /dev/null +++ b/admin/ajax/threads.php @@ -0,0 +1,175 @@ + + + + + + + + + + + + + + + + + + + + + + + getTasks(10, 'priority', 'asc'); + foreach ($arTasks as $task) { + ?> + + + + + + + + + + + + + + + + + + + +
IDАктивенВыполняетсяПриоритетВремя выполненияПопыткиЛимит попытокКлассМетодПараметрыСтатусРезультатСозданОбновлен
RUN' : ''; ?>
+ + + + + + + + + + + + + + + + + + + getTasksHistory(10, 'id', 'desc'); + foreach($arTasks as $task) { + ?> + + + + + + + + + + + + + + + +
IDID заданияВремя выполненияПопытокКлассМетодПараметрыСтатусРезультатСозданОбновлен
+ + + + + + + + + + + + + + + + + + + + + +
ВсегоВоркеровАктивныхВыполняютсяС ошибкой
getCountTasks();?>getCountWorkers() . ' / ' . MQ::WORKERS_LIMIT;?>getCountTasks(['active' => 'Y']);?>getCountTasks(['active' => 'Y', 'in_progress' => 'Y']);?>getCountTasks(['in_progress' => 'N', 'status' => MQ::STATUS_ERROR]);?>
+ + + + + + + + + + + + + + + + + +
ВсегоУспешныхНеудачных
getCountTasksHistory();?>getCountTasksHistory(['status' => MQ::STATUS_OK]);?>getCountTasksHistory(['status' => MQ::STATUS_ERROR]);?>
+ +
+
+ 40% Complete (success) +
+
+Delta - CPanel + + diff --git a/admin/threads.php b/admin/threads.php index eabd860..7e95bdd 100644 --- a/admin/threads.php +++ b/admin/threads.php @@ -29,29 +29,8 @@

Задания в очереди

Задания, ожидающие выполнения и задания, выполнение которых завершилось ошибкой

-
- - - - - - - - - - - - - - - - - - - - -
ВсегоВоркеровАктивныхВыполняютсяС ошибкой
getCountTasks();?>getCountWorkers() . ' / ' . MQ::WORKERS_LIMIT;?>getCountTasks(['active' => 'Y']);?>getCountTasks(['active' => 'Y', 'in_progress' => 'Y']);?>getCountTasks(['in_progress' => 'N', 'status' => MQ::STATUS_ERROR]);?>
- +
+
@@ -64,26 +43,8 @@

Завершенные задания

Задания, которые были успешно выполнены

-
- - - - - - - - - - - - - - - - - -
ВсегоУспешныхНеудачных
getCountTasksHistory();?>getCountTasksHistory(['status' => MQ::STATUS_OK]);?>getCountTasksHistory(['status' => MQ::STATUS_ERROR]);?>
- +
+
@@ -93,7 +54,19 @@ - +
+
+
+
+

Текущая нагрузка на диспетчер очереди

+

Чем больше количество активных воркеров - тем больше нагрузка

+
+
+
+
+
+
+
@@ -105,66 +78,8 @@

Список текущих заданий очереди

-
- - - - - - - - - - - - - - - - - - - - - - getTasks(10, 'priority', 'asc'); - foreach($arTasks as $task) { - ?> - - - - - - - - - - - - - - - - - - - -
IDАктивенВыполняетсяПриоритетВремя выполненияПопыткиЛимит попытокКлассМетодПараметрыСтатусРезультатСозданОбновлен
RUN' : '';?>
+
+
@@ -183,52 +98,8 @@

Список заверщившихся заданий

-
- - - - - - - - - - - - - - - - - - getTasksHistory(10, 'id', 'desc'); - foreach($arTasks as $task) { - ?> - - - - - - - - - - - - - - - -
IDID заданияВремя выполненияПопытокКлассМетодПараметрыСтатусРезультатСозданОбновлен
+
+
@@ -236,4 +107,33 @@ + \ No newline at end of file diff --git a/composer.json b/composer.json index bfad3b6..a802a65 100644 --- a/composer.json +++ b/composer.json @@ -17,6 +17,9 @@ "require": { "twig/twig": "^3.0", "sentry/sdk": "^3.2", - "ext-iconv": "*" + "ext-iconv": "*", + "ext-curl": "*", + "ext-json": "*", + "ext-openssl": "*" } } diff --git a/core/cron.php b/core/cron.php index ba0adb6..2dc8eb8 100644 --- a/core/cron.php +++ b/core/cron.php @@ -20,8 +20,12 @@ if((time() - (int)$cronMyslo) > 600) { $runMyslo = true; Cache::set($cacheId, time()); + sendTelegram('cron_myslo if if set (' . $cronMyslo . ')'); } - + } else { + $runMyslo = true; + sendTelegram('cron_myslo global else (' . $cronMyslo . ')'); + Cache::set($cacheId, time()); } if($runMyslo === true) { diff --git a/core/lib/Core/ExternalServices/Request.class.php b/core/lib/Core/ExternalServices/Request.class.php index 46e08a7..32d847c 100644 --- a/core/lib/Core/ExternalServices/Request.class.php +++ b/core/lib/Core/ExternalServices/Request.class.php @@ -2,6 +2,161 @@ namespace Core\ExternalServices; + use Core\CoreException; + class Request { + private $httpStatus = null; + + private $httpCode = null; + + private $maxRedirects = 2; + + private $requestHeaders = []; + + /** + * @var false|string + */ + private $responseBody = null; + + private $responseHeader = null; + + private $timeout = 5; + + /** + * @return string|null + */ + public function getResponseBody(): ?string + { + return $this->responseBody; + } + + /** + * @return null + */ + public function getResponseHeader() + { + return $this->responseHeader; + } + + private $usertAgent = null; + + private $requestType = null; + + private $url = null; + + private $params = []; + + private $referer = ''; + + private $usePostMethod = false; + + public function __construct() + { + } + + public function setUrl(?string $url): self + { + $this->url = $url; + return $this; + } + + public function post(): self + { + $this->usePostMethod = true; + return $this; + } + + public function get(): self + { + $this->usePostMethod = true; + return $this; + } + + public function setParams(?array $params = null): self + { + $this->params = $params ?: []; + return $this; + } + + public function setHeaders(?array $arHeaders = null): self + { + $this->requestHeaders = $arHeaders ?: []; + return $this; + } + + public function setTimeout(int $timeout): self + { + $this->timeout = $timeout; + return $this; + } + + public function setMaxRedirects(int $redirects): self + { + $this->maxRedirects = $redirects; + return $this; + } + + + public function setReferer(?string $referer = null): self + { + $this->referer = $referer ?: ''; + return $this; + } + + public function setUserAgent(?string $userAgent = null): self + { + $this->userAgent = $userAgent ?: ''; + return $this; + } + + public function getCode(): ?int + { + return $this->httpCode; + } + + public function send() + { + $res = $this->sendRequest(); + return $res; + } + + /** + * @throws CoreException + */ + private function sendRequest(): ?string + { + if (empty($this->url)) { + throw new CoreException('Адрес запроса не задан'); + } + + if ($this->usePostMethod === true) { + $ch = curl_init($this->url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params); + } else { + $ch = curl_init($this->url . '?' . http_build_query($this->params)); + } + + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HEADER, false); + curl_setopt($ch, CURLOPT_REFERER, $this->referer); + curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); + curl_setopt($ch, CURLOPT_MAXREDIRS, $this->maxRedirects); + curl_setopt($ch, CURLOPT_USERAGENT, $this->usertAgent); + curl_setopt($ch, CURLOPT_HTTPHEADER, $this->requestHeaders); + curl_setopt($ch, CURLOPT_HEADER, 1); + + $res = curl_exec($ch); + + $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE); + + $this->responseHeader = substr($res, 0, $headerSize); + $this->responseBody = substr($res, $headerSize); + + $this->httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + curl_close($ch); + return $res; + } } \ No newline at end of file diff --git a/core/lib/Core/Helpers/Crypto.class.php b/core/lib/Core/Helpers/Crypto.class.php new file mode 100644 index 0000000..af05389 --- /dev/null +++ b/core/lib/Core/Helpers/Crypto.class.php @@ -0,0 +1,99 @@ +key); + unset($this->method); + } + + /** + * Генерация ключа шифрования + * + * @return string Ключ + */ + public static function generateKey(): string + { + return bin2hex(openssl_random_pseudo_bytes(40)); + } + + /** + * @param string $key + * + * @return Crypto + */ + public function setKey(string $key): self + { + $this->key = $key; + return $this; + } + + /** + * Шифрование данных + * + * @param string $string Строка + * + * @return string + */ + public function encode(string $string): string + { + $ivlen = openssl_cipher_iv_length($this->method); + $iv = openssl_random_pseudo_bytes($ivlen); + $ciphertext_raw = openssl_encrypt($string, $this->method, $this->key, OPENSSL_RAW_DATA, $iv); + $hmac = hash_hmac($this->algorithm, $ciphertext_raw, $this->key, true); + $ciphertext = base64_encode($iv . $hmac . $ciphertext_raw); + return $ciphertext; + } + + /** + * Расшифровка данных + * + * @param string $string Зашифрованная строка + * + * @return string|null + */ + public function decode(string $string): ?string + { + $c = base64_decode($string); + $ivlen = openssl_cipher_iv_length($this->method); + $iv = substr($c, 0, $ivlen); + $hmac = substr($c, $ivlen, $sha2len = 32); + $ciphertext_raw = substr($c, $ivlen + $sha2len); + $plaintext = openssl_decrypt($ciphertext_raw, $this->method, $this->key, OPENSSL_RAW_DATA, $iv); + $calcmac = hash_hmac($this->algorithm, $ciphertext_raw, $this->key, true); + if (hash_equals($hmac, $calcmac)) { + return $plaintext; + } + return null; + } + + } \ No newline at end of file diff --git a/core/lib/Core/MQTasks.class.php b/core/lib/Core/MQTasks.class.php index 00b275f..01c52e3 100644 --- a/core/lib/Core/MQTasks.class.php +++ b/core/lib/Core/MQTasks.class.php @@ -259,6 +259,8 @@ DELETE from `jokes` WHERE `jokes`.id not in (SELECT id FROM t_temp);' if($oldLink !== $link) { + sendTelegram('OLD (' .strlen($oldLink).'): ' . $oldLink . PHP_EOL . 'NEW (' .strlen($link).'): ' . $link); + sendTelegram('Обнаружен новый пост'); $full = file_get_contents($link); preg_match_all('/

(.*?)<\/p><\/div>/us', $full, $lidArticle); @@ -286,10 +288,10 @@ DELETE from `jokes` WHERE `jokes`.id not in (SELECT id FROM t_temp);' $post .= PHP_EOL . 'Подробнее'; $telegram = new \Core\ExternalServices\TelegramSender(TELEGRAM_BOT_TOKEN); - $telegram->setChat(TELEGRAM_NOTIFICATION_CHANNEL); + $telegram->setChat('-1001789206618'); $res = $telegram->sendPhoto($tmpFile, $post); @unlink($tmpFile); - Cache::set($cacheId, $link); + Cache::set($cacheId, trim($link)); return $res; } else { return 'Обновление не требуется'; diff --git a/core/lib/Core/Models/MQ.class.php b/core/lib/Core/Models/MQ.class.php index e0e805b..20117c4 100644 --- a/core/lib/Core/Models/MQ.class.php +++ b/core/lib/Core/Models/MQ.class.php @@ -160,7 +160,7 @@ */ public static function test2(...$params): string { - sleep(rand(10, 60)); + sleep(rand(1, 5)); $result = rand(0, 100); if (($result % 2) == 0) { throw new CoreException('Сервер обмена временно недоступен'); diff --git a/test.php b/test.php index 61d820a..4182325 100644 --- a/test.php +++ b/test.php @@ -2,5 +2,7 @@ $_SERVER['DOCUMENT_ROOT'] = '/home/rsgrinko/sites/dev.it-stories.ru'; require_once $_SERVER['DOCUMENT_ROOT'] . '/core/bootstrap.php'; - use Core\MQTasks; - echo MQTasks::sendJokeToTelegram(); + print_r($_REQUEST); + print_r($_SERVER); + /*use Core\MQTasks; + echo MQTasks::sendJokeToTelegram();*/ diff --git a/uploads/preloader.gif b/uploads/preloader.gif new file mode 100644 index 0000000..2b86fe8 Binary files /dev/null and b/uploads/preloader.gif differ