Skip to content

Commit e521d38

Browse files
author
Alexey Tsitkin
committed
adding boto3 sqs retry limit, fixing tests and various python3 issues
1 parent 20f5502 commit e521d38

File tree

9 files changed

+32
-15
lines changed

9 files changed

+32
-15
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ The following settings can be used to fine tune django-eb-sqs. Copy them into yo
133133
- EB_AWS_REGION (`us-east-1`): The AWS region to use when working with SQS.
134134
- EB_SQS_MAX_NUMBER_OF_MESSAGES (`10`): The maximum number of messages to read in a single call from SQS (<= 10).
135135
- EB_SQS_WAIT_TIME_S (`2`): The time to wait (seconds) when receiving messages from SQS.
136-
- EB_SQS_AUTO_ADD_QUEUE (`True`): If queues should be added automatically to AWS if they don't exist.
136+
- EB_SQS_AUTO_ADD_QUEUE (`False`): If queues should be added automatically to AWS if they don't exist.
137137
- EB_SQS_DEAD_LETTER_MODE (`False`): Enable if this worker is handling the SQS dead letter queue. Tasks won't be executed but group callback is.
138138
- EB_SQS_DEFAULT_DELAY (`0`): Default task delay time in seconds.
139139
- EB_SQS_DEFAULT_MAX_RETRIES (`0`): Default retry limit for all tasks.
@@ -147,6 +147,7 @@ The following settings can be used to fine tune django-eb-sqs. Copy them into yo
147147
- EB_SQS_REDIS_EXPIRY (`604800`): Default expiry time in seconds until a group is removed
148148
- EB_SQS_REDIS_KEY_PREFIX (`eb-sqs-`): Prefix used for all Redis keys
149149
- EB_SQS_USE_PICKLE (`False`): Enable to use `pickle` to serialize task parameters. Uses `json` as default.
150+
- EB_SQS_AWS_MAX_RETRIES (`10`): Default retry limit on a boto3 call to AWS SQS.
150151

151152

152153
### Development
@@ -158,5 +159,5 @@ Make sure to install the development dependencies from `development.txt`.
158159
The build in tests can be executed with the Django test runner.
159160

160161
```bash
161-
django-admin test --settings=eb_sqs.test_settings
162+
python -m django test --settings=eb_sqs.test_settings
162163
```

development.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
boto3==1.4.4
1+
boto3==1.4.7
22
Django==1.10.6
33
mock==2.0.0
44
moto==0.4.24

eb_sqs/management/commands/process_queue.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import boto3
44
import logging
55

6+
from botocore.config import Config
67
from django.core.management import BaseCommand, CommandError
78

89
from eb_sqs import settings
@@ -28,7 +29,11 @@ def handle(self, *args, **options):
2829

2930
logger.debug('[django-eb-sqs] Connecting to SQS: {}'.format(', '.join(queue_names)))
3031

31-
sqs = boto3.resource('sqs', region_name=settings.AWS_REGION)
32+
sqs = boto3.resource(
33+
'sqs',
34+
region_name=settings.AWS_REGION,
35+
config=Config(retries={'max_attempts': settings.AWS_MAX_RETRIES})
36+
)
3237
queues = [sqs.get_queue_by_name(QueueName=queue_name) for queue_name in queue_names]
3338

3439
logger.debug('[django-eb-sqs] Connected to SQS: {}'.format(', '.join(queue_names)))

eb_sqs/management/commands/run_eb_sqs_worker.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from __future__ import absolute_import, unicode_literals
22

33
import boto3
4+
from botocore.config import Config
5+
from django.conf import settings
46
from django.core.management import BaseCommand, CommandError
57
import requests
68
from requests.exceptions import ConnectionError
@@ -36,7 +38,11 @@ def handle(self, *args, **options):
3638

3739
try:
3840
self.stdout.write('Connect to SQS')
39-
sqs = boto3.resource("sqs")
41+
sqs = boto3.resource(
42+
'sqs',
43+
region_name=settings.AWS_REGION,
44+
config=Config(retries={'max_attempts': settings.AWS_MAX_RETRIES})
45+
)
4046
queue = sqs.get_queue_by_name(QueueName=queue_name)
4147
self.stdout.write('> Connected')
4248

eb_sqs/settings.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
from django.conf import settings
44

5-
AWS_REGION = getattr(settings, 'EB_AWS_REGION', 'us-east-1') # type: unicode
5+
AWS_REGION = getattr(settings, 'EB_AWS_REGION', 'us-east-1') # type: str
66

77
MAX_NUMBER_OF_MESSAGES = getattr(settings, 'EB_SQS_MAX_NUMBER_OF_MESSAGES', 10) # type: int
88
WAIT_TIME_S = getattr(settings, 'EB_SQS_WAIT_TIME_S', 2) # type: int
99

