Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #424 #482

Merged
merged 2 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions django_q/brokers/__init__.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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