1
0
mirror of https://github.com/django/django.git synced 2025-10-24 14:16:09 +00:00

Fixed #35859 -- Added background Tasks framework interface.

This work implements what was defined in DEP 14
(https://github.com/django/deps/blob/main/accepted/0014-background-workers.rst).

Thanks to Raphael Gaschignard, Eric Holscher, Ran Benita, Sarah Boyce,
Jacob Walls, and Natalia Bidart for the reviews.
This commit is contained in:
Jake Howard
2025-07-17 12:51:09 +01:00
committed by nessita
parent 218f69f05e
commit 4289966d1b
29 changed files with 3094 additions and 0 deletions

88
tests/tasks/tasks.py Normal file
View File

@@ -0,0 +1,88 @@
import time
from django.tasks import TaskContext, task
@task()
def noop_task(*args, **kwargs):
return None
@task
def noop_task_from_bare_decorator(*args, **kwargs):
return None
@task()
async def noop_task_async(*args, **kwargs):
return None
@task()
def calculate_meaning_of_life():
return 42
@task()
def failing_task_value_error():
raise ValueError("This Task failed due to ValueError")
@task()
def failing_task_system_exit():
raise SystemExit("This Task failed due to SystemExit")
@task()
def failing_task_keyboard_interrupt():
raise KeyboardInterrupt("This Task failed due to KeyboardInterrupt")
@task()
def complex_exception():
raise ValueError(ValueError("This task failed"))
@task()
def complex_return_value():
# Return something which isn't JSON serializable nor picklable.
return lambda: True
@task()
def exit_task():
exit(1)
@task(enqueue_on_commit=True)
def enqueue_on_commit_task():
pass
@task(enqueue_on_commit=False)
def never_enqueue_on_commit_task():
pass
@task()
def hang():
"""
Do nothing for 5 minutes
"""
time.sleep(300)
@task()
def sleep_for(seconds):
time.sleep(seconds)
@task(takes_context=True)
def get_task_id(context):
return context.task_result.id
@task(takes_context=True)
def test_context(context, attempt):
assert isinstance(context, TaskContext)
assert context.attempt == attempt