10-
AUTO_ADD_QUEUE = getattr(settings, 'EB_SQS_AUTO_ADD_QUEUE', True) # type: bool
11-
QUEUE_PREFIX = getattr(settings, 'EB_SQS_QUEUE_PREFIX', 'eb-sqs-') # type: unicode
12-
DEFAULT_QUEUE = getattr(settings, 'EB_SQS_DEFAULT_QUEUE', 'default') # type: unicode
10+
AUTO_ADD_QUEUE = getattr(settings, 'EB_SQS_AUTO_ADD_QUEUE', False) # type: bool
11+
QUEUE_PREFIX = getattr(settings, 'EB_SQS_QUEUE_PREFIX', 'eb-sqs-') # type: str
12+
DEFAULT_QUEUE = getattr(settings, 'EB_SQS_DEFAULT_QUEUE', 'default') # type: str
1313

1414
EXECUTE_INLINE = getattr(settings, 'EB_SQS_EXECUTE_INLINE', False) # type: bool
1515
FORCE_SERIALIZATION = getattr(settings, 'EB_SQS_FORCE_SERIALIZATION', False) # type: bool
@@ -25,8 +25,10 @@
2525
REDIS_CLIENT = getattr(settings, 'EB_SQS_REDIS_CLIENT', None) # type: StrictRedis
2626
# default: 7 days
2727
REDIS_EXPIRY = getattr(settings, 'EB_SQS_REDIS_EXPIRY', 3600 * 24 * 7) # type: int
28-
REDIS_KEY_PREFIX = getattr(settings, 'EB_SQS_REDIS_KEY_PREFIX', 'eb-sqs-') # type: string
28+
REDIS_KEY_PREFIX = getattr(settings, 'EB_SQS_REDIS_KEY_PREFIX', 'eb-sqs-') # type: str
2929

3030
WORKER_FACTORY = getattr(settings, 'EB_SQS_WORKER_FACTORY', None) # type: WorkerFactory
3131

3232
DEAD_LETTER_MODE = getattr(settings, 'EB_SQS_DEAD_LETTER_MODE', False) # type: bool
33+
34+
AWS_MAX_RETRIES = getattr(settings, 'EB_SQS_AWS_MAX_RETRIES', 10) # type: int

eb_sqs/tests/worker/tests_worker_task.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import, unicode_literals
22

3+
import json
34
from unittest import TestCase
45

56
from eb_sqs.worker.worker_task import WorkerTask
@@ -24,7 +25,7 @@ def test_serialize_worker_task(self):
2425
worker_task = WorkerTask('id-1', 'group-5', 'default', dummy_function, [], {}, 5, 0, 'retry-uuid', False)
2526
msg = worker_task.serialize()
2627

27-
self.assertEqual(msg, self.dummy_msg)
28+
self.assertDictEqual(json.loads(msg), json.loads(self.dummy_msg))
2829

2930
def test_deserialize_worker_task(self):
3031
worker_task = WorkerTask.deserialize(self.dummy_msg)

eb_sqs/worker/worker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import logging
55
import uuid
66

7+
from six import string_types
8+
79
from eb_sqs import settings
810
from eb_sqs.worker.group_client import GroupClient
911
from eb_sqs.worker.queue_client import QueueDoesNotExistException, QueueClient, QueueClientException
@@ -163,7 +165,7 @@ def _execute_group_callback(self, worker_task):
163165
if settings.GROUP_CALLBACK_TASK:
164166
callback = settings.GROUP_CALLBACK_TASK
165167

166-
if isinstance(callback, basestring):
168+
if isinstance(callback, string_types):
167169
func_name = callback.split(".")[-1]
168170
func_path = ".".join(callback.split(".")[:-1])
169171
func_module = importlib.import_module(func_path)

eb_sqs/worker/worker_task.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def copy(self, use_serialization):
7575
@staticmethod
7676
def _pickle_args(args):
7777
# type: (dict) -> unicode
78-
return base64.b64encode(pickle.dumps(args, pickle.HIGHEST_PROTOCOL))
78+
return base64.b64encode(pickle.dumps(args, pickle.HIGHEST_PROTOCOL)).decode('utf-8')
7979

8080
@staticmethod
8181
def deserialize(msg):
@@ -105,4 +105,4 @@ def deserialize(msg):
105105
@staticmethod
106106
def _unpickle_args(args):
107107
# type: (unicode) -> dict
108-
return pickle.loads(base64.b64decode(args))
108+
return pickle.loads(base64.b64decode(args.encode('utf-8')))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
long_description=README,
1515
url='https://github.com/cuda-networks/django-eb-sqs',
1616
install_requires=[
17-
'boto3>=1.3.1',
17+
'boto3>=1.4.7',
1818
'Django>=1.7',
1919
'redis>=2.10',
2020
'requests>=2',

0 commit comments

Comments
 (0)