mirror of
https://github.com/django-q2/django-q2.git
synced 2026-09-25 22:08:12 +08:00
try to get SQS queue before creating it
In some environments permissions for an IAM role or access key might not include "SQS create". The broker should first try to get the queue and if this fails try to create it as fall back to the current default behaviour.
This commit is contained in:
@@ -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
|
||||
@@ -62,4 +66,13 @@ class Sqs(Broker):
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user