| 1 |
<?php |
| 2 |
|
| 3 |
namespace PostHog\Consumer; |
| 4 |
|
| 5 |
use PostHog\QueueConsumer; |
| 6 |
|
| 7 |
class ForkCurl extends QueueConsumer |
| 8 |
{ |
| 9 |
protected $type = "ForkCurl"; |
| 10 |
|
| 11 |
/** |
| 12 |
* Creates a new queued fork consumer which queues fork and identify |
| 13 |
* calls before adding them to |
| 14 |
* @param string $apiKey |
| 15 |
* @param array $options |
| 16 |
* boolean "debug" - whether to use debug output, wait for response. |
| 17 |
* number "max_queue_size" - the max size of messages to enqueue |
| 18 |
* number "batch_size" - how many messages to send in a single request |
| 19 |
*/ |
| 20 |
public function __construct($apiKey, $options = array()) |
| 21 |
{ |
| 22 |
parent::__construct($apiKey, $options); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Define getter method for consumer type |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public function getConsumer() |
| 31 |
{ |
| 32 |
return $this->type; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Make an async request to our API. Fork a curl process, immediately send |
| 37 |
* to the API. If debug is enabled, we wait for the response. |
| 38 |
* @param array $messages array of all the messages to send |
| 39 |
* @return boolean whether the request succeeded |
| 40 |
*/ |
| 41 |
public function flushBatch($messages) |
| 42 |
{ |
| 43 |
$body = $this->payload($messages); |
| 44 |
$payload = json_encode($body); |
| 45 |
|
| 46 |
// Escape for shell usage. |
| 47 |
$payload = escapeshellarg($payload); |
| 48 |
|
| 49 |
$protocol = $this->ssl() ? "https://" : "http://"; |
| 50 |
|
| 51 |
$path = "/batch/"; |
| 52 |
$url = $protocol . $this->host . $path; |
| 53 |
|
| 54 |
$cmd = "curl -X POST -H 'Content-Type: application/json'"; |
| 55 |
|
| 56 |
$tmpfname = ""; |
| 57 |
if ($this->compress_request) { |
| 58 |
// Compress request to file |
| 59 |
$tmpfname = tempnam("/tmp", "forkcurl_"); |
| 60 |
$cmd2 = "echo " . $payload . " | gzip > " . $tmpfname; |
| 61 |
exec($cmd2, $output, $exit); |
| 62 |
|
| 63 |
if (0 != $exit) { |
| 64 |
$this->handleError($exit, $output); |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
$cmd .= " -H 'Content-Encoding: gzip'"; |
| 69 |
|
| 70 |
$cmd .= " --data-binary '@" . $tmpfname . "'"; |
| 71 |
} else { |
| 72 |
$cmd .= " -d " . $payload; |
| 73 |
} |
| 74 |
|
| 75 |
$cmd .= " '" . $url . "'"; |
| 76 |
|
| 77 |
// Verify message size is below than 32KB |
| 78 |
if (strlen($payload) >= 32 * 1024) { |
| 79 |
if ($this->debug()) { |
| 80 |
$msg = "Message size is larger than 32KB"; |
| 81 |
error_log("[PostHog][" . $this->type . "] " . $msg); |
| 82 |
} |
| 83 |
|
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
// Send user agent in the form of {library_name}/{library_version} as per RFC 7231. |
| 88 |
$libName = $messages[0]['library']; |
| 89 |
$libVersion = $messages[0]['library_version']; |
| 90 |
$cmd .= " -H 'User-Agent: {$libName}/{$libVersion}'"; |
| 91 |
|
| 92 |
if (!$this->debug()) { |
| 93 |
$cmd .= " > /dev/null 2>&1 &"; |
| 94 |
} |
| 95 |
|
| 96 |
exec($cmd, $output, $exit); |
| 97 |
|
| 98 |
if (0 != $exit) { |
| 99 |
$this->handleError($exit, $output); |
| 100 |
} |
| 101 |
|
| 102 |
if ($tmpfname != "") { |
| 103 |
unlink($tmpfname); |
| 104 |
} |
| 105 |
|
| 106 |
return 0 == $exit; |
| 107 |
} |
| 108 |
} |
| 109 |
|