Skip to content

Commit

Permalink
Merge pull request #478 from fallenhitokiri/sqs-get-queue-before-create
Browse files Browse the repository at this point in the history
try to get SQS queue before creating it
  • Loading branch information
Koed00 authored Oct 20, 2020
2 parents 8739050 + 9be4da7 commit 6184a1c
Showing 1 changed file with 13 additions and 0 deletions.
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 @@ -67,4 +71,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)

0 comments on commit 6184a1c

Please sign in to comment.