mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-11 00:20:56 +08:00
Linting and hints
This commit is contained in:
+21
-18
@@ -1,34 +1,37 @@
|
||||
from django_q.conf import Conf
|
||||
from django_q.brokers import Broker
|
||||
from boto3 import Session
|
||||
|
||||
from django_q.brokers import Broker
|
||||
from django_q.conf import Conf
|
||||
|
||||
|
||||
class Sqs(Broker):
|
||||
def __init__(self, list_key=Conf.PREFIX):
|
||||
def __init__(self, list_key: str = Conf.PREFIX):
|
||||
self.sqs = None
|
||||
super(Sqs, self).__init__(list_key)
|
||||
self.queue = self.get_queue()
|
||||
|
||||
def enqueue(self, task):
|
||||
response = self.queue.send_message(MessageBody=task)
|
||||
return response.get('MessageId')
|
||||
return response.get("MessageId")
|
||||
|
||||
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)
|
||||
tasks = self.queue.receive_messages(
|
||||
MaxNumberOfMessages=Conf.BULK, VisibilityTimeout=Conf.RETRY
|
||||
)
|
||||
if tasks:
|
||||
return [(t.receipt_handle, t.body) for t in tasks]
|
||||
|
||||
def acknowledge(self, task_id):
|
||||
return self.delete(task_id)
|
||||
|
||||
def queue_size(self):
|
||||
return int(self.queue.attributes['ApproximateNumberOfMessages'])
|
||||
def queue_size(self) -> int:
|
||||
return int(self.queue.attributes["ApproximateNumberOfMessages"])
|
||||
|
||||
def lock_size(self):
|
||||
return int(self.queue.attributes['ApproximateNumberOfMessagesNotVisible'])
|
||||
def lock_size(self) -> int:
|
||||
return int(self.queue.attributes["ApproximateNumberOfMessagesNotVisible"])
|
||||
|
||||
def delete(self, task_id):
|
||||
message = self.sqs.Message(self.queue.url, task_id)
|
||||
@@ -43,20 +46,20 @@ class Sqs(Broker):
|
||||
def purge_queue(self):
|
||||
self.queue.purge()
|
||||
|
||||
def ping(self):
|
||||
return 'sqs' in self.connection.get_available_resources()
|
||||
def ping(self) -> bool:
|
||||
return "sqs" in self.connection.get_available_resources()
|
||||
|
||||
def info(self):
|
||||
return 'AWS SQS'
|
||||
def info(self) -> str:
|
||||
return "AWS SQS"
|
||||
|
||||
@staticmethod
|
||||
def get_connection(list_key=Conf.PREFIX):
|
||||
def get_connection(list_key: str = Conf.PREFIX) -> Session:
|
||||
config = Conf.SQS
|
||||
if 'aws_region' in config:
|
||||
config['region_name'] = config['aws_region']
|
||||
del(config['aws_region'])
|
||||
if "aws_region" in config:
|
||||
config["region_name"] = config["aws_region"]
|
||||
del config["aws_region"]
|
||||
return Session(**config)
|
||||
|
||||
def get_queue(self):
|
||||
self.sqs = self.connection.resource('sqs')
|
||||
self.sqs = self.connection.resource("sqs")
|
||||
return self.sqs.create_queue(QueueName=self.list_key)
|
||||
|
||||
Reference in New Issue
Block a user