Skip to content

Commit

Permalink
Merge pull request #482 from ihuk/master
Browse files Browse the repository at this point in the history
Fix for #424
  • Loading branch information
Koed00 committed Oct 20, 2020
2 parents 22b6f8e + 5076367 commit 9219116
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
8 changes: 8 additions & 0 deletions django_q/brokers/__init__.py
Expand Up @@ -13,6 +13,14 @@ def __init__(self, list_key: str = Conf.PREFIX):
self.cache = self.get_cache()
self._info = None

def __getstate__(self):
return self.list_key, self._info

def __setstate__(self, state):
self.list_key, self._info = state
self.connection = self.get_connection(self.list_key)
self.cache = self.get_cache()

def enqueue(self, task):
"""
Puts a task onto the queue
Expand Down
5 changes: 5 additions & 0 deletions django_q/brokers/aws_sqs.py
Expand Up @@ -10,6 +10,11 @@ def __init__(self, list_key: str = Conf.PREFIX):
super(Sqs, self).__init__(list_key)
self.queue = self.get_queue()

def __setstate__(self, state):
super(Sqs, self).__setstate__(state)
self.sqs = None
self.queue = self.get_queue()

def enqueue(self, task):
response = self.queue.send_message(MessageBody=task)
return response.get("MessageId")
Expand Down
4 changes: 4 additions & 0 deletions django_q/brokers/mongo.py
Expand Up @@ -19,6 +19,10 @@ def __init__(self, list_key=Conf.PREFIX):
super(Mongo, self).__init__(list_key)
self.collection = self.get_collection()

def __setstate__(self, state):
super(Mongo, self).__setstate__(state)
self.collection = self.get_collection()

@staticmethod
def get_connection(list_key: str = Conf.PREFIX) -> MongoClient:
return MongoClient(**Conf.MONGO)
Expand Down
10 changes: 9 additions & 1 deletion django_q/cluster.py
Expand Up @@ -12,7 +12,15 @@
import arrow

# Django
from django import db
from django import db, core
from django.apps.registry import apps

try:
apps.check_apps_ready()
except core.exceptions.AppRegistryNotReady:
import django
django.setup()

from django.conf import settings
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
Expand Down
8 changes: 7 additions & 1 deletion django_q/queues.py
Expand Up @@ -54,9 +54,15 @@ def __init__(self, *args, **kwargs):
super(Queue, self).__init__(
*args, ctx=multiprocessing.get_context(), **kwargs
)

self.size = SharedCounter(0)

def __getstate__(self):
return super(Queue, self).__getstate__() + (self.size, )

def __setstate__(self, state):
super(Queue, self).__setstate__(state[:-1])
self.size = state[-1]

def put(self, *args, **kwargs):
super(Queue, self).put(*args, **kwargs)
self.size.increment(1)
Expand Down

0 comments on commit 9219116

Please sign in to comment.