Bladeren bron

Added support for retry logic

psquickitjayant 10 jaren geleden
bovenliggende
commit
f258a2ed29
1 gewijzigde bestanden met toevoegingen van 52 en 18 verwijderingen
  1. 52 18
      src/Monolog/Handler/LogglyHandler.php

+ 52 - 18
src/Monolog/Handler/LogglyHandler.php

@@ -1,13 +1,13 @@
 <?php
 
 /*
- * This file is part of the Monolog package.
- *
- * (c) Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
+* This file is part of the Monolog package.
+*
+* (c) Jordi Boggiano <j.boggiano@seld.be>
+*
+* For the full copyright and license information, please view the LICENSE
+* file that was distributed with this source code.
+*/
 
 namespace Monolog\Handler;
 
@@ -15,12 +15,12 @@ use Monolog\Logger;
 use Monolog\Formatter\LogglyFormatter;
 
 /**
- * Sends errors to Loggly.
- *
- * @author Przemek Sobstel <przemek@sobstel.org>
- * @author Adam Pancutt <adam@pancutt.com>
- * @author Gregory Barchard <gregory@barchard.net>
- */
+* Sends errors to Loggly.
+*
+* @author Przemek Sobstel <przemek@sobstel.org>
+* @author Adam Pancutt <adam@pancutt.com>
+* @author Gregory Barchard <gregory@barchard.net>
+*/
 class LogglyHandler extends AbstractProcessingHandler
 {
     const HOST = 'logs-01.loggly.com';
@@ -92,11 +92,45 @@ class LogglyHandler extends AbstractProcessingHandler
         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
-        if (curl_exec($ch) === false) {
-            throw new \RuntimeException(sprintf('Curl error (code %s): %s', curl_errno($ch), curl_error($ch)));
-        }
-
-        curl_close($ch);
+        $RETRY_COUNT = 5;  
+        $TOTAL_RETRIES = 0;
+
+        /*
+        * Curl error code 6  : CURLE_COULDNT_RESOLVE_HOST  
+        * Curl error code 7  : CURLE_COULDNT_CONNECT
+        * Curl error code 9  : CURLE_REMOTE_ACCESS_DENIED 
+        * Curl error code 22 : CURLE_HTTP_RETURNED_ERROR
+        * Curl error code 25 : CURLE_UPLOAD_FAILED
+        * Curl error code 26 : CURLE_READ_ERROR 
+        * Curl error code 28 : CURLE_OPERATION_TIMEDOUT
+        * Curl error code 34 : CURLE_HTTP_POST_ERROR 
+        * Curl error code 35 : CURLE_SSL_CONNECT_ERROR 
+        *
+        * Curl Error Codes  : http://curl.haxx.se/libcurl/c/libcurl-errors.html
+        */
+        $CurlErrorCodesForRetries = array(6,7,9,22,25,26,28,34,35);
+
+        do
+        {
+            $TOTAL_RETRIES = $TOTAL_RETRIES + 1;
+            if (curl_exec($ch) === false) {
+                /*
+                    If the error cannot be controlled by retries or
+                    total retry count is already completed then
+                    show error and break the loop
+                */
+                if(in_array(curl_errno($ch),$CurlErrorCodesForRetries) === false 
+                        || $TOTAL_RETRIES > $RETRY_COUNT){
+                    echo sprintf('Curl error (code %s): %s', curl_errno($ch), curl_error($ch));
+                    curl_close($ch);
+                    break;
+                }
+            }
+            else{
+                curl_close($ch);
+                break;
+            }
+        }while($TOTAL_RETRIES <= $RETRY_COUNT);
     }
 
     protected function getDefaultFormatter()