Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #882 from groovecoder/newsletter-exceptions-821255

fix bug 821255 - retry basket for transient errors
  • Loading branch information...
commit a91cc2909187c39c9b789436f375fe2749de1ac8 2 parents b3671f1 + 228ac82
@darkwing darkwing authored
Showing with 46 additions and 14 deletions.
  1. +19 −3 apps/landing/test_views.py
  2. +22 −6 apps/landing/views.py
  3. +5 −5 settings.py
View
22 apps/landing/test_views.py
@@ -3,6 +3,10 @@
from mock import patch
from pyquery import PyQuery as pq
import test_utils
+
+import basket
+
+import constance.config
from waffle.models import Switch
from sumo.tests import LocalizingClient
@@ -97,9 +101,7 @@ class AppsViewsTest(test_utils.TestCase):
def setUp(self):
self.client = LocalizingClient()
- @patch('landing.views.basket.subscribe')
- def test_apps_subscription(self, subscribe):
- subscribe.return_value = True
+ def _good_newsletter_post(self):
url = reverse('landing.views.apps_newsletter')
r = self.client.post(url,
@@ -108,6 +110,13 @@ def test_apps_subscription(self, subscribe):
'agree': 'checked'},
follow=True)
eq_(200, r.status_code)
+
+ return r
+
+ @patch('landing.views.basket.subscribe')
+ def test_apps_subscription(self, subscribe):
+ subscribe.return_value = {'status': 'success'}
+ r = self._good_newsletter_post()
# assert thank you message
self.assertContains(r, 'Thank you')
eq_(1, subscribe.call_count)
@@ -122,3 +131,10 @@ def test_apps_subscription_bad_values(self, subscribe):
self.assertContains(r, 'Enter a valid e-mail address.')
self.assertContains(r, 'Select a valid choice.')
self.assertContains(r, 'You must agree to the privacy policy.')
+
+ @patch('landing.views.basket.subscribe')
+ def test_apps_subscription_exception_retry(self, subscribe):
+ subscribe.side_effect = basket.base.BasketException("500!")
+ subscribe.return_value = True
+ self._good_newsletter_post()
+ eq_(constance.config.BASKET_RETRIES, subscribe.call_count)
View
28 apps/landing/views.py
@@ -1,8 +1,14 @@
+import time
+
+import basket
+from basket.base import BasketException
+
from django.conf import settings
+from django.http import HttpResponseServerError
+import constance.config
import jingo
from waffle.decorators import waffle_switch
-import basket
from devmo import (SECTION_USAGE, SECTION_ADDONS, SECTION_APPS, SECTION_MOBILE,
SECTION_WEB, SECTION_MOZILLA)
@@ -77,12 +83,22 @@ def apps_newsletter(request):
optin = 'N'
if request.locale == 'en-US':
optin = 'Y'
- basket.subscribe(email=form.cleaned_data['email'],
- newsletters=settings.BASKET_APPS_NEWSLETTER,
- format=form.cleaned_data['format'],
- lang=request.locale,
- optin=optin)
+ for i in range(constance.config.BASKET_RETRIES):
+ try:
+ result = basket.subscribe(email=form.cleaned_data['email'],
+ newsletters=settings.BASKET_APPS_NEWSLETTER,
+ format=form.cleaned_data['format'],
+ lang=request.locale,
+ optin=optin)
+ if result.get('status') != 'error':
+ break
+ except BasketException:
+ if i == constance.config.BASKET_RETRIES:
+ return HttpResponseServerError()
+ else:
+ time.sleep(constance.config.BASKET_RETRY_WAIT * i)
del context['form']
+
else:
context = {'form': SubscriptionForm()}
View
10 settings.py
@@ -972,13 +972,13 @@ def read_only_mode(env):
"Dev derby tags for previous challenges (space-separated tags)"
),
- DEKIWIKI_POST_RETRIES = (
- 6,
- 'Number of time to retry dekiwiki/MindTouch post before giving up.'
+ BASKET_RETRIES = (
+ 5,
+ 'Number of time to retry basket post before giving up.'
),
- DEKIWIKI_API_RETRY_WAIT = (
+ BASKET_RETRY_WAIT = (
.5,
- 'How long to wait between dekiwiki/Mindtouch api request retries. '
+ 'How long to wait between basket api request retries. '
'We typically multiply this value by the retry number so, e.g., '
'the 4th retry waits 4*.5 = 2 seconds.'
),
Please sign in to comment.
Something went wrong with that request. Please try again.