Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added long polling support #506

Merged
merged 4 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions django_q/brokers/aws_sqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django_q.brokers import Broker
from django_q.conf import Conf


QUEUE_DOES_NOT_EXIST = "AWS.SimpleQueueService.NonExistentQueue"


Expand All @@ -27,9 +26,22 @@ def dequeue(self):
# sqs supports max 10 messages in bulk
if Conf.BULK > 10:
Conf.BULK = 10
tasks = self.queue.receive_messages(
MaxNumberOfMessages=Conf.BULK, VisibilityTimeout=Conf.RETRY
)

params = {"MaxNumberOfMessages": Conf.BULK, "VisibilityTimeout": Conf.RETRY}

# sqs long polling
sqs_config = Conf.SQS
if "receive_message_wait_time_seconds" in sqs_config:
wait_time_second = sqs_config.get("receive_message_wait_time_seconds", 20)

# validation of parameter
if not isinstance(wait_time_second, int):
raise ValueError("receive_message_wait_time_seconds should be int")
if wait_time_second > 20:
raise ValueError("receive_message_wait_time_seconds is invalid. Reason: Must be >= 0 and <= 20")
params.update({"WaitTimeSeconds": wait_time_second})

tasks = self.queue.receive_messages(**params)
if tasks:
return [(t.receipt_handle, t.body) for t in tasks]

Expand Down Expand Up @@ -67,6 +79,10 @@ def get_connection(list_key: str = Conf.PREFIX) -> Session:
if "aws_region" in config:
config["region_name"] = config["aws_region"]
del config["aws_region"]

if 'receive_message_wait_time_seconds' in config:
del config["receive_message_wait_time_seconds"]

return Session(**config)

def get_queue(self):
Expand Down
1 change: 1 addition & 0 deletions django_q/tests/test_brokers.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ def canceled_sqs(monkeypatch):
"aws_region": os.getenv("AWS_REGION"),
"aws_access_key_id": os.getenv("AWS_ACCESS_KEY_ID"),
"aws_secret_access_key": os.getenv("AWS_SECRET_ACCESS_KEY"),
"receive_message_wait_time_seconds": 20
},
)
# check broker
Expand Down