RetryN.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 35709 2009-12-14 14:14:14Z unknown $
  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. * @see Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
  24. */
  25. require_once 'Zend/Service/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php';
  26. /**
  27. * @see Zend_Service_WindowsAzure_RetryPolicy_Exception
  28. */
  29. require_once 'Zend/Service/WindowsAzure/RetryPolicy/Exception.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Service_WindowsAzure
  33. * @subpackage RetryPolicy
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Service_WindowsAzure_RetryPolicy_RetryN extends Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
  38. {
  39. /**
  40. * Number of retries
  41. *
  42. * @var int
  43. */
  44. protected $_retryCount = 1;
  45. /**
  46. * Interval between retries (in milliseconds)
  47. *
  48. * @var int
  49. */
  50. protected $_retryInterval = 0;
  51. /**
  52. * Constructor
  53. *
  54. * @param int $count Number of retries
  55. * @param int $intervalBetweenRetries Interval between retries (in milliseconds)
  56. */
  57. public function __construct($count = 1, $intervalBetweenRetries = 0)
  58. {
  59. $this->_retryCount = $count;
  60. $this->_retryInterval = $intervalBetweenRetries;
  61. }
  62. /**
  63. * Execute function under retry policy
  64. *
  65. * @param string|array $function Function to execute
  66. * @param array $parameters Parameters for function call
  67. * @return mixed
  68. */
  69. public function execute($function, $parameters = array())
  70. {
  71. $returnValue = null;
  72. for ($retriesLeft = $this->_retryCount; $retriesLeft >= 0; --$retriesLeft) {
  73. try {
  74. $returnValue = call_user_func_array($function, $parameters);
  75. return $returnValue;
  76. } catch (Exception $ex) {
  77. if ($retriesLeft == 1) {
  78. throw new Zend_Service_WindowsAzure_RetryPolicy_Exception("Exceeded retry count of " . $this->_retryCount . ". " . $ex->getMessage());
  79. }
  80. usleep($this->_retryInterval * 1000);
  81. }
  82. }
  83. }
  84. }