django / django/new-features

Add metadata API to Tasks

Open
#141 3 comments 11 reactions 0 assignees View on GitHub
Tasks
Dominant language
No language data
Stars
188
Forks
7
PR merge metrics
No merged PRs in 30d

Description

### Code of Conduct

- [x] I agree to follow Django's Code of Conduct

### Feature Description

A `TaskResult` should have `metadata` - a writable persistent `dict` (JSON-compatible) containing additional data about the task, but outside of its arguments.

Metadata should be settable as part of enqueueing, readable and modifiable during configuration, and persist.

### Problem

It's useful for a number of cases for a task result to have additional "metadata" attached to it, for example smuggling a telemetry trace id through to reconstruct trace timelines.

It's unlikely to enable many additional behaviours inside Django, but is mostly useful for the Tasks ecosystem.

### Request or proposal

proposal

### Additional Details

A similar API existed in `django-tasks`, but was removed to match what was upstreamed.

### Implementation Suggestions

The API:

- `my_task.using(metadata={"trace_id": 123}).enqueue(foo=1)` would enqueue the `my_task` task with metadata `{"trace_id": 123}` and arguments `foo=1`.
- Metadata could be retrieved using task context `context.metadata`
- If the `context.metadata` is modified, it is saved when a task finishes (successfully or otherwise)
- Metadata can be saved within a task using `context.save_metadata()` (or `.asave_metadata`)
- `metadata` is always a `dict`
- Metadata support is opt-in - backends must set the `supports_metadata` feature and implement `save_metadata` and/or `asave_metadata`
- Metadata keys prefixed with `_django` are reserve for internal implementation (eg for backend users). They may be stored, but are stripped out before being shown by the user, and are ignored during save.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.