Fix runLocally test

This commit is contained in:
Anton Medvedev 2017-05-03 20:57:51 +07:00
parent 542e5f0dc2
commit 998592b5ef
4 changed files with 67 additions and 27 deletions

View File

@ -5,6 +5,7 @@
### Fixed ### Fixed
- Fixed command parsing in runLocally func - Fixed command parsing in runLocally func
- Added `Request` class for get/post json requests
## v5.0.0-beta.2 ## v5.0.0-beta.2

View File

@ -347,6 +347,10 @@ class Deployer extends Container
$stats['exception'] = get_class($commandEvent->getException()); $stats['exception'] = get_class($commandEvent->getException());
} }
if ($stats['command_name'] === 'worker') {
return;
}
Reporter::report($stats); Reporter::report($stats);
} }
} }

View File

@ -27,38 +27,13 @@ class Reporter
if (is_null($pid) || $pid === -1) { if (is_null($pid) || $pid === -1) {
// Fork fails or there is no `pcntl` extension. // Fork fails or there is no `pcntl` extension.
self::send($stats); Request::post(self::ENDPOINT, $stats);
} elseif ($pid === 0) { } elseif ($pid === 0) {
// Child process. // Child process.
posix_setsid(); posix_setsid();
self::send($stats); Request::post(self::ENDPOINT, $stats);
// Close child process after doing job. // Close child process after doing job.
exit(0); exit(0);
} }
} }
/**
* @param array $stats
*/
private static function send(array $stats)
{
if (extension_loaded('curl')) {
$body = json_encode($stats, JSON_PRETTY_PRINT);
$ch = curl_init(self::ENDPOINT);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($body)
]);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_exec($ch);
} else {
file_get_contents(self::ENDPOINT . '?' . http_build_query($stats));
}
}
} }

60
src/Utility/Request.php Normal file
View File

@ -0,0 +1,60 @@
<?php
/* (c) Anton Medvedev <anton@medv.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Deployer\Utility;
/**
* @codeCoverageIgnore
*/
class Request
{
/**
* @param string $url
* @param array $query Query params for request
* @return array
*/
public static function get($url, $query)
{
return self::curl('GET', $url, $query);
}
/**
* @param string $url
* @param array $data Post fields data, send as json with `Content-Type: application/json`.
* @return array
*/
public static function post($url, $data)
{
return self::curl('POST', $url, [], $data);
}
private static function curl($method, $url, $query = [], $data = [])
{
$ch = curl_init($url . '?' . http_build_query($query));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method === 'POST' ? 'POST' : 'GET');
if (!empty($data)) {
$body = json_encode($data, JSON_PRETTY_PRINT);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($body)
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$result = curl_exec($ch);
curl_close($ch);
$response = @json_decode($result, true);
if ($data === null && json_last_error() !== JSON_ERROR_NONE) {
return null;
}
return $response;
}
}