Skip to content

Commit

Permalink
Fixed behavior for USE_TZ=False, updated changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
maciej-jaworski committed Jun 17, 2015
1 parent 1d5278e commit f55d78e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
17 changes: 11 additions & 6 deletions django_cron/__init__.py
Expand Up @@ -5,7 +5,7 @@

from django_cron.models import CronJobLog
from django.conf import settings
from django.utils.timezone import now as utc_now, localtime
from django.utils.timezone import now as utc_now, localtime, is_naive
from django.db.models import Q


Expand All @@ -27,6 +27,11 @@ def get_class(kls):
return m


def get_current_time():
now = utc_now()
return now if is_naive(now) else localtime(now)


class Schedule(object):
def __init__(self, run_every_mins=None, run_at_times=None, retry_after_failure_mins=None):
if run_at_times is None:
Expand Down Expand Up @@ -93,7 +98,7 @@ def should_run_now(self, force=False):
pass
if last_job:
if not last_job.is_success and cron_job.schedule.retry_after_failure_mins:
if localtime(utc_now()) > last_job.start_time + timedelta(minutes=cron_job.schedule.retry_after_failure_mins):
if get_current_time() > last_job.start_time + timedelta(minutes=cron_job.schedule.retry_after_failure_mins):
return True
else:
return False
Expand All @@ -108,15 +113,15 @@ def should_run_now(self, force=False):
pass

if self.previously_ran_successful_cron:
if localtime(utc_now()) > self.previously_ran_successful_cron.start_time + timedelta(minutes=cron_job.schedule.run_every_mins):
if get_current_time() > self.previously_ran_successful_cron.start_time + timedelta(minutes=cron_job.schedule.run_every_mins):
return True
else:
return True

if cron_job.schedule.run_at_times:
for time_data in cron_job.schedule.run_at_times:
user_time = time.strptime(time_data, "%H:%M")
now = localtime(utc_now())
now = get_current_time()
actual_time = time.strptime("%s:%s" % (now.hour, now.minute), "%H:%M")
if actual_time >= user_time:
qset = CronJobLog.objects.filter(
Expand All @@ -141,7 +146,7 @@ def make_log(self, *messages, **kwargs):
cron_log.is_success = kwargs.get('success', True)
cron_log.message = self.make_log_msg(*messages)
cron_log.ran_at_time = getattr(self, 'user_time', None)
cron_log.end_time = localtime(utc_now())
cron_log.end_time = get_current_time()
cron_log.save()

def make_log_msg(self, msg, *other_messages):
Expand All @@ -163,7 +168,7 @@ def make_log_msg(self, msg, *other_messages):
return self.make_log_msg(msg)

def __enter__(self):
self.cron_log = CronJobLog(start_time=localtime(utc_now()))
self.cron_log = CronJobLog(start_time=get_current_time())

return self

Expand Down
4 changes: 0 additions & 4 deletions django_cron/tests.py
Expand Up @@ -88,7 +88,6 @@ def test_runs_every_mins(self):

def test_runs_at_time(self):
logs_count = CronJobLog.objects.all().count()

with freeze_time("2014-01-01 00:00:01"):
call_command('runcrons', self.run_at_times_cron)
self.assertEqual(CronJobLog.objects.all().count(), logs_count + 1)
Expand All @@ -111,9 +110,6 @@ def test_admin(self):
self.client = Client()
self.client.login(username=user.username, password=password)

# get list of CronJobLogs
url = reverse('admin:django_cron_cronjoblog_changelist')

# edit CronJobLog object
call_command('runcrons', self.success_cron, force=True)
log = CronJobLog.objects.all()[0]
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.rst
@@ -1,6 +1,12 @@
Changelog
=========

0.4.2
------

- Fix for #57 (ignoring Django timezone settings)


0.4.1
------

Expand Down

0 comments on commit f55d78e

Please sign in to comment.