From 881fb9cbfb660ed799ec9ca892e97ca98df602c8 Mon Sep 17 00:00:00 2001 From: Juan Lago Date: Thu, 21 Mar 2024 11:04:52 +0100 Subject: [PATCH] Provided support for PHP 8.2+ --- composer.json | 18 ++++--- src/App/Controller/Main.php | 23 ++++----- src/App/Model/Clients/Datadog.php | 5 +- src/App/Model/Senders/Datadog.php | 67 +++++++++++++++---------- src/App/Model/Senders/ElasticSearch.php | 7 ++- src/Config/Version.php | 6 +-- 6 files changed, 69 insertions(+), 57 deletions(-) diff --git a/composer.json b/composer.json index bcb55c6..ae34209 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,10 @@ } ], "config": { - "vendor-dir": "src/Vendor" + "vendor-dir": "src/Vendor", + "allow-plugins": { + "php-http/discovery": true + } }, "scripts": { "post-create-project-cmd": [ @@ -19,12 +22,13 @@ ] }, "require": { - "league/climate": "3.2.1", - "elasticsearch/elasticsearch": "v6.0.1", - "nesbot/carbon": "1.22.1", - "amphp/amp": "^2.1", - "amphp/artax": "^3.0", + "league/climate": "^3.8.1", + "elasticsearch/elasticsearch": "^v7.17", + "nesbot/carbon": "^3.1", + "amphp/amp": "^v3.0.0", + "amphp/http-client": "^5.0", "ext-pcntl": "*", - "ext-json": "*" + "ext-json": "*", + "revolt/event-loop": "^1.0" } } diff --git a/src/App/Controller/Main.php b/src/App/Controller/Main.php index ae58dcf..d76779a 100644 --- a/src/App/Controller/Main.php +++ b/src/App/Controller/Main.php @@ -1,6 +1,6 @@ readLog(); }); + EventLoop::onSignal(SIGINT, fn() => $this->actionTerminateBySignal()); + EventLoop::onSignal(SIGHUP, fn() => $this->actionTerminateBySignal()); - // Finish app - Apprunner::terminate(Apprunner::EXIT_SUCCESS); + EventLoop::run(); + // Finish app + Apprunner::terminate(); } @@ -253,9 +251,8 @@ public function actionTerminateBySignal() { if (!ignore_user_abort()) { - echo "Exiting..."; - - Loop::stop(); + echo "Exiting...\n"; + Apprunner::terminate(Apprunner::EXIT_HUP); } } diff --git a/src/App/Model/Clients/Datadog.php b/src/App/Model/Clients/Datadog.php index c04aa49..d00b1ab 100644 --- a/src/App/Model/Clients/Datadog.php +++ b/src/App/Model/Clients/Datadog.php @@ -1,6 +1,6 @@ settings['batch_size'] = $this->settings['batch_size'] > static::MAX_BATCH_SIZE ? static::MAX_BATCH_SIZE : $this->settings['batch_size']; - - $this->client = new DefaultClient(); + $this->client = HttpClientBuilder::buildDefault(); } } diff --git a/src/App/Model/Senders/Datadog.php b/src/App/Model/Senders/Datadog.php index 1b20b5f..3fd5b5b 100755 --- a/src/App/Model/Senders/Datadog.php +++ b/src/App/Model/Senders/Datadog.php @@ -1,8 +1,7 @@ client = $client->getClient(); - $this->client->setOption(Client::OP_DISCARD_BODY, $async); - $this->client_settings = $client->getClientSettings(); - - $this->async = $async; + $this->async = $async; } - /** + /** * Send log entry. * * @param string $index * @param array $body * @param string $type + * @throws \Amp\Http\Client\HttpException */ public function send(string $index, array $body) { @@ -83,16 +80,32 @@ public function send(string $index, array $body) if (count($this->batch[$index]) >= $this->client_settings['batch_size']) $this->sendNow($index); + } + + /** + * Perform the request. + * + * @param Request $request + * @return void + * @throws \Amp\Http\Client\HttpException + */ + protected function performRequest(Request $request) + { + $response = $this->client->request($request); + if (Params::get('verbose') && !$response->isSuccessful()) { + echo 'Error on send log ' . $response->getStatus(); + } } - /** - * Send logs immediately. - * - * @param string $index - * @return Generator - */ + /** + * Send logs immediately. + * + * @param string $index + * @return Generator + * @throws \Amp\Http\Client\HttpException + */ protected function sendNow(string $index) { $query_params = http_build_query([ @@ -101,18 +114,18 @@ protected function sendNow(string $index) 'hostname' => $this->batch[$index][0]['hostname'] ]); - $request = (new Request($this->client_settings['hosts'][0] . '?' . $query_params, 'POST')) - ->withHeader('Content-Type', 'application/json') - ->withBody(json_encode($this->batch[$index])); - - $this->client->request($request)->onResolve(function ($error, $response) { - if ($error && Params::get('verbose')) - { - echo 'Error on send log' . PHP_EOL; - var_dump($error); - } - }); - + $request = new Request( + $this->client_settings['hosts'][0] . '?' . $query_params, + 'POST', + json_encode($this->batch[$index]) + ); + $request->setHeader('Content-Type', 'application/json'); + + if ($this->async) { + Amp\async(fn() => $this->performRequest($request)); + } else { + $this->performRequest($request); + } $this->batch[$index] = []; } diff --git a/src/App/Model/Senders/ElasticSearch.php b/src/App/Model/Senders/ElasticSearch.php index 4228455..be99cd2 100755 --- a/src/App/Model/Senders/ElasticSearch.php +++ b/src/App/Model/Senders/ElasticSearch.php @@ -11,9 +11,8 @@ class Model_Senders_ElasticSearch implements Model_Contracts_Sender /** * Elastic Search client. * - * @var \Elasticsearch\Client */ - protected $client; + protected Elasticsearch\Client $client; /** @@ -21,7 +20,7 @@ class Model_Senders_ElasticSearch implements Model_Contracts_Sender * * @var array */ - protected $last_entry = []; + protected array $last_entry = []; /** @@ -29,7 +28,7 @@ class Model_Senders_ElasticSearch implements Model_Contracts_Sender * * @var bool $future_mode */ - protected $future_mode = false; + protected bool $future_mode = false; /** diff --git a/src/Config/Version.php b/src/Config/Version.php index e2050e3..0b99550 100644 --- a/src/Config/Version.php +++ b/src/Config/Version.php @@ -1,7 +1,7 @@ 2, - 'minor' => 7, - 'build' => 53, + 'major' => 4, + 'minor' => 0, + 'build' => 55, 'codename' => '', ); \ No newline at end of file