Skip to content

Commit

Permalink
Merge pull request #9 from Koed00/dev
Browse files Browse the repository at this point in the history
Fixes save pruning bug
  • Loading branch information
Koed00 committed Jul 10, 2015
2 parents 46c439f + e590628 commit 423e0d3
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 8 deletions.
4 changes: 3 additions & 1 deletion django_q/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from django.contrib import admin

from django.utils.translation import ugettext_lazy as _

from .tasks import async
Expand All @@ -11,6 +10,7 @@ class TaskAdmin(admin.ModelAdmin):
u'name',
'func',
'started',
'stopped',
'time_taken'
)

Expand Down Expand Up @@ -45,6 +45,7 @@ class FailAdmin(admin.ModelAdmin):
'name',
'func',
'started',
'stopped',
'result'
)

Expand Down Expand Up @@ -74,6 +75,7 @@ class ScheduleAdmin(admin.ModelAdmin):

list_filter = ('next_run', 'schedule_type')
search_fields = ('func',)
list_display_links = ('id', 'func')


admin.site.register(Schedule, ScheduleAdmin)
Expand Down
1 change: 1 addition & 0 deletions django_q/apps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.apps import AppConfig

from .conf import Conf


Expand Down
7 changes: 3 additions & 4 deletions django_q/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
from django_q.monitor import Status, Stat




class Cluster(object):
def __init__(self, list_key=Conf.Q_LIST):
try:
Expand Down Expand Up @@ -389,7 +387,7 @@ def save_task(task):
return
# SAVE LIMIT > 0: Prune database, SAVE_LIMIT 0: No pruning
if task['success'] and 0 < Conf.SAVE_LIMIT < Success.objects.count():
Success.objects.first().delete()
Success.objects.last().delete()

try:
Task.objects.create(id=task['id'],
Expand Down Expand Up @@ -450,7 +448,8 @@ def scheduler(list_key=Conf.Q_LIST):
kwargs['list_key'] = list_key
s.task = tasks.async(s.func, *args, **kwargs)
if not s.task:
logger.error(_('{} failed to create a task from schedule {} [{}]').format(current_process().name, s.id), s.func)
logger.error(_('{} failed to create a task from schedule {} [{}]').format(current_process().name, s.id),
s.func)
else:
logger.info(_('{} created a task from schedule {} [{}]').format(current_process().name, s.id, s.func))
s.save()
2 changes: 2 additions & 0 deletions django_q/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class Conf(object):
STOPPED = _('Stopped')
STOPPING = _('Stopping')


# logger
logger = logging.getLogger('django-q')

Expand All @@ -90,5 +91,6 @@ def get_redis_client():
return django_redis.get_redis_connection(Conf.DJANGO_REDIS)
return redis.StrictRedis(**Conf.REDIS)


# redis client
redis_client = get_redis_client()
3 changes: 3 additions & 0 deletions django_q/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def __unicode__(self):

class Meta:
app_label = 'django_q'
ordering = ['-stopped']


@receiver(pre_save, sender=Task)
Expand Down Expand Up @@ -78,6 +79,7 @@ class Meta:
app_label = 'django_q'
verbose_name = _('Successful task')
verbose_name_plural = _('Successful tasks')
ordering = ['-stopped']
proxy = True


Expand All @@ -94,6 +96,7 @@ class Meta:
app_label = 'django_q'
verbose_name = _('Failed task')
verbose_name_plural = _('Failed tasks')
ordering = ['-stopped']
proxy = True


Expand Down
4 changes: 3 additions & 1 deletion django_q/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import pickle

from django.core import signing

from django_q.conf import Conf

BadSignature = signing.BadSignature


class SignedPackage(object):
"""
Wraps Django's signing module with custom Pickle serializer
Expand Down Expand Up @@ -41,4 +43,4 @@ def dumps(obj):

@staticmethod
def loads(data):
return pickle.loads(data)
return pickle.loads(data)
1 change: 0 additions & 1 deletion django_q/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,3 @@ def _sync(task_id, pack):
result_queue.put('STOP')
cluster.monitor(result_queue)
return task_id

6 changes: 5 additions & 1 deletion docs/admin.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Uses the :class:`Success` proxy model.

.. tip::

The maximum number of successful tasks can be set using the `save_limit` :ref:`configuration` option.
The maximum number of successful tasks can be set using the :ref:`save_limit` option.



Expand Down Expand Up @@ -63,5 +63,9 @@ Success

Indicates the success status of the last scheduled task, if any.

.. note::

if you have set the :ref:`save_limit` configuration option to not save successful tasks to the database, you will only see the failed results of your schedules.


Uses the :class:`Schedule` model
2 changes: 2 additions & 0 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ compress
Compresses task packages to Redis. Useful for large payloads, but can add overhead when used with many small packages.
Defaults to ``False``

.. _save_limit:

save_limit
~~~~~~~~~~

Expand Down

0 comments on commit 423e0d3

Please sign in to comment.