Linting and hints

This commit is contained in:
Ilan Steemers
2020-06-12 12:33:04 +02:00
parent 2f2269c27f
commit ef4c7876ae
11 changed files with 148 additions and 125 deletions
+21 -18
View File
@@ -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)
+7 -4
View File
@@ -1,5 +1,8 @@
import random
import redis
from redis import Redis
from django_q.brokers import Broker
from django_q.conf import Conf
@@ -25,7 +28,7 @@ class Disque(Broker):
command = "FASTACK" if Conf.DISQUE_FASTACK else "ACKJOB"
return self.connection.execute_command(f"{command} {task_id}")
def ping(self):
def ping(self) -> bool:
return self.connection.execute_command("HELLO")[0] > 0
def delete(self, task_id):
@@ -34,21 +37,21 @@ class Disque(Broker):
def fail(self, task_id):
return self.delete(task_id)
def delete_queue(self):
def delete_queue(self) -> int:
jobs = self.connection.execute_command(f"JSCAN QUEUE {self.list_key}")[1]
if jobs:
job_ids = " ".join(jid.decode() for jid in jobs)
self.connection.execute_command(f"DELJOB {job_ids}")
return len(jobs)
def info(self):
def info(self) -> str:
if not self._info:
info = self.connection.info("server")
self._info = f'Disque {info["disque_version"]}'
return self._info
@staticmethod
def get_connection(list_key=Conf.PREFIX):
def get_connection(list_key: str = Conf.PREFIX) -> Redis:
# randomize nodes
random.shuffle(Conf.DISQUE_NODES)
# find one that works
+12 -11
View File
@@ -1,31 +1,32 @@
from iron_mq import IronMQ, Queue
from requests.exceptions import HTTPError
from django_q.conf import Conf
from django_q.brokers import Broker
from iron_mq import IronMQ
from django_q.conf import Conf
class IronMQBroker(Broker):
def enqueue(self, task):
return self.connection.post(task)['ids'][0]
return self.connection.post(task)["ids"][0]
def dequeue(self):
timeout = Conf.RETRY or None
tasks = self.connection.get(timeout=timeout, wait=1, max=Conf.BULK)['messages']
tasks = self.connection.get(timeout=timeout, wait=1, max=Conf.BULK)["messages"]
if tasks:
return [(t['id'], t['body']) for t in tasks]
return [(t["id"], t["body"]) for t in tasks]
def ping(self):
def ping(self) -> bool:
return self.connection.name == self.list_key
def info(self):
return 'IronMQ'
def info(self) -> str:
return "IronMQ"
def queue_size(self):
return self.connection.size()
def delete_queue(self):
try:
return self.connection.delete_queue()['msg']
return self.connection.delete_queue()["msg"]
except HTTPError:
return False
@@ -34,7 +35,7 @@ class IronMQBroker(Broker):
def delete(self, task_id):
try:
return self.connection.delete(task_id)['msg']
return self.connection.delete(task_id)["msg"]
except HTTPError:
return False
@@ -45,6 +46,6 @@ class IronMQBroker(Broker):
return self.delete(task_id)
@staticmethod
def get_connection(list_key=Conf.PREFIX):
def get_connection(list_key: str = Conf.PREFIX) -> Queue:
ironmq = IronMQ(name=None, **Conf.IRON_MQ)
return ironmq.queue(queue_name=list_key)
+3 -4
View File
@@ -4,7 +4,6 @@ from time import sleep
from bson import ObjectId
from django.utils import timezone
from pymongo import MongoClient
from pymongo.errors import ConfigurationError
from django_q.brokers import Broker
@@ -21,7 +20,7 @@ class Mongo(Broker):
self.collection = self.get_collection()
@staticmethod
def get_connection(list_key=Conf.PREFIX):
def get_connection(list_key: str = Conf.PREFIX) -> MongoClient:
return MongoClient(**Conf.MONGO)
def get_collection(self):
@@ -41,10 +40,10 @@ class Mongo(Broker):
def purge_queue(self):
return self.delete_queue()
def ping(self):
def ping(self) -> bool:
return self.info is not None
def info(self):
def info(self) -> str:
if not self._info:
self._info = f"MongoDB {self.connection.server_info()['version']}"
return self._info
+11 -9
View File
@@ -1,13 +1,13 @@
from datetime import timedelta
from time import sleep
from django.utils import timezone
from django import db
from django.db import transaction
from django.utils import timezone
from django_q.brokers import Broker
from django_q.models import OrmQ
from django_q.conf import Conf, logger
from django_q.models import OrmQ
def _timeout():
@@ -16,8 +16,10 @@ def _timeout():
class ORM(Broker):
@staticmethod
def get_connection(list_key=Conf.PREFIX):
if transaction.get_autocommit(using=Conf.ORM): # Only True when not in an atomic block
def get_connection(list_key: str = Conf.PREFIX):
if transaction.get_autocommit(
using=Conf.ORM
): # Only True when not in an atomic block
# Make sure stale connections in the broker thread are explicitly
# closed before attempting DB access.
# logger.debug("Broker thread calling close_old_connections")
@@ -26,14 +28,14 @@ class ORM(Broker):
logger.debug("Broker in an atomic transaction")
return OrmQ.objects.using(Conf.ORM)
def queue_size(self):
def queue_size(self) -> int:
return (
self.get_connection()
.filter(key=self.list_key, lock__lte=_timeout())
.count()
)
def lock_size(self):
def lock_size(self) -> int:
return (
self.get_connection().filter(key=self.list_key, lock__gt=_timeout()).count()
)
@@ -41,10 +43,10 @@ class ORM(Broker):
def purge_queue(self):
return self.get_connection().filter(key=self.list_key).delete()
def ping(self):
def ping(self) -> bool:
return True
def info(self):
def info(self) -> str:
if not self._info:
self._info = f"ORM {Conf.ORM}"
return self._info
@@ -60,7 +62,7 @@ class ORM(Broker):
def dequeue(self):
tasks = self.get_connection().filter(key=self.list_key, lock__lt=_timeout())[
0: Conf.BULK
0 : Conf.BULK
]
if tasks:
task_list = []
+8 -7
View File
@@ -1,4 +1,5 @@
import redis
from redis import Redis
from django_q.brokers import Broker
from django_q.conf import Conf, logger
@@ -10,7 +11,7 @@ except ImportError:
class Redis(Broker):
def __init__(self, list_key=Conf.PREFIX):
def __init__(self, list_key: str = Conf.PREFIX):
super(Redis, self).__init__(list_key=f"django_q:{list_key}:q")
def enqueue(self, task):
@@ -30,33 +31,33 @@ class Redis(Broker):
def purge_queue(self):
return self.connection.ltrim(self.list_key, 1, 0)
def ping(self):
def ping(self) -> bool:
try:
return self.connection.ping()
except redis.ConnectionError as e:
logger.error("Can not connect to Redis server.")
raise e
def info(self):
def info(self) -> str:
if not self._info:
info = self.connection.info("server")
self._info = f"Redis {info['redis_version']}"
return self._info
def set_stat(self, key, value, timeout):
def set_stat(self, key: str, value: str, timeout: int):
self.connection.set(key, value, timeout)
def get_stat(self, key):
def get_stat(self, key: str):
if self.connection.exists(key):
return self.connection.get(key)
def get_stats(self, pattern):
def get_stats(self, pattern: str):
keys = self.connection.keys(pattern=pattern)
if keys:
return self.connection.mget(keys)
@staticmethod
def get_connection(list_key=Conf.PREFIX):
def get_connection(list_key: str = Conf.PREFIX) -> Redis:
if django_redis and Conf.DJANGO_REDIS:
return django_redis.get_redis_connection(Conf.DJANGO_REDIS)
if isinstance(Conf.REDIS, str):