Skip to content

Commit 4a423ef

Browse files
Dmitry TarasovDmitry Tarasov
authored andcommitted
Divide huge log batches into chunks and send them one by one (exec transport only)
1 parent c08d498 commit 4a423ef

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

src/Stackify/Log/Transport/ExecTransport.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,19 @@ public function __construct($apiKey, array $options = array())
3030
}
3131
}
3232

33+
/**
34+
* Overrides parent's method
35+
*/
36+
public function finish()
37+
{
38+
if (!empty($this->queue)) {
39+
// empty queue to avoid duplicates
40+
$queue = $this->queue;
41+
$this->queue = array();
42+
$this->sendChunk($queue);
43+
}
44+
}
45+
3346
protected function getAllowedOptions()
3447
{
3548
return array_merge(parent::getAllowedOptions(), array(
@@ -42,6 +55,27 @@ protected function getTransportName()
4255
return 'ExecTransport';
4356
}
4457

58+
protected function sendChunk(array $items)
59+
{
60+
$json = $this->messageBuilder->getApiMessage($items);
61+
$jsonLength = strlen($json);
62+
$count = count($items);
63+
if ($jsonLength > self::MAX_POST_LENGTH) {
64+
if (1 === $count) {
65+
// it makes no sense to divide into chunks, just fail
66+
$this->logError(self::ERROR_LENGTH, $jsonLength);
67+
return;
68+
}
69+
$maxCount = floor($count / ceil($jsonLength / self::MAX_POST_LENGTH));
70+
$chunks = array_chunk($items, $maxCount);
71+
foreach ($chunks as $chunk) {
72+
$this->sendChunk($chunk);
73+
}
74+
} else {
75+
$this->send($json);
76+
}
77+
}
78+
4579
protected function send($data)
4680
{
4781
$url = Api::API_BASE_URL . Api::API_CALL_LOGS;
@@ -61,11 +95,6 @@ protected function send($data)
6195
// return immediately while curl will run in the background
6296
$cmd .= ' > /dev/null 2>&1 &';
6397
}
64-
$cmdLength = strlen($cmd);
65-
if ($cmdLength > self::MAX_POST_LENGTH) {
66-
$this->logError(self::ERROR_LENGTH, $cmdLength);
67-
return;
68-
}
6998
$output = array();
7099
$r = exec($cmd, $output, $result);
71100
// if debug mode is off, it makes no sense to check result,

0 commit comments

Comments
 (0)