Skip to content

Commit 00a071e

Browse files
committed
wip: add deprecation warning for non-coroutine immediate task dispatches
1 parent 0ecc3a7 commit 00a071e

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

pulpcore/tasking/tasks.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import sys
88
import traceback
99
import tempfile
10+
from asgiref.sync import sync_to_async
1011
from datetime import timedelta
1112
from gettext import gettext as _
1213

@@ -16,7 +17,7 @@
1617
from django_guid import get_guid
1718
from pulpcore.app.apps import MODULE_PLUGIN_VERSIONS
1819
from pulpcore.app.models import Task, TaskGroup
19-
from pulpcore.app.util import current_task, get_domain, get_prn
20+
from pulpcore.app.util import current_task, get_domain, get_prn, deprecation_logger
2021
from pulpcore.constants import (
2122
TASK_FINAL_STATES,
2223
TASK_INCOMPLETE_STATES,
@@ -73,20 +74,29 @@ def _execute_task(task):
7374
is_coroutine = asyncio.iscoroutine(result)
7475

7576
if immediate is True and not is_coroutine:
76-
raise RuntimeError(_("Immediate tasks must be coroutines."))
77+
deprecation_logger.warning(
78+
_(
79+
"Immediate tasks must be coroutine functions."
80+
"Support for non-coroutine immediate tasks will be dropped in pulpcore 3.85."
81+
)
82+
)
83+
result = sync_to_async(func)
84+
is_coroutine = True
7785

7886
if is_coroutine:
7987
_logger.debug(_("Task is coroutine %s"), task.pk)
80-
loop = asyncio.get_event_loop()
8188
if immediate:
82-
try:
83-
loop.run_until_complete(asyncio.wait_for(result, timeout=IMMEDIATE_TIMEOUT))
84-
except asyncio.TimeoutError:
85-
raise RuntimeError(
86-
_("Immediate task timed out after {} seconds").format(IMMEDIATE_TIMEOUT)
87-
)
89+
coro = asyncio.wait_for(result, timeout=IMMEDIATE_TIMEOUT)
8890
else:
89-
loop.run_until_complete(result)
91+
coro = result
92+
93+
loop = asyncio.get_event_loop()
94+
try:
95+
loop.run_until_complete(coro)
96+
except asyncio.TimeoutError:
97+
raise RuntimeError(
98+
_("Immediate task timed out after {} seconds").format(IMMEDIATE_TIMEOUT)
99+
)
90100

91101
except Exception:
92102
exc_type, exc, tb = sys.exc_info()
@@ -225,9 +235,6 @@ def dispatch(
225235
stack.enter_context(task)
226236
else:
227237
notify_workers = True
228-
_logger.info("asdfasdf*")
229-
_logger.info("asdfasdf")
230-
_logger.info("*" * 50)
231238
if immediate:
232239
prior_tasks = Task.objects.filter(
233240
state__in=TASK_INCOMPLETE_STATES, pulp_created__lt=task.pulp_created

0 commit comments

Comments
 (0)