Skip to content

Commit

Permalink
Merge branch 'master' into kwargs+args
Browse files Browse the repository at this point in the history
Merged re-written tests and updates into branch.
  • Loading branch information
tom-price committed Jul 25, 2018
2 parents ca427da + f0d9c0b commit 923ed26
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 220 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Testing also requires:

* factory_boy >= 2.6.1
* psycopg2 >= 2.6.1
* fakeredis >= 0.11.0


## Usage
Expand Down
39 changes: 17 additions & 22 deletions scheduler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class BaseJob(TimeStampedModel):
'0: Result gets deleted immediately. >0: Result expires '
'after n seconds.')
)
repeat = models.PositiveIntegerField(_('repeat'), blank=True, null=True)

def __str__(self):
return self.name
Expand Down Expand Up @@ -152,32 +153,12 @@ def is_schedulable(self):
return False
return self.enabled

def schedule(self):
if self.is_schedulable() is False:
return False
kwargs = self.parse_kwargs()
if self.timeout:
kwargs['timeout'] = self.timeout
if self.result_ttl is not None:
kwargs['result_ttl'] = self.result_ttl
job = self.scheduler().enqueue_at(
self.schedule_time_utc(),
self.callable_func(),
*self.parse_args(),
**kwargs
)
self.job_id = job.id
return True

def unschedule(self):
if self.is_scheduled():
self.scheduler().cancel(self.job_id)
self.job_id = None
return True

def schedule_time_utc(self):
return utc(self.scheduled_time)

def parse_args(self):
args = self.callable_args.values().order_by('id')
return [arg[arg['arg_name']] for arg in args]
Expand Down Expand Up @@ -212,6 +193,22 @@ class Meta:


class ScheduledJob(ScheduledTimeMixin, BaseJob):
repeat = None

def schedule(self):
if self.is_schedulable() is False:
return False
kwargs = {}
if self.timeout:
kwargs['timeout'] = self.timeout
if self.result_ttl is not None:
kwargs['job_result_ttl'] = self.result_ttl
job = self.scheduler().enqueue_at(
self.schedule_time_utc(), self.callable_func(),
**kwargs
)
self.job_id = job.id
return True

class Meta:
verbose_name = _('Scheduled Job')
Expand All @@ -232,7 +229,6 @@ class RepeatableJob(ScheduledTimeMixin, BaseJob):
interval_unit = models.CharField(
_('interval unit'), max_length=12, choices=UNITS, default=UNITS.hours
)
repeat = models.PositiveIntegerField(_('repeat'), blank=True, null=True)

def interval_display(self):
return '{} {}'.format(self.interval, self.get_interval_unit_display())
Expand Down Expand Up @@ -277,7 +273,6 @@ class CronJob(BaseJob):
_('cron string'), max_length=64,
help_text=_('Define the schedule in a crontab like syntax.')
)
repeat = models.PositiveIntegerField(_('repeat'), blank=True, null=True)

def clean(self):
super(CronJob, self).clean()
Expand Down
Loading

0 comments on commit 923ed26

Please sign in to comment.