"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "zaqar/notification/tasks/webhook.py" between
zaqar-5.0.0.tar.gz and zaqar-7.0.0.tar.gz

About: OpenStack Zaqar is a multi-tenant cloud messaging and notification service for web and mobile developers.
The "Rocky" series (latest release).

webhook.py  (zaqar-5.0.0):webhook.py  (zaqar-7.0.0)
skipping to change at line 16 skipping to change at line 16
# #
# http://www.apache.org/licenses/LICENSE-2.0 # http://www.apache.org/licenses/LICENSE-2.0
# #
# Unless required by applicable law or agreed to in writing, software # Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, # distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. # implied.
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import math
import time import time
import json import json
from oslo_log import log as logging from oslo_log import log as logging
import requests import requests
from zaqar.common import consts from zaqar.common import consts
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
def _Linear_function(minimum_delay, maximum_delay, times): def _Linear_function(minimum_delay, maximum_delay, times):
return range(minimum_delay, maximum_delay, times) return range(minimum_delay, maximum_delay, times)
RETRY_BACKOFF_FUNCTION_MAP = {'linear': _Linear_function} def _Geometric_function(minimum_delay, maximum_delay, times):
x_max = int((maximum_delay - minimum_delay) / times)
k = math.pow(10, math.log10(maximum_delay/minimum_delay)/(x_max-1))
xarray = range(1, x_max+1)
return [int(minimum_delay*math.pow(k, a-1)) for a in xarray]
def _Exponential_function(minimum_delay, maximum_delay, times):
x_max = int((maximum_delay - minimum_delay) / times)
k = math.pow(10, math.log10(maximum_delay/minimum_delay)/(x_max-1))
p = minimum_delay/k
xarray = range(1, x_max+1)
return [int(p*math.pow(k, a)) for a in xarray]
def _Arithmetic_function(minimum_delay, maximum_delay, times):
x_max = int((maximum_delay - minimum_delay) / times)
d = 2.0 * (maximum_delay - minimum_delay) / (x_max * (x_max - 1))
xarray = range(1, x_max+1)
return [int(minimum_delay+(a-1)*a*d/2) for a in xarray]
RETRY_BACKOFF_FUNCTION_MAP = {'linear': _Linear_function,
'arithmetic': _Arithmetic_function,
'geometric': _Geometric_function,
'exponential': _Exponential_function}
class WebhookTask(object): class WebhookTask(object):
def _post_request_success(self, subscriber, data, headers): def _post_request_success(self, subscriber, data, headers):
try: try:
response = requests.post(subscriber, data=data, headers=headers) response = requests.post(subscriber, data=data, headers=headers)
if response and (response.status_code in range(200, 500)): if response and (response.status_code in range(200, 500)):
return True return True
except Exception as e: except Exception as e:
LOG.exception('post request got exception in retry: %s.', str(e)) LOG.exception('post request got exception in retry: %s.', str(e))
skipping to change at line 66 skipping to change at line 89
return return
# Pre-Backoff Phase # Pre-Backoff Phase
for minimum_delay_retry in range( for minimum_delay_retry in range(
0, retry_policy.get('minimum_delay_retries', 0, retry_policy.get('minimum_delay_retries',
consts.MINIMUM_DELAY_RETRIES)): consts.MINIMUM_DELAY_RETRIES)):
LOG.debug('Retry with minimum delay, count: %s', LOG.debug('Retry with minimum delay, count: %s',
minimum_delay_retry) minimum_delay_retry)
time.sleep(retry_policy.get('minimum_delay', consts.MINIMUM_DELAY)) time.sleep(retry_policy.get('minimum_delay', consts.MINIMUM_DELAY))
if self._post_request_success(subscriber, data, headers): if self._post_request_success(subscriber, data, headers):
return return
# Backoff Phase: Linear retry # Now we support linear,arithmetic,
# TODO(wanghao): Now we only support the linear function, we should # exponential and geometric retry backoff function.
# support more in Queens.
retry_function = retry_policy.get('retry_backoff_function', 'linear') retry_function = retry_policy.get('retry_backoff_function', 'linear')
backoff_function = RETRY_BACKOFF_FUNCTION_MAP[retry_function] backoff_function = RETRY_BACKOFF_FUNCTION_MAP[retry_function]
for i in backoff_function(retry_policy.get('minimum_delay', for i in backoff_function(retry_policy.get('minimum_delay',
consts.MINIMUM_DELAY), consts.MINIMUM_DELAY),
retry_policy.get('maximum_delay', retry_policy.get('maximum_delay',
consts.MAXIMUM_DELAY), consts.MAXIMUM_DELAY),
consts.LINEAR_INTERVAL): consts.LINEAR_INTERVAL):
LOG.debug('Retry with retry_backoff_function, sleep: %s seconds', LOG.debug('Retry with function:%s, sleep: %s seconds',
i) retry_function, i)
time.sleep(i) time.sleep(i)
if self._post_request_success(subscriber, data, headers): if self._post_request_success(subscriber, data, headers):
return return
# Post-Backoff Phase # Post-Backoff Phase
for maximum_delay_retries in range( for maximum_delay_retries in range(
0, retry_policy.get('maximum_delay_retries', 0, retry_policy.get('maximum_delay_retries',
consts.MAXIMUM_DELA_RETRIES)): consts.MAXIMUM_DELAY_RETRIES)):
LOG.debug('Retry with maximum delay, count: %s', LOG.debug('Retry with maximum delay, count: %s',
maximum_delay_retries) maximum_delay_retries)
time.sleep(retry_policy.get('maximum_delay', consts.MAXIMUM_DELAY)) time.sleep(retry_policy.get('maximum_delay', consts.MAXIMUM_DELAY))
if self._post_request_success(subscriber, data, headers): if self._post_request_success(subscriber, data, headers):
return return
LOG.debug('Send request retries are all failed.') LOG.debug('Send request retries are all failed.')
def execute(self, subscription, messages, headers=None, **kwargs): def execute(self, subscription, messages, headers=None, **kwargs):
if headers is None: if headers is None:
headers = {'Content-Type': 'application/json'} headers = {'Content-Type': 'application/json'}
 End of changes. 5 change blocks. 
7 lines changed or deleted 29 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)