Skip to content

Add support for stamps #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
9 changes: 8 additions & 1 deletion django_celery_results/backends/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,14 @@ def _get_meta_from_request(self, request=None):
retrieve and stored on the TaskResult.
"""
request = request or getattr(get_current_task(), "request", None)
return getattr(request, "meta", {})
meta = getattr(request, "meta", {})

if getattr(request, "stamps", None):
if hasattr(request, "stamped_headers"):
meta["stamped_headers"] = request.stamped_headers
meta.update(request.stamps)

return meta

def _store_result(
self,
Expand Down
31 changes: 31 additions & 0 deletions t/unit/backends/test_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,37 @@ def test_backend__task_result_meta_injection(self):
tr = TaskResult.objects.get(task_id=tid2)
assert json.loads(tr.meta) == {'key': 'value', 'children': []}

@pytest.mark.skip("Need to find how to inject stamped headers")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmmm.... not sure if we should merge this with this test only.... should we wait bit more?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree this needs testing - I just couldn't work out how to actually go about setting the stamp on the request

def test_backend__task_result_stamped(self):
self.app.conf.result_serializer = 'json'
self.app.conf.accept_content = {'pickle', 'json'}
self.b = DatabaseBackend(app=self.app)

tid2 = uuid()

request = self._create_request(
task_id=tid2,
name='my_task',
args=[],
kwargs={},
task_protocol=1,
)
result = None

self.b.mark_as_done(tid2, result, request=request)
mindb = self.b.get_task_meta(tid2)

# check task meta
assert mindb.get('result') is None
assert mindb.get('task_name') == 'my_task'

tr = TaskResult.objects.get(task_id=tid2)
assert json.loads(tr.meta) == {
"stamped_headers": ["stamp", "cust_stamp"],
"stamp": "value1", "cust_stamp": "value2",
"children": []
}

def test_backend__task_result_date(self):
tid2 = uuid()

Expand Down