Skip to content

Commit

Permalink
fixing issue #106
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Davies committed Mar 26, 2019
1 parent 35ec132 commit 05fbda8
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 12 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ language: python
python:
- "3.6"

before_install:
- pip install poetry

install:
- poetry install

Expand Down
8 changes: 4 additions & 4 deletions carrot/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.conf import settings

from carrot.models import MessageLog
from carrot.objects import VirtualHost, BaseMessageSerializer
from carrot.objects import VirtualHost, BaseMessageSerializer, DefaultMessageSerializer

import json
import traceback
Expand All @@ -32,7 +32,7 @@ class Consumer(threading.Thread):
:class:`.ConsumerSet` object.
"""

serializer: Type[BaseMessageSerializer] = BaseMessageSerializer
serializer: Type[BaseMessageSerializer] = DefaultMessageSerializer
reconnect_timeout: int = 5
task_log: List[str] = []
exchange_arguments: Dict[str, Any] = {}
Expand Down Expand Up @@ -282,7 +282,7 @@ def on_message(self, channel: pika.channel.Channel, method_frame: pika.frame.Met
properties: pika.BasicProperties, body: bytes) -> None:
"""
The process that takes a single message from RabbitMQ, converts it into a python executable and runs it,
logging the output back to the assoicated :class:`carrot.models.MessageLog`
logging the output back to the associated :class:`carrot.models.MessageLog`
"""
self.channel.basic_ack(method_frame.delivery_tag)
Expand All @@ -305,7 +305,7 @@ def on_message(self, channel: pika.channel.Channel, method_frame: pika.frame.Met
self.logger.info('Consuming task %s, ID=%s' % (task_type, properties.message_id))

try:
func = self.serializer.get_task(properties, body)
func = func = self.serializer.get_task(properties, body)
except (ValueError, ImportError, AttributeError) as err:
return self.fail(log, err)

Expand Down
10 changes: 6 additions & 4 deletions carrot/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,17 @@ class BaseMessageSerializer(object):
type_header, message_type = ('',) * 2
task_get_attempts = 20

def get_task(self, properties: pika.BasicProperties, body: bytes) -> Callable:
@classmethod
def get_task(cls, properties: pika.BasicProperties, body: bytes) -> Callable:
"""
Identifies the python function to be executed from the content of the RabbitMQ message. By default, Carrot
returns the value of the self.type_header header in the properties.
Once this string has been found, carrot uses importlib to return a callable python function.
"""
mod = '.'.join(properties.headers[self.type_header].split('.')[:-1])
task = properties.headers[self.type_header].split('.')[-1]
mod = '.'.join(properties.headers[cls.type_header].split('.')[:-1])
task = properties.headers[cls.type_header].split('.')[-1]
module = importlib.import_module(mod)
func = getattr(module, task)
return func
Expand Down Expand Up @@ -183,7 +184,8 @@ def publish(self, connection: pika.BlockingConnection, channel: pika.channel.Cha
channel.basic_publish(**kwargs)
connection.close()

def serialize_arguments(self, body: str) -> Tuple[tuple, dict]:
@classmethod
def serialize_arguments(cls, body: str) -> Tuple[tuple, dict]:
"""
Extracts positional and keyword arguments to be sent to a function from the message body
"""
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.2"
version = "1.4.3a1"
description = "A RabbitMQ asynchronous task queue for Django."
authors = ["Christoper Davies <christopherdavies553@gmail.com>"]
license = "Apache-2.0"
Expand Down
1 change: 1 addition & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@ def main():
args = parser.parse_args()
runner(args)


if __name__ == '__main__':
main()

0 comments on commit 05fbda8

Please sign in to comment.