diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Exception/MetricException.php | 17 | ||||
-rw-r--r-- | src/HttpClient.php | 2 | ||||
-rw-r--r-- | src/Metrics.php | 103 | ||||
-rw-r--r-- | src/Worker.php | 2 | ||||
-rw-r--r-- | src/bin/rr | 292 |
5 files changed, 122 insertions, 294 deletions
diff --git a/src/Exception/MetricException.php b/src/Exception/MetricException.php new file mode 100644 index 00000000..2d12eefe --- /dev/null +++ b/src/Exception/MetricException.php @@ -0,0 +1,17 @@ +<?php +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ +declare(strict_types=1); + +namespace Spiral\RoadRunner\Exception; + +use Spiral\Goridge\Exceptions\RPCException; + +class MetricException extends RPCException +{ + +}
\ No newline at end of file diff --git a/src/HttpClient.php b/src/HttpClient.php index f31a9b50..d2862ad8 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -8,7 +8,7 @@ declare(strict_types=1); namespace Spiral\RoadRunner; -class HttpClient +final class HttpClient { /** @var Worker */ private $worker; diff --git a/src/Metrics.php b/src/Metrics.php new file mode 100644 index 00000000..c07a1e7a --- /dev/null +++ b/src/Metrics.php @@ -0,0 +1,103 @@ +<?php +/** + * Spiral Framework. + * + * @license MIT + * @author Anton Titov (Wolfy-J) + */ +declare(strict_types=1); + +namespace Spiral\RoadRunner; + +use Spiral\Goridge\Exceptions\RPCException; +use Spiral\Goridge\RPC; +use Spiral\RoadRunner\Exception\MetricException; + +/** + * Application metrics. + */ +final class Metrics +{ + /** @var RPC */ + private $rpc; + + /** + * @param RPC $rpc + */ + public function __construct(RPC $rpc) + { + $this->rpc = $rpc; + } + + /** + * Add collector value. Fallback to appropriate method of related collector. + * + * @param string $collector + * @param float $value + * @param array $labels + * + * @throws MetricException + */ + public function add(string $collector, float $value, array $labels = []) + { + try { + $this->rpc->call('metrics.Add', compact('collector', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Subtract the collector value, only for gauge collector. + * + * @param string $collector + * @param float $value + * @param array $labels + * + * @throws MetricException + */ + public function sub(string $collector, float $value, array $labels = []) + { + try { + $this->rpc->call('metrics.Sub', compact('collector', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Observe collector value, only for histogram and summary collectors. + * + * @param string $collector + * @param float $value + * @param array $labels + * + * @throws MetricException + */ + public function observe(string $collector, float $value, array $labels = []) + { + try { + $this->rpc->call('metrics.Observe', compact('collector', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } + + /** + * Set collector value, only for gauge collector. + * + * @param string $collector + * @param float $value + * @param array $labels + * + * @throws MetricException + */ + public function set(string $collector, float $value, array $labels = []) + { + try { + $this->rpc->call('metrics.Set', compact('collector', 'value', 'labels')); + } catch (RPCException $e) { + throw new MetricException($e->getMessage(), $e->getCode(), $e); + } + } +}
\ No newline at end of file diff --git a/src/Worker.php b/src/Worker.php index b67ebd3b..00cf072a 100644 --- a/src/Worker.php +++ b/src/Worker.php @@ -22,7 +22,7 @@ use Spiral\RoadRunner\Exception\RoadRunnerException; * $worker->send("DONE", json_encode($context)); * } */ -class Worker +final class Worker { // Send as response context to request worker termination const STOP = '{"stop":true}'; diff --git a/src/bin/rr b/src/bin/rr deleted file mode 100644 index 2b392467..00000000 --- a/src/bin/rr +++ /dev/null @@ -1,292 +0,0 @@ -#!/usr/bin/env php -<?php -/** - * RoadRunner - * High-performance PHP process supervisor and load balancer written in Go - * - * This file responsive for cli commands - */ -declare(strict_types=1); - -foreach ([ - __DIR__ . '/../../../../autoload.php', - __DIR__ . '/../../vendor/autoload.php', - __DIR__ . '/vendor/autoload.php' - ] as $file) { - if (file_exists($file)) { - define('RR_COMPOSER_INSTALL', $file); - - break; - } -} - -unset($file); - -if (!defined('RR_COMPOSER_INSTALL')) { - fwrite( - STDERR, - 'You need to set up the project dependencies using Composer:' . PHP_EOL . PHP_EOL . - ' composer install' . PHP_EOL . PHP_EOL . - 'You can learn all about Composer on https://getcomposer.org/.' . PHP_EOL - ); - - die(1); -} - -if (RRHelper::getOSType() !== 'linux' && !class_exists('ZipArchive')) { - fwrite(STDERR, 'Extension `php-zip` is required.' . PHP_EOL); - die(1); -} - -if (!function_exists('curl_init')) { - fwrite(STDERR, 'Extension `php-curl` is required.' . PHP_EOL); - die(1); -} - -require RR_COMPOSER_INSTALL; - -use Symfony\Component\Console\Application; -use Symfony\Component\Console\Helper\ProgressBar; -use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Output\OutputInterface; -use Symfony\Component\Console\Question\ConfirmationQuestion; - -class RRHelper -{ - /** - * Returns version of RoadRunner based on build.sh file - * - * @return string Version of RoadRunner - * @throws Exception - */ - public static function getVersion(): string - { - $file = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'build.sh'; - $fileResource = fopen($file, 'r') or die(1); - while (!feof($fileResource)) { - $line = fgets($fileResource, 4096); - $matches = []; - if (preg_match("/^RR_VERSION=(.*)/", $line, $matches)) { - return trim($matches[1]); - } - } - fclose($fileResource); - throw new Exception("Can't find version of RoadRunner"); - } - - /** - * Returns OS Type for filename - * - * @return string OS Type - */ - public static function getOSType(): string - { - switch (PHP_OS) { - case 'Darwin': - return 'darwin'; - case 'Linux': - return 'linux'; - case 'FreeBSD': - return 'freebsd'; - case 'WIN32': - case 'WINNT': - case 'Windows': - return 'windows'; - default: - return 'linux'; - } - } - - /** - * @return string - * @throws Exception - */ - public static function getSignature(): string - { - return 'roadrunner-' . self::getVersion() . '-' . self::getOSType() . '-amd64'; - } - - /** - * Returns generated URL to zip file on GitHub with binary file - * - * @return string URL - * @throws Exception - */ - public static function getBinaryDownloadUrl(): string - { - $ext = '.zip'; - if (self::getOSType() == 'linux') { - $ext = '.tar.gz'; - } - - return 'https://github.com/spiral/roadrunner/releases/download/v' - . static::getVersion() . '/' . self::getSignature() - . $ext; - } - - /** - * Extracts the roadrunner RR binary into given location. - * - * @param string $archive - * @param string $target - * @throws Exception - */ - public static function extractBinary(string $archive, string $target) - { - if (self::getOSType() !== 'linux') { - self::extractZIP($archive, $target); - } else { - self::extractTAR($archive, $target); - } - } - - /** - * @param string $archive - * @param string $target - * @throws Exception - */ - protected static function extractZIP(string $archive, string $target) - { - $zip = new ZipArchive(); - $zip->open($archive); - - $name = self::getSignature() . '/rr'; - if (self::getOSType() == 'windows') { - $name .= '.exe'; - } - - $stream = $zip->getStream($name); - if (!is_resource($stream)) { - return; - } - - $to = fopen($target, 'w'); - stream_copy_to_stream($stream, $to); - fclose($to); - - $zip->close(); - } - - /** - * @param string $archive - * @param string $target - * @throws Exception - */ - protected static function extractTAR(string $archive, string $target) - { - $arch = new PharData($archive); - $arch->extractTo('./', self::getSignature() . '/rr'); - - copy('./' . self::getSignature() . '/rr', $target); - unlink('./' . self::getSignature() . '/rr'); - rmdir('./' . self::getSignature()); - } -} - -(new Application('RoadRunner', RRHelper::getVersion())) - ->register('get-binary') - ->setDescription("Install or update RoadRunner binaries in specified folder (current folder by default)") - ->addOption('location', 'l', InputArgument::OPTIONAL, 'destination folder', '.') - ->setCode(function (InputInterface $input, OutputInterface $output) { - $output->writeln('<info>Updating binary file of RoadRunner</info>'); - - $finalFile = $input->getOption('location') . DIRECTORY_SEPARATOR . 'rr'; - if (RRHelper::getOSType() == 'windows') { - $finalFile .= '.exe'; - } - - if (is_file($finalFile)) { - $output->writeln('<error>RoadRunner binary file already exists!</error>'); - $helper = $this->getHelper('question'); - $question = new ConfirmationQuestion('Do you want overwrite it? [Y/n] '); - - if (!$helper->ask($input, $output, $question)) { - return; - } - } - - $output->writeln('<info>Downloading RoadRunner archive for <fg=cyan>' . ucfirst(RRHelper::getOSType()) . '</fg=cyan></info>'); - - $progressBar = new ProgressBar($output); - $progressBar->setFormat('verbose'); - - $zipFileName = tempnam('.', "rr_zip"); - if (RRHelper::getOSType() == 'linux') { - $zipFileName .= '.tar.gz'; - } - - $zipFile = fopen($zipFileName, "w+"); - $curlResource = curl_init(); - - curl_setopt($curlResource, CURLOPT_URL, RRHelper::getBinaryDownloadUrl()); - curl_setopt($curlResource, CURLOPT_RETURNTRANSFER, true); - curl_setopt($curlResource, CURLOPT_BINARYTRANSFER, true); - curl_setopt($curlResource, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curlResource, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($curlResource, CURLOPT_FILE, $zipFile); - curl_setopt($curlResource, CURLOPT_PROGRESSFUNCTION, - function ($resource, $download_size, $downloaded, $upload_size, $uploaded) use (&$progressBar, $output) { - if ($download_size == 0) { - return; - } - - if ($progressBar->getStartTime() === 0) { - $progressBar->start(); - } - - if ($progressBar->getMaxSteps() != $download_size) { - /** - * Workaround for symfony < 4.1.x, for example PHP 7.0 will use 3.x - * feature #26449 Make ProgressBar::setMaxSteps public (ostrolucky) - */ - $progressBar = new ProgressBar($output, $download_size); - } - - $progressBar->setFormat('[%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% ' . intval($download_size / 1024) . 'KB'); - $progressBar->setProgress($downloaded); - }); - curl_setopt($curlResource, CURLOPT_NOPROGRESS, false); // needed to make progress function work - curl_setopt($curlResource, CURLOPT_HEADER, 0); - curl_exec($curlResource); - curl_close($curlResource); - fclose($zipFile); - - $progressBar->finish(); - $output->writeln(""); - - $output->writeln('<info>Unpacking <comment>' . basename(RRHelper::getBinaryDownloadUrl()) . '</comment></info>'); - - RRHelper::extractBinary($zipFileName, $finalFile); - unlink($zipFileName); - - if (!file_exists($finalFile) || filesize($finalFile) === 0) { - throw new Exception('Unable to extract the file.'); - } - - chmod($finalFile, 0755); - $output->writeln('<info>Binary file updated!</info>'); - }) - ->getApplication() - ->register("init-config") - ->setDescription("Inits default .rr.yaml config in specified folder (current folder by default)") - ->addOption('location', 'l', InputArgument::OPTIONAL, 'destination folder', '.') - ->setCode(function (InputInterface $input, OutputInterface $output) { - if (is_file($input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml')) { - $output->writeln('<error>Config file already exists!</error>'); - $helper = $this->getHelper('question'); - $question = new ConfirmationQuestion('Do you want overwrite it? [Y/n] '); - - if (!$helper->ask($input, $output, $question)) { - return; - } - } - - copy( - __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '.rr.yaml', - $input->getOption('location') . DIRECTORY_SEPARATOR . '.rr.yaml' - ); - $output->writeln('<info>Config file created!</info>'); - }) - ->getApplication() - ->run(); |