1
0
mirror of https://github.com/django/django.git synced 2025-06-05 03:29:12 +00:00

Make task ids opaque string

They no longer have to be UUIDs
This commit is contained in:
Jake Howard 2024-11-21 21:12:15 +00:00
parent 1e914655af
commit 47fd467bba
No known key found for this signature in database
GPG Key ID: 57AFB45680EDD477
4 changed files with 21 additions and 7 deletions

View File

@ -1,12 +1,11 @@
from copy import deepcopy from copy import deepcopy
from functools import partial from functools import partial
from uuid import uuid4
from django.db import transaction from django.db import transaction
from django.tasks.exceptions import ResultDoesNotExist from django.tasks.exceptions import ResultDoesNotExist
from django.tasks.signals import task_enqueued from django.tasks.signals import task_enqueued
from django.tasks.task import ResultStatus, TaskResult from django.tasks.task import ResultStatus, TaskResult
from django.tasks.utils import json_normalize from django.tasks.utils import get_random_id, json_normalize
from django.utils import timezone from django.utils import timezone
from .base import BaseTaskBackend from .base import BaseTaskBackend
@ -31,7 +30,7 @@ class DummyBackend(BaseTaskBackend):
result = TaskResult( result = TaskResult(
task=task, task=task,
id=str(uuid4()), id=get_random_id(),
status=ResultStatus.NEW, status=ResultStatus.NEW,
enqueued_at=None, enqueued_at=None,
started_at=None, started_at=None,

View File

@ -1,14 +1,13 @@
import logging import logging
from functools import partial from functools import partial
from inspect import iscoroutinefunction from inspect import iscoroutinefunction
from uuid import uuid4
from asgiref.sync import async_to_sync from asgiref.sync import async_to_sync
from django.db import transaction from django.db import transaction
from django.tasks.signals import task_enqueued, task_finished from django.tasks.signals import task_enqueued, task_finished
from django.tasks.task import ResultStatus, TaskResult from django.tasks.task import ResultStatus, TaskResult
from django.tasks.utils import exception_to_dict, json_normalize from django.tasks.utils import exception_to_dict, get_random_id, json_normalize
from django.utils import timezone from django.utils import timezone
from .base import BaseTaskBackend from .base import BaseTaskBackend
@ -66,7 +65,7 @@ class ImmediateBackend(BaseTaskBackend):
task_result = TaskResult( task_result = TaskResult(
task=task, task=task,
id=str(uuid4()), id=get_random_id(),
status=ResultStatus.NEW, status=ResultStatus.NEW,
enqueued_at=None, enqueued_at=None,
started_at=None, started_at=None,

View File

@ -1,9 +1,11 @@
import inspect import inspect
import json import json
import random
import time import time
from functools import wraps from functools import wraps
from traceback import format_exception from traceback import format_exception
from django.utils.crypto import RANDOM_STRING_CHARS
from django.utils.module_loading import import_string from django.utils.module_loading import import_string
@ -69,3 +71,15 @@ def exception_from_dict(exc_data):
raise TypeError(f"{type(exc_class)} is not an exception") raise TypeError(f"{type(exc_class)} is not an exception")
return exc_class(*exc_data["exc_args"]) return exc_class(*exc_data["exc_args"])
def get_random_id():
"""
Return a random string for use as a task id.
Whilst 64 characters is the max, just use 32 as a sensible middle-ground.
This should be much faster than `get_random_string`, since
it's not cryptographically secure.
"""
return "".join(random.choices(RANDOM_STRING_CHARS, k=32))

View File

@ -156,7 +156,9 @@ Attributes of ``TaskResult`` cannot be modified.
A unique identifier for the result, which can be passed to A unique identifier for the result, which can be passed to
:meth:`Task.get_result`. :meth:`Task.get_result`.
The id must be a UUID4, represented as a dashed string. The format of a task result id will depend on the backend being used.
Some may use numbers, others UUIDs.
Task ids are always strings less than 64 characters.
.. attribute:: TaskResult.status .. attribute:: TaskResult.status