Skip to content

Commit 93621cf

Browse files
committed
Merge pull request #2 from dimitrytarasov/master
[Exec transport only] Divide huge log batches into chunks and send them one by one
2 parents fbb43ca + d88379d commit 93621cf

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ all transports are available for standalone logger as well.
3232

3333
## License
3434

35-
Copyright 2014 Stackify, LLC.
35+
Copyright 2015 Stackify, LLC.
3636

3737
Licensed under the Apache License, Version 2.0 (the "License");
3838
you may not use this file except in compliance with the License.
@@ -44,4 +44,4 @@ Unless required by applicable law or agreed to in writing, software
4444
distributed under the License is distributed on an "AS IS" BASIS,
4545
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
4646
See the License for the specific language governing permissions and
47-
limitations under the License.
47+
limitations under the License.

src/Stackify/Log/Transport/AbstractApiTransport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ abstract class AbstractApiTransport extends AbstractTransport
1111

1212
protected $apiKey;
1313
protected $proxy;
14-
private $queue = array();
14+
protected $queue = array();
1515

1616
public function __construct($apiKey, array $options = array())
1717
{

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)