Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions django_celery_results/backends/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
from celery.utils.log import get_logger
from celery.utils.serialization import b64decode, b64encode
from django.db import connection, router, transaction
from django.db.models.functions import Now
from django.db.utils import InterfaceError
from kombu.exceptions import DecodeError

from ..models import ChordCounter
from ..models import GroupResult as GroupResultModel
from ..models import TaskResult
from ..utils import now

EXCEPTIONS_TO_CATCH = (InterfaceError,)

Expand Down Expand Up @@ -146,7 +146,7 @@ def _store_result(
)

if status == states.STARTED:
task_props['date_started'] = Now()
task_props['date_started'] = now()

self.TaskModel._default_manager.store_result(**task_props)
return result
Expand Down
20 changes: 20 additions & 0 deletions t/unit/backends/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,26 @@ def test_custom_state(self):
assert self.b.get_status(tid) == states.SUCCESS
assert self.b.get_result(tid) == 42

def test_date_started_is_timezone_aware(self):
"""Test that date_started is timezone-aware when task is started"""
tid = uuid()

# Create a task in PENDING state
self.b.store_result(tid, None, states.PENDING)
tr = TaskResult.objects.get(task_id=tid)
assert tr.date_started is None

# Mark as started
self.b.mark_as_started(tid)

# Verify date_started is set and timezone-aware
tr = TaskResult.objects.get(task_id=tid)
assert tr.date_started is not None
assert isinstance(tr.date_started, datetime.datetime)
# Check if datetime is timezone-aware (has tzinfo set)
assert tr.date_started.tzinfo is not None
assert tr.date_started.tzinfo.utcoffset(tr.date_started) is not None


class DjangoCeleryResultRouter:
route_app_labels = {"django_celery_results"}
Expand Down