MixpanelBaseProducer.php
6.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
require_once(dirname(__FILE__) . "/../Base/MixpanelBase.php");
require_once(dirname(__FILE__) . "/../ConsumerStrategies/FileConsumer.php");
require_once(dirname(__FILE__) . "/../ConsumerStrategies/CurlConsumer.php");
require_once(dirname(__FILE__) . "/../ConsumerStrategies/SocketConsumer.php");
if (!function_exists('json_encode')) {
throw new Exception('The JSON PHP extension is required.');
}
/**
* Provides some base methods for use by a message Producer
*/
abstract class Producers_MixpanelBaseProducer extends Base_MixpanelBase {
/**
* @var string a token associated to a Mixpanel project
*/
protected $_token;
/**
* @var array a queue to hold messages in memory before flushing in batches
*/
private $_queue = array();
/**
* @var ConsumerStrategies_AbstractConsumer the consumer to use when flushing messages
*/
private $_consumer = null;
/**
* @var array The list of available consumers
*/
private $_consumers = array(
"file" => "ConsumerStrategies_FileConsumer",
"curl" => "ConsumerStrategies_CurlConsumer",
"socket" => "ConsumerStrategies_SocketConsumer"
);
/**
* If the queue reaches this size we'll auto-flush to prevent out of memory errors
* @var int
*/
protected $_max_queue_size = 1000;
/**
* Creates a new MixpanelBaseProducer, assings Mixpanel project token, registers custom Consumers, and instantiates
* the desired consumer
* @param $token
* @param array $options
*/
public function __construct($token, $options = array()) {
parent::__construct($options);
// register any customer consumers
if (isset($options["consumers"])) {
$this->_consumers = array_merge($this->_consumers, $options['consumers']);
}
// set max queue size
if (isset($options["max_queue_size"])) {
$this->_max_queue_size = $options['max_queue_size'];
}
// associate token
$this->_token = $token;
if ($this->_debug()) {
$this->_log("Using token: ".$this->_token);
}
// instantiate the chosen consumer
$this->_consumer = $this->_getConsumer();
}
/**
* Flush the queue when we destruct the client with retries
*/
public function __destruct() {
$attempts = 0;
$max_attempts = 10;
$success = false;
while (!$success && $attempts < $max_attempts) {
if ($this->_debug()) {
$this->_log("destruct flush attempt #".($attempts+1));
}
$success = $this->flush();
$attempts++;
}
}
/**
* Iterate the queue and write in batches using the instantiated Consumer Strategy
* @param int $desired_batch_size
* @return bool whether or not the flush was successful
*/
public function flush($desired_batch_size = 50) {
$queue_size = count($this->_queue);
$succeeded = true;
$num_threads = $this->_consumer->getNumThreads();
if ($this->_debug()) {
$this->_log("Flush called - queue size: ".$queue_size);
}
while($queue_size > 0 && $succeeded) {
$batch_size = min(array($queue_size, $desired_batch_size*$num_threads, $this->_options['max_batch_size']*$num_threads));
$batch = array_splice($this->_queue, 0, $batch_size);
$succeeded = $this->_persist($batch);
if (!$succeeded) {
if ($this->_debug()) {
$this->_log("Batch consumption failed!");
}
$this->_queue = array_merge($batch, $this->_queue);
if ($this->_debug()) {
$this->_log("added batch back to queue, queue size is now $queue_size");
}
}
$queue_size = count($this->_queue);
if ($this->_debug()) {
$this->_log("Batch of $batch_size consumed, queue size is now $queue_size");
}
}
return $succeeded;
}
/**
* Empties the queue without persisting any of the messages
*/
public function reset() {
$this->_queue = array();
}
/**
* Returns the in-memory queue
* @return array
*/
public function getQueue() {
return $this->_queue;
}
/**
* Returns the current Mixpanel project token
* @return string
*/
public function getToken() {
return $this->_token;
}
/**
* Given a strategy type, return a new PersistenceStrategy object
* @return ConsumerStrategies_AbstractConsumer
*/
protected function _getConsumer() {
$key = $this->_options['consumer'];
$Strategy = $this->_consumers[$key];
if ($this->_debug()) {
$this->_log("Using consumer: " . $key . " -> " . $Strategy);
}
$this->_options['endpoint'] = $this->_getEndpoint();
return new $Strategy($this->_options);
}
/**
* Add an array representing a message to be sent to Mixpanel to a queue.
* @param array $message
*/
public function enqueue($message = array()) {
array_push($this->_queue, $message);
// force a flush if we've reached our threshold
if (count($this->_queue) > $this->_max_queue_size) {
$this->flush();
}
if ($this->_debug()) {
$this->_log("Queued message: ".json_encode($message));
}
}
/**
* Add an array representing a list of messages to be sent to Mixpanel to a queue.
* @param array $messages
*/
public function enqueueAll($messages = array()) {
foreach($messages as $message) {
$this->enqueue($message);
}
}
/**
* Given an array of messages, persist it with the instantiated Persistence Strategy
* @param $message
* @return mixed
*/
protected function _persist($message) {
return $this->_consumer->persist($message);
}
/**
* Return the endpoint that should be used by a consumer that consumes messages produced by this producer.
* @return string
*/
abstract function _getEndpoint();
}