web/lib/Zend/Service/WindowsAzure/RetryPolicy/RetryN.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service_WindowsAzure
       
    17  * @subpackage RetryPolicy
       
    18  * @version    $Id: RetryN.php 20785 2010-01-31 09:43:03Z mikaelkael $
       
    19  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
       
    25  */
       
    26 require_once 'Zend/Service/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_Service_WindowsAzure_RetryPolicy_Exception
       
    30  */
       
    31 require_once 'Zend/Service/WindowsAzure/RetryPolicy/Exception.php';
       
    32 
       
    33 /**
       
    34  * @category   Zend
       
    35  * @package    Zend_Service_WindowsAzure
       
    36  * @subpackage RetryPolicy
       
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    39  */
       
    40 class Zend_Service_WindowsAzure_RetryPolicy_RetryN extends Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
       
    41 {
       
    42     /**
       
    43      * Number of retries
       
    44      * 
       
    45      * @var int
       
    46      */
       
    47     protected $_retryCount = 1;
       
    48     
       
    49     /**
       
    50      * Interval between retries (in milliseconds)
       
    51      * 
       
    52      * @var int
       
    53      */
       
    54     protected $_retryInterval = 0;
       
    55     
       
    56     /**
       
    57      * Constructor
       
    58      * 
       
    59      * @param int $count                    Number of retries
       
    60      * @param int $intervalBetweenRetries   Interval between retries (in milliseconds)
       
    61      */
       
    62     public function __construct($count = 1, $intervalBetweenRetries = 0)
       
    63     {
       
    64         $this->_retryCount = $count;
       
    65         $this->_retryInterval = $intervalBetweenRetries;
       
    66     }
       
    67     
       
    68     /**
       
    69      * Execute function under retry policy
       
    70      * 
       
    71      * @param string|array $function       Function to execute
       
    72      * @param array        $parameters     Parameters for function call
       
    73      * @return mixed
       
    74      */
       
    75     public function execute($function, $parameters = array())
       
    76     {
       
    77         $returnValue = null;
       
    78         
       
    79         for ($retriesLeft = $this->_retryCount; $retriesLeft >= 0; --$retriesLeft) {
       
    80             try {
       
    81                 $returnValue = call_user_func_array($function, $parameters);
       
    82                 return $returnValue;
       
    83             } catch (Exception $ex) {
       
    84                 if ($retriesLeft == 1) {
       
    85                     throw new Zend_Service_WindowsAzure_RetryPolicy_Exception("Exceeded retry count of " . $this->_retryCount . ". " . $ex->getMessage());
       
    86                 }
       
    87                     
       
    88                 usleep($this->_retryInterval * 1000);
       
    89             }
       
    90         }
       
    91     }
       
    92 }