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

try to get SQS queue before creating it #478

Merged
merged 1 commit into from
Oct 20, 2020
Merged
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
13 changes: 13 additions & 0 deletions django_q/brokers/aws_sqs.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
from boto3 import Session
from botocore.client import ClientError

from django_q.brokers import Broker
from django_q.conf import Conf


QUEUE_DOES_NOT_EXIST = "AWS.SimpleQueueService.NonExistentQueue"


class Sqs(Broker):
def __init__(self, list_key: str = Conf.PREFIX):
self.sqs = None
Expand Down Expand Up @@ -62,4 +66,13 @@ def get_connection(list_key: str = Conf.PREFIX) -> Session:

def get_queue(self):
self.sqs = self.connection.resource("sqs")

try:
# try to return an existing queue by name. If the queue does not
# exist try to create it.
return self.sqs.get_queue_by_name(QueueName=self.list_key)
except ClientError as exp:
if not exp.response["Error"]["Code"] == QUEUE_DOES_NOT_EXIST:
raise exp

return self.sqs.create_queue(QueueName=self.list_key)