Skip to content
This repository has been archived by the owner on May 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from advisory/unschedule-when-delete
Browse files Browse the repository at this point in the history
delete takes care of unscheduling
  • Loading branch information
bafio authored Jan 3, 2018
2 parents 8becf97 + 7eb0413 commit 383b510
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion scheduler/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '1.1.1'
__version__ = '1.1.2'

default_app_config = 'scheduler.apps.SchedulerConfig'
25 changes: 17 additions & 8 deletions scheduler/admin.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
from __future__ import unicode_literals

from django.conf import settings
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from scheduler.models import RepeatableJob, ScheduledJob


QUEUES = [(key, key) for key in settings.RQ_QUEUES.keys()]
from scheduler.forms import JobAdminForm


class QueueMixin(object):
form = JobAdminForm
actions = ['delete_model']

def get_actions(self, request):
actions = super(QueueMixin, self).get_actions(request)
del actions['delete_selected']
return actions

def get_form(self, request, obj=None, **kwargs):
queue_field = self.model._meta.get_field('queue')
queue_field.choices = QUEUES
return super(QueueMixin, self).get_form(request, obj, **kwargs)
def delete_model(self, request, obj):
if hasattr(obj, 'all'):
for o in obj.all():
o.delete()
else:
obj.delete()
delete_model.short_description = _("Delete selected %(verbose_name_plural)s")


@admin.register(ScheduledJob)
Expand All @@ -38,6 +45,7 @@ class ScheduledJobAdmin(QueueMixin, admin.ModelAdmin):
'scheduled_time',
'timeout',
),
'description': _('Please be aware: Scheduled Time has to be in the future.'),
}),
)

Expand Down Expand Up @@ -65,5 +73,6 @@ class RepeatableJobAdmin(QueueMixin, admin.ModelAdmin):
'repeat',
'timeout',
),
'description': _('Please be aware: Scheduled Time has to be in the future.'),
}),
)
11 changes: 11 additions & 0 deletions scheduler/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.forms import ModelForm, ValidationError
from django.utils.timezone import now


class JobAdminForm(ModelForm):

def clean_scheduled_time(self):
data = self.cleaned_data['scheduled_time']
if data < now():
raise ValidationError("Scheduled time has to be in the future")
return data
9 changes: 8 additions & 1 deletion scheduler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
from model_utils.models import TimeStampedModel


QUEUES = [(key, key) for key in settings.RQ_QUEUES.keys()]


@python_2_unicode_compatible
class BaseJob(TimeStampedModel):

name = models.CharField(_('name'), max_length=128, unique=True)
callable = models.CharField(_('callable'), max_length=2048)
enabled = models.BooleanField(_('enabled'), default=True)
queue = models.CharField(_('queue'), max_length=16)
queue = models.CharField(_('queue'), max_length=16, choices=QUEUES)
job_id = models.CharField(
_('job id'), max_length=128, editable=False, blank=True, null=True)
scheduled_time = models.DateTimeField(_('scheduled time'))
Expand Down Expand Up @@ -77,6 +80,10 @@ def save(self, **kwargs):
self.schedule()
super(BaseJob, self).save(**kwargs)

def delete(self, **kwargs):
self.unschedule()
super(BaseJob, self).delete(**kwargs)

def scheduler(self):
return django_rq.get_scheduler(self.queue)

Expand Down
21 changes: 21 additions & 0 deletions scheduler/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,27 @@ def test_save_disabled(self):
job.save()
self.assertIsNone(job.job_id)

def test_save_and_schedule(self):
job = self.JobClassFactory()
job.id = 1
job.save()
is_scheduled = job.is_scheduled()
self.assertIsNotNone(job.job_id)
self.assertTrue(is_scheduled)

def test_delete_and_unschedule(self):
job_id = 1
job = self.JobClassFactory()
job.id = job_id
job.save()
is_scheduled = job.is_scheduled()
self.assertIsNotNone(job.job_id)
self.assertTrue(is_scheduled)
scheduler = job.scheduler()
job.delete()
is_scheduled = job_id in scheduler
self.assertFalse(is_scheduled)


class TestRepeatableJob(TestScheduledJob):

Expand Down

0 comments on commit 383b510

Please sign in to comment.