Skip to content

Task result metadata #171

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
pdrivomHW opened this issue May 28, 2025 · 1 comment
Open

Task result metadata #171

pdrivomHW opened this issue May 28, 2025 · 1 comment
Labels
tasks-api Issues relating to the tasks API and general API contract from the library

Comments

@pdrivomHW
Copy link

Description

It would be very useful for django-tasks to support a built-in task caching mechanism to allow developers to track the progress and store metadata of running tasks. This is particularly helpful for long-running tasks where the client needs to poll for updates or retrieve the result later.

We've implemented a simple version of this using a TaskCache model and utility functions to associate a task with a cache entry, update its progress, and retrieve results.

Example Use Case

# Create a cache entry before enqueuing
cache_id = init_task_cache()
result = some_long_task.enqueue(args, cache_id)
bind_task_cache(cache_id, result.id)

# Inside the task
@task()
def some_long_task(args, cache_id=None):
    set_task_cache(cache_id, "progress", 0)
    # do some work...
    set_task_cache(cache_id, "progress", 50)
    # more work...
    set_task_cache(cache_id, "progress", 100)

Example Implementation

# models.py
class TaskCache(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    task_id = models.UUIDField(blank=True, null=True)
    cache_data = models.JSONField(default=dict)

# utils.py
def init_task_cache():
    task_cache = TaskCache.objects.create(cache_data={})
    return str(task_cache.id)

def bind_task_cache(cache_id, task_id):
    task_cache = TaskCache.objects.get(id=cache_id)
    task_cache.task_id = task_id
    task_cache.save()

def set_task_cache(cache_id, key, value):
    task_cache = TaskCache.objects.get(id=cache_id)
    task_cache.cache_data[key] = value
    task_cache.save()

def get_task_cache_by_task_id(task_id, key):
    task_cache = TaskCache.objects.get(task_id=task_id)
    return task_cache.cache_data.get(key)

Benefits

  • Enables tracking task progress (e.g. for frontend progress bars)
  • Stores metadata or partial results across async boundaries
  • Simplifies debugging and monitoring of background jobs
  • Provides a standardized pattern for cache/task binding

Proposal

Add optional support to django-tasks for task-level caching. This could include:

  • A pluggable base cache model or interface
  • Hooks or decorators to bind tasks to a cache record
  • Option to use Django’s cache framework or a model-based backend
@RealOrangeOne
Copy link
Owner

I don't think this needs to be a cache specifically, but some kind of additional metadata on a task would be useful.

Currently, running function doesn't have access to the task object, which makes it more complex. That might be the first step in resolving this.

@RealOrangeOne RealOrangeOne added the tasks-api Issues relating to the tasks API and general API contract from the library label May 29, 2025
@RealOrangeOne RealOrangeOne changed the title Add built-in task cache support to django-tasks Task result metadata May 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tasks-api Issues relating to the tasks API and general API contract from the library
Projects
None yet
Development

No branches or pull requests

2 participants