Merge pull request #9 from Koed00/dev

Fixes save pruning bug
This commit is contained in:
Ilan Steemers
2015-07-10 12:19:01 +02:00
9 changed files with 22 additions and 8 deletions
+3 -1
View File
@@ -1,5 +1,4 @@
from django.contrib import admin from django.contrib import admin
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from .tasks import async from .tasks import async
@@ -11,6 +10,7 @@ class TaskAdmin(admin.ModelAdmin):
u'name', u'name',
'func', 'func',
'started', 'started',
'stopped',
'time_taken' 'time_taken'
) )
@@ -45,6 +45,7 @@ class FailAdmin(admin.ModelAdmin):
'name', 'name',
'func', 'func',
'started', 'started',
'stopped',
'result' 'result'
) )
@@ -74,6 +75,7 @@ class ScheduleAdmin(admin.ModelAdmin):
list_filter = ('next_run', 'schedule_type') list_filter = ('next_run', 'schedule_type')
search_fields = ('func',) search_fields = ('func',)
list_display_links = ('id', 'func')
admin.site.register(Schedule, ScheduleAdmin) admin.site.register(Schedule, ScheduleAdmin)
+1
View File
@@ -1,4 +1,5 @@
from django.apps import AppConfig from django.apps import AppConfig
from .conf import Conf from .conf import Conf
+3 -4
View File
@@ -35,8 +35,6 @@ from django_q.models import Task, Success, Schedule
from django_q.monitor import Status, Stat from django_q.monitor import Status, Stat
class Cluster(object): class Cluster(object):
def __init__(self, list_key=Conf.Q_LIST): def __init__(self, list_key=Conf.Q_LIST):
try: try:
@@ -389,7 +387,7 @@ def save_task(task):
return return
# SAVE LIMIT > 0: Prune database, SAVE_LIMIT 0: No pruning # SAVE LIMIT > 0: Prune database, SAVE_LIMIT 0: No pruning
if task['success'] and 0 < Conf.SAVE_LIMIT < Success.objects.count(): if task['success'] and 0 < Conf.SAVE_LIMIT < Success.objects.count():
Success.objects.first().delete() Success.objects.last().delete()
try: try:
Task.objects.create(id=task['id'], Task.objects.create(id=task['id'],
@@ -450,7 +448,8 @@ def scheduler(list_key=Conf.Q_LIST):
kwargs['list_key'] = list_key kwargs['list_key'] = list_key
s.task = tasks.async(s.func, *args, **kwargs) s.task = tasks.async(s.func, *args, **kwargs)
if not s.task: 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: else:
logger.info(_('{} created a task from schedule {} [{}]').format(current_process().name, s.id, s.func)) logger.info(_('{} created a task from schedule {} [{}]').format(current_process().name, s.id, s.func))
s.save() s.save()
+2
View File
@@ -64,6 +64,7 @@ class Conf(object):
STOPPED = _('Stopped') STOPPED = _('Stopped')
STOPPING = _('Stopping') STOPPING = _('Stopping')
# logger # logger
logger = logging.getLogger('django-q') logger = logging.getLogger('django-q')
@@ -90,5 +91,6 @@ def get_redis_client():
return django_redis.get_redis_connection(Conf.DJANGO_REDIS) return django_redis.get_redis_connection(Conf.DJANGO_REDIS)
return redis.StrictRedis(**Conf.REDIS) return redis.StrictRedis(**Conf.REDIS)
# redis client # redis client
redis_client = get_redis_client() redis_client = get_redis_client()
+3
View File
@@ -44,6 +44,7 @@ class Task(models.Model):
class Meta: class Meta:
app_label = 'django_q' app_label = 'django_q'
ordering = ['-stopped']
@receiver(pre_save, sender=Task) @receiver(pre_save, sender=Task)
@@ -78,6 +79,7 @@ class Success(Task):
app_label = 'django_q' app_label = 'django_q'
verbose_name = _('Successful task') verbose_name = _('Successful task')
verbose_name_plural = _('Successful tasks') verbose_name_plural = _('Successful tasks')
ordering = ['-stopped']
proxy = True proxy = True
@@ -94,6 +96,7 @@ class Failure(Task):
app_label = 'django_q' app_label = 'django_q'
verbose_name = _('Failed task') verbose_name = _('Failed task')
verbose_name_plural = _('Failed tasks') verbose_name_plural = _('Failed tasks')
ordering = ['-stopped']
proxy = True proxy = True
+3 -1
View File
@@ -4,10 +4,12 @@ except ImportError:
import pickle import pickle
from django.core import signing from django.core import signing
from django_q.conf import Conf from django_q.conf import Conf
BadSignature = signing.BadSignature BadSignature = signing.BadSignature
class SignedPackage(object): class SignedPackage(object):
""" """
Wraps Django's signing module with custom Pickle serializer Wraps Django's signing module with custom Pickle serializer
@@ -41,4 +43,4 @@ class PickleSerializer(object):
@staticmethod @staticmethod
def loads(data): def loads(data):
return pickle.loads(data) return pickle.loads(data)
-1
View File
@@ -107,4 +107,3 @@ def _sync(task_id, pack):
result_queue.put('STOP') result_queue.put('STOP')
cluster.monitor(result_queue) cluster.monitor(result_queue)
return task_id return task_id
+5 -1
View File
@@ -16,7 +16,7 @@ Uses the :class:`Success` proxy model.
.. tip:: .. 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.
@@ -63,5 +63,9 @@ Success
Indicates the success status of the last scheduled task, if any. 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 Uses the :class:`Schedule` model
+2
View File
@@ -77,6 +77,8 @@ compress
Compresses task packages to Redis. Useful for large payloads, but can add overhead when used with many small packages. Compresses task packages to Redis. Useful for large payloads, but can add overhead when used with many small packages.
Defaults to ``False`` Defaults to ``False``
.. _save_limit:
save_limit save_limit
~~~~~~~~~~ ~~~~~~~~~~