Skip to content

Commit

Permalink
mypy changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Davies committed Mar 26, 2019
1 parent f12f12a commit cff7718
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 30 deletions.
12 changes: 6 additions & 6 deletions carrot/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ class PublishedMessageLogViewSet(MessageLogViewset):

queryset = MessageLog.objects.filter(status__in=['PUBLISHED', 'IN_PROGRESS'], id__isnull=False)

def purge(self, request: Request, *args, **kwargs) -> MessageLogViewset.list:
def purge(self, request: Request, *args, **kwargs) -> response.Response:
"""
Deletes all items in the pending queue
"""
purge_queue()
return super(PublishedMessageLogViewSet, self).list(request, *args, **kwargs)

def requeue(self, request: Request, *args, **kwargs) -> MessageLogViewset.list:
def requeue(self, request: Request, *args, **kwargs) -> response.Response:
"""
Requeues all pending MessageLogs. Useful when stuff gets stuck due to system update
"""
Expand All @@ -89,7 +89,7 @@ def destroy(self, request: Request, *args, **kwargs) -> response.Response:
self.queryset.delete()
return response.Response(status=204)

def retry(self, request: Request, *args, **kwargs) -> MessageLogViewset.list:
def retry(self, request: Request, *args, **kwargs) -> response.Response:
"""
Retries all `MessageLog` objects in the queryset
"""
Expand Down Expand Up @@ -118,15 +118,15 @@ class MessageLogDetailViewset(MessageLogViewset):
Shows the detail of a single `MessageLog` object
"""
queryset = MessageLog.objects.all()
kwargs = {}
kwargs: dict = {}

def destroy(self, request: Request, *args, **kwargs) -> MessageLogViewset.destroy:
def destroy(self, request: Request, *args, **kwargs) -> response.Response:
"""
Deletes the given `MessageLog` object
"""
return super(MessageLogDetailViewset, self).destroy(request, *args, **kwargs)

def retry(self, request: Request, *args, **kwargs) -> MessageLogViewset.retrieve:
def retry(self, request: Request, *args, **kwargs) -> response.Response:
"""
Requeue a single task
"""
Expand Down
4 changes: 3 additions & 1 deletion carrot/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ def get_task_type(self, properties: pika.spec.BasicProperties, body: bytes) -> s
"""
return properties[self.serializer.type_header]

def __get_message_log(self, properties: pika.spec.BasicProperties, body: bytes) -> MessageLog:
def __get_message_log(self, properties: pika.spec.BasicProperties, body: bytes) -> Optional[MessageLog]:
for i in range(0, self.get_message_attempts):
log = self.get_message_log(properties, body)

if log:
return log
time.sleep(0.1)

return None

def get_message_log(self, properties: pika.spec.BasicProperties, body: bytes) -> Optional[MessageLog]:
"""
Finds a MessageLog based on the content of the RabbitMQ message
Expand Down
10 changes: 6 additions & 4 deletions carrot/management/commands/carrot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
import signal
import psutil
import types
from typing import Optional


class Command(BaseCommand):
"""
The main process for creating and running :class:`carrot.consumer.ConsumerSet` objects and starting thes scheduler
"""
pks = []
pks: list = []
run = True
help = 'Starts the carrot service.'
scheduler = None
active_consumer_sets = []
scheduler: Optional[ScheduledTaskManager] = None
active_consumer_sets: list = []

def __init__(self,
stdout: str = None,
Expand Down Expand Up @@ -196,7 +197,8 @@ def handle(self, **options) -> None:
new_tasks = new_qs.exclude(pk__in=self.pks) or [ScheduledTask()]
for new_task in new_tasks:
print('adding new task %s' % new_task)
self.scheduler.add_task(new_task)
if self.scheduler:
self.scheduler.add_task(new_task)

self.pks = [t.pk for t in new_qs]

Expand Down
39 changes: 24 additions & 15 deletions carrot/management/commands/carrot_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import time
import subprocess
import argparse
from typing import Optional


class PidExists(Exception):
Expand All @@ -19,14 +20,14 @@ class Command(BaseCommand):
"""
The daemon process for controlling the :class:`carrot.management.commands.carrot` service
"""
pid_file = None
options = None
pid_file: Optional[str] = None
options: dict = {}

def delete_pid(self) -> None:
"""
Deletes the pid file, if it exists
"""
if os.path.exists(self.pid_file):
if self.pid_file and os.path.exists(self.pid_file):
os.remove(self.pid_file)

def stop(self, hard_stop: bool = False) -> None:
Expand Down Expand Up @@ -83,34 +84,40 @@ def add_arguments(self, parser: CommandParser) -> None:
'used when running Carrot\'s tests')

@property
def pid(self) -> int:
def pid(self) -> Optional[int]:
"""
Opens and reads the file stored at `self.pidfile`, and returns the content as an integer. If the pidfile doesn't
exist, then None is returned.
"""
try:
with open(self.pid_file, 'r') as pf:
return int(pf.read().strip())
except IOError:
pass
if self.pid_file:
try:
with open(self.pid_file, 'r') as pf:
return int(pf.read().strip())
except IOError:
pass

return None

def write_pid(self, pid: int) -> None:
"""
Writes the pid to the pidfile
"""
with open(self.pid_file, 'w') as f:
f.write(str(pid) + '\n')
if self.pid_file:
with open(self.pid_file, 'w') as f:
f.write(str(pid) + '\n')

def start(self, **options) -> None:
def start(self, **kwargs: dict) -> None:
"""
Starts the carrot service as a subprocess and records the pid
"""
if self.pid:
raise PidExists('Process already running!')

self.options = options
options = ['python3', 'manage.py', 'carrot', '--verbosity', str(options.get('verbosity', 2)),
'--logfile', self.options['logfile'], '--loglevel', self.options['loglevel'],]
if kwargs:
self.options = kwargs

options: list = ['python3', 'manage.py', 'carrot', '--verbosity', str(kwargs.get('verbosity', 2)),
'--logfile', self.options['logfile'], '--loglevel', self.options['loglevel']]

if not self.options['run_scheduler']:
options.append('--no-scheduler')
Expand All @@ -123,6 +130,8 @@ def start(self, **options) -> None:

self.write_pid(proc.pid)

return None

def handle(self, *args, **options) -> None:
"""
The main handler. Initiates :class:`CarrotService`, then handles it based on the options supplied
Expand Down
2 changes: 1 addition & 1 deletion carrot/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Migration(migrations.Migration):

initial = True

dependencies = [
dependencies: list = [
]

operations = [
Expand Down
5 changes: 3 additions & 2 deletions carrot/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@
detail_message_log_viewset, scheduled_task_detail, run_scheduled_task, task_list, validate_args, purge_messages,
MessageLogViewset, requeue_pending
)
from typing import Any

try:
decorators = settings.CARROT.get('monitor_authentication', [])
except AttributeError:
decorators = []


def _(v: MessageList.__class__, **kwargs) -> create_class_view:
def _(v: Any, **kwargs) -> Any:
"""
Decorates a class based view with a custom auth decorator specified in the settings module
"""
return decorate_class_view(v, decorators).as_view(**kwargs)


def _f(v: MessageLogViewset) -> create_class_view:
def _f(v: MessageLogViewset) -> Any:
"""
The same as the above _ method, but for function-based views
"""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-carrot"
version = "1.4.3"
version = "1.4.4a0"
description = "A RabbitMQ asynchronous task queue for Django."
authors = ["Christoper Davies <christopherdavies553@gmail.com>"]
license = "Apache-2.0"
Expand Down

0 comments on commit cff7718

Please sign in to comment.