RetryN.php 2.8 KB

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