Skip to content

Commit

Permalink
Fixes localtime for naive setups
Browse files Browse the repository at this point in the history
  • Loading branch information
Koed00 committed May 30, 2021
1 parent bcc6eab commit 2493f4a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
10 changes: 9 additions & 1 deletion django_q/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import socket
import traceback
import uuid
from datetime import datetime
from multiprocessing import Event, Process, Value, current_process
from time import sleep

Expand Down Expand Up @@ -633,7 +634,7 @@ def scheduler(broker: Broker = None):
)
)
next_run = arrow.get(
croniter(s.cron, timezone.localtime()).get_next()
croniter(s.cron, localtime()).get_next()
)
if Conf.CATCH_UP or next_run > arrow.utcnow():
break
Expand Down Expand Up @@ -741,3 +742,10 @@ def rss_check():
elif psutil:
return psutil.Process().memory_info().rss >= Conf.MAX_RSS * 1024
return False


def localtime() -> datetime:
"""" Override for timezone.localtime to deal with naive times and local times"""
if settings.USE_TZ:
return timezone.localtime()
return datetime.now()
18 changes: 14 additions & 4 deletions django_q/tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
from django.db import IntegrityError
from django.test import override_settings
from django.utils import timezone
from django.utils.timezone import is_naive

from django_q.brokers import Broker, get_broker
from django_q.cluster import monitor, pusher, scheduler, worker
from django_q.cluster import monitor, pusher, scheduler, worker, localtime
from django_q.conf import Conf
from django_q.queues import Queue
from django_q.tasks import Schedule, fetch
Expand Down Expand Up @@ -282,7 +283,7 @@ def test_scheduler(broker, monkeypatch):
)
@pytest.mark.django_db
def test_scheduler_atomic_transaction_must_specify_a_database_when_no_replicas_are_used(
orm_no_replica_broker: Broker,
orm_no_replica_broker: Broker,
):
"""
GIVEN a environment without a read replica database
Expand All @@ -301,7 +302,7 @@ def test_scheduler_atomic_transaction_must_specify_a_database_when_no_replicas_a
)
@pytest.mark.django_db
def test_scheduler_atomic_transaction_must_specify_no_database_when_read_write_replicas_are_used(
orm_replica_broker: Broker,
orm_replica_broker: Broker,
):
"""
GIVEN a environment with a read/write configured replica database
Expand All @@ -319,7 +320,7 @@ def test_scheduler_atomic_transaction_must_specify_no_database_when_read_write_r
)
@pytest.mark.django_db
def test_scheduler_atomic_transaction_must_specify_the_database_based_on_router_redirection(
orm_no_replica_broker: Broker,
orm_no_replica_broker: Broker,
):
"""
GIVEN a environment without a read replica database
Expand All @@ -332,3 +333,12 @@ def test_scheduler_atomic_transaction_must_specify_the_database_based_on_router_
# The router should correctly set the database to use!
assert broker.connection.db == "default"
mocked_db.transaction.atomic.assert_called_with(using=broker.connection.db)


def test_localtime():
assert not is_naive(localtime())


@override_settings(USE_TZ=False)
def test_naive_localtime():
assert is_naive(localtime())

0 comments on commit 2493f4a

Please sign in to comment.