| 1 |
<?php |
| 2 |
|
| 3 |
namespace PostHog; |
| 4 |
|
| 5 |
abstract class QueueConsumer extends Consumer |
| 6 |
{ |
| 7 |
protected $type = "QueueConsumer"; |
| 8 |
|
| 9 |
protected $queue; |
| 10 |
protected $max_queue_size = 1000; |
| 11 |
protected $batch_size = 100; |
| 12 |
protected $maximum_backoff_duration = 10000; // Set maximum waiting limit to 10s |
| 13 |
protected $host = "app.posthog.com"; |
| 14 |
protected $compress_request = false; |
| 15 |
|
| 16 |
/** |
| 17 |
* Store our api key and options as part of this consumer |
| 18 |
* @param string $apiKey |
| 19 |
* @param array $options |
| 20 |
*/ |
| 21 |
public function __construct($apiKey, $options = array()) |
| 22 |
{ |
| 23 |
parent::__construct($apiKey, $options); |
| 24 |
|
| 25 |
if (isset($options["max_queue_size"])) { |
| 26 |
$this->max_queue_size = $options["max_queue_size"]; |
| 27 |
} |
| 28 |
|
| 29 |
if (isset($options["batch_size"])) { |
| 30 |
$this->batch_size = $options["batch_size"]; |
| 31 |
} |
| 32 |
|
| 33 |
if (isset($options["host"])) { |
| 34 |
$this->host = $options["host"]; |
| 35 |
|
| 36 |
if ($this->host && preg_match("/^https?:\\/\\//i", $this->host)) { |
| 37 |
$this->options['ssl'] = substr($this->host, 0, 5) == 'https'; |
| 38 |
$this->host = preg_replace("/^https?:\\/\\//i", "", $this->host); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
if (isset($options["compress_request"])) { |
| 43 |
$this->compress_request = json_decode($options["compress_request"]); |
| 44 |
} |
| 45 |
|
| 46 |
$this->queue = array(); |
| 47 |
} |
| 48 |
|
| 49 |
public function __destruct() |
| 50 |
{ |
| 51 |
// Flush our queue on destruction |
| 52 |
$this->flush(); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Captures a user action |
| 57 |
* |
| 58 |
* @param array $message |
| 59 |
* @return boolean whether the capture call succeeded |
| 60 |
*/ |
| 61 |
public function capture(array $message) |
| 62 |
{ |
| 63 |
return $this->enqueue($message); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Tags properties about the user. |
| 68 |
* |
| 69 |
* @param array $message |
| 70 |
* @return boolean whether the identify call succeeded |
| 71 |
*/ |
| 72 |
public function identify(array $message) |
| 73 |
{ |
| 74 |
return $this->enqueue($message); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Aliases from one user id to another |
| 79 |
* |
| 80 |
* @param array $message |
| 81 |
* @return boolean whether the alias call succeeded |
| 82 |
*/ |
| 83 |
public function alias(array $message) |
| 84 |
{ |
| 85 |
return $this->enqueue($message); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Flushes our queue of messages by batching them to the server |
| 90 |
*/ |
| 91 |
public function flush() |
| 92 |
{ |
| 93 |
$count = count($this->queue); |
| 94 |
$success = true; |
| 95 |
|
| 96 |
while ($count > 0 && $success) { |
| 97 |
$batch = array_splice($this->queue, 0, min($this->batch_size, $count)); |
| 98 |
$success = $this->flushBatch($batch); |
| 99 |
|
| 100 |
$count = count($this->queue); |
| 101 |
} |
| 102 |
|
| 103 |
return $success; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Adds an item to our queue. |
| 108 |
* @param mixed $item |
| 109 |
* @return boolean whether call has succeeded |
| 110 |
*/ |
| 111 |
public function enqueue($item) |
| 112 |
{ |
| 113 |
$count = count($this->queue); |
| 114 |
|
| 115 |
if ($count > $this->max_queue_size) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
$count = array_push($this->queue, $item); |
| 120 |
|
| 121 |
if ($count >= $this->batch_size) { |
| 122 |
return $this->flush(); // return ->flush() result: true on success |
| 123 |
} |
| 124 |
|
| 125 |
return true; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Given a batch of messages the method returns |
| 130 |
* a valid payload. |
| 131 |
* |
| 132 |
* @param {Array} $batch |
| 133 |
* @return {Array} |
| 134 |
*/ |
| 135 |
protected function payload($batch) |
| 136 |
{ |
| 137 |
return array( |
| 138 |
"batch" => $batch, |
| 139 |
"api_key" => $this->apiKey, |
| 140 |
); |
| 141 |
} |
| 142 |
} |
| 143 |
|