MixpanelBaseProducer.php
2 years ago
MixpanelEvents.php
2 years ago
MixpanelGroups.php
2 years ago
MixpanelPeople.php
2 years ago
index.php
2 years ago
MixpanelBaseProducer.php
115 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | require_once(dirname(__FILE__) . "/../Base/MixpanelBase.php"); |
| 4 | require_once(dirname(__FILE__) . "/../ConsumerStrategies/FileConsumer.php"); |
| 5 | require_once(dirname(__FILE__) . "/../ConsumerStrategies/CurlConsumer.php"); |
| 6 | require_once(dirname(__FILE__) . "/../ConsumerStrategies/SocketConsumer.php"); |
| 7 | if (!function_exists('json_encode')) { |
| 8 | throw new Exception('The JSON PHP extension is required.'); |
| 9 | } |
| 10 | abstract class Producers_MixpanelBaseProducer extends Base_MixpanelBase { |
| 11 | protected $_token; |
| 12 | private $_queue = array(); |
| 13 | private $_consumer = null; |
| 14 | private $_consumers = array( |
| 15 | "file" => "ConsumerStrategies_FileConsumer", |
| 16 | "curl" => "ConsumerStrategies_CurlConsumer", |
| 17 | "socket" => "ConsumerStrategies_SocketConsumer" |
| 18 | ); |
| 19 | protected $_max_queue_size = 1000; |
| 20 | public function __construct($token, $options = array()) { |
| 21 | parent::__construct($options); |
| 22 | // register any customer consumers |
| 23 | if (isset($options["consumers"])) { |
| 24 | $this->_consumers = array_merge($this->_consumers, $options['consumers']); |
| 25 | } |
| 26 | // set max queue size |
| 27 | if (isset($options["max_queue_size"])) { |
| 28 | $this->_max_queue_size = $options['max_queue_size']; |
| 29 | } |
| 30 | // associate token |
| 31 | $this->_token = $token; |
| 32 | if ($this->_debug()) { |
| 33 | $this->_log("Using token: ".$this->_token); |
| 34 | } |
| 35 | // instantiate the chosen consumer |
| 36 | $this->_consumer = $this->_getConsumer(); |
| 37 | } |
| 38 | public function __destruct() { |
| 39 | $attempts = 0; |
| 40 | $max_attempts = 10; |
| 41 | $success = false; |
| 42 | while (!$success && $attempts < $max_attempts) { |
| 43 | if ($this->_debug()) { |
| 44 | $this->_log("destruct flush attempt #".($attempts+1)); |
| 45 | } |
| 46 | $success = $this->flush(); |
| 47 | $attempts++; |
| 48 | } |
| 49 | } |
| 50 | public function flush($desired_batch_size = 50) { |
| 51 | $queue_size = count($this->_queue); |
| 52 | $succeeded = true; |
| 53 | $num_threads = $this->_consumer->getNumThreads(); |
| 54 | if ($this->_debug()) { |
| 55 | $this->_log("Flush called - queue size: ".$queue_size); |
| 56 | } |
| 57 | while($queue_size > 0 && $succeeded) { |
| 58 | $batch_size = min(array($queue_size, $desired_batch_size*$num_threads, $this->_options['max_batch_size']*$num_threads)); |
| 59 | $batch = array_splice($this->_queue, 0, $batch_size); |
| 60 | $succeeded = $this->_persist($batch); |
| 61 | if (!$succeeded) { |
| 62 | if ($this->_debug()) { |
| 63 | $this->_log("Batch consumption failed!"); |
| 64 | } |
| 65 | $this->_queue = array_merge($batch, $this->_queue); |
| 66 | if ($this->_debug()) { |
| 67 | $this->_log("added batch back to queue, queue size is now $queue_size"); |
| 68 | } |
| 69 | } |
| 70 | $queue_size = count($this->_queue); |
| 71 | if ($this->_debug()) { |
| 72 | $this->_log("Batch of $batch_size consumed, queue size is now $queue_size"); |
| 73 | } |
| 74 | } |
| 75 | return $succeeded; |
| 76 | } |
| 77 | public function reset() { |
| 78 | $this->_queue = array(); |
| 79 | } |
| 80 | public function getQueue() { |
| 81 | return $this->_queue; |
| 82 | } |
| 83 | public function getToken() { |
| 84 | return $this->_token; |
| 85 | } |
| 86 | protected function _getConsumer() { |
| 87 | $key = $this->_options['consumer']; |
| 88 | $Strategy = $this->_consumers[$key]; |
| 89 | if ($this->_debug()) { |
| 90 | $this->_log("Using consumer: " . $key . " -> " . $Strategy); |
| 91 | } |
| 92 | $this->_options['endpoint'] = $this->_getEndpoint(); |
| 93 | return new $Strategy($this->_options); |
| 94 | } |
| 95 | public function enqueue($message = array()) { |
| 96 | array_push($this->_queue, $message); |
| 97 | // force a flush if we've reached our threshold |
| 98 | if (count($this->_queue) > $this->_max_queue_size) { |
| 99 | $this->flush(); |
| 100 | } |
| 101 | if ($this->_debug()) { |
| 102 | $this->_log("Queued message: ".json_encode($message)); |
| 103 | } |
| 104 | } |
| 105 | public function enqueueAll($messages = array()) { |
| 106 | foreach($messages as $message) { |
| 107 | $this->enqueue($message); |
| 108 | } |
| 109 | } |
| 110 | protected function _persist($message) { |
| 111 | return $this->_consumer->persist($message); |
| 112 | } |
| 113 | abstract function _getEndpoint(); |
| 114 | } |
| 115 |