-
Notifications
You must be signed in to change notification settings - Fork 618
UN-3266 [FEAT] Async Executor Backend for Prompt Studio #1849
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
base: main
Are you sure you want to change the base?
Changes from all commits
2da4907
41eeef8
f66dfb2
95c6592
d8cc6cc
44a2b3f
2f4f2dc
d041201
0a0cfb1
a4e1fd7
ae77d6a
5c22956
3cc3213
d0532f8
6173df5
bbe6f58
a3dc912
98c8071
21157ac
0216b59
db81b9d
e1da202
d119797
fbadbf8
882296e
6d3bbbf
292460b
f35c0e6
9bcb458
0cbd10a
2b1ab1e
4122f08
1ceb352
d69304d
7c1266b
0b84d9e
5b0629d
98ee4b9
2dffcef
3b35fb2
1ab6031
15c3daf
7ae1a74
fbf9c29
ec2f762
d6a3c5e
5c23ab0
525024f
a8cbce1
549f17a
f9b86a9
5369e5a
b5205ff
9659661
67eef62
3f4cc7d
a563a35
9b422da
6a6e8e9
817fc1c
d9bc50f
b715f64
e9c23b2
f59755a
4bf9736
0531870
a2edb23
3f86131
45e61c4
6391c6c
0af0484
807e405
9bdb3f5
18eafe9
7a01a35
3e5ce31
e3ca0c6
db3d8c2
a62a9fd
b3a90af
4200ac1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """Lightweight Celery app for dispatching tasks to worker-v2 workers. | ||
|
|
||
| The Django backend already has a Celery app for internal tasks (beat, | ||
| periodic tasks, etc.) whose broker URL is set via CELERY_BROKER_URL. | ||
| Workers use the same broker. This module provides a second Celery app | ||
| instance that reuses the same broker URL (from Django settings) but | ||
| bypasses Celery's env-var-takes-priority behaviour so it can coexist | ||
| with the main Django Celery app in the same process. | ||
|
|
||
| Problem: Celery reads the ``CELERY_BROKER_URL`` environment variable | ||
| with highest priority — overriding constructor args, ``conf.update()``, | ||
| and ``config_from_object()``. | ||
|
|
||
| Solution: Subclass Celery and override ``connection_for_write`` / | ||
| ``connection_for_read`` so they always use our explicit broker URL, | ||
| bypassing the config resolution chain entirely. | ||
| """ | ||
|
|
||
| import logging | ||
| from urllib.parse import quote_plus | ||
|
|
||
| from celery import Celery | ||
| from django.conf import settings | ||
| from kombu import Queue | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| _worker_app: Celery | None = None | ||
|
|
||
|
|
||
| class _WorkerDispatchCelery(Celery): | ||
| """Celery subclass that forces an explicit broker URL. | ||
|
|
||
| Works around Celery's env-var-takes-priority behaviour where | ||
| ``CELERY_BROKER_URL`` always overrides per-app configuration. | ||
| The connection methods are the actual points where Celery opens | ||
| AMQP/Redis connections, so overriding them is both sufficient | ||
| and safe. | ||
| """ | ||
|
|
||
| _explicit_broker: str | None = None | ||
|
|
||
| def connection_for_write(self, url=None, *args, **kwargs): | ||
| return super().connection_for_write(url or self._explicit_broker, *args, **kwargs) | ||
|
|
||
| def connection_for_read(self, url=None, *args, **kwargs): | ||
| return super().connection_for_read(url or self._explicit_broker, *args, **kwargs) | ||
|
|
||
|
|
||
| def get_worker_celery_app() -> Celery: | ||
| """Get or create a Celery app for dispatching to worker-v2 workers. | ||
|
|
||
| The app uses: | ||
| - Same broker as the workers (built from CELERY_BROKER_BASE_URL, | ||
| CELERY_BROKER_USER, CELERY_BROKER_PASS via Django settings) | ||
| - Same PostgreSQL result backend as the Django Celery app | ||
|
|
||
| Returns: | ||
| Celery app configured for worker-v2 dispatch. | ||
| """ | ||
| global _worker_app | ||
| if _worker_app is not None: | ||
| return _worker_app | ||
|
|
||
| # Reuse the broker URL already built by Django settings (base.py) | ||
| # from CELERY_BROKER_BASE_URL + CELERY_BROKER_USER + CELERY_BROKER_PASS | ||
| broker_url = settings.CELERY_BROKER_URL | ||
|
|
||
| # Reuse the same PostgreSQL result backend as Django's Celery app | ||
| result_backend = ( | ||
| f"db+postgresql://{settings.DB_USER}:" | ||
| f"{quote_plus(settings.DB_PASSWORD)}" | ||
| f"@{settings.DB_HOST}:{settings.DB_PORT}/" | ||
| f"{settings.CELERY_BACKEND_DB_NAME}" | ||
| ) | ||
|
|
||
| app = _WorkerDispatchCelery( | ||
| "worker-dispatch", | ||
| set_as_current=False, | ||
| fixups=[], | ||
| ) | ||
| # Store the explicit broker URL for use in connection overrides | ||
| app._explicit_broker = broker_url | ||
|
|
||
| app.conf.update( | ||
| result_backend=result_backend, | ||
| task_queues=[Queue("executor")], | ||
| task_serializer="json", | ||
| accept_content=["json"], | ||
| result_serializer="json", | ||
| result_extended=True, | ||
| ) | ||
|
Comment on lines
+85
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Configured queue name
The queue declared on the app ( Either align the queue name to Prompt To Fix With AIThis is a comment left during a code review.
Path: backend/backend/worker_celery.py
Line: 85-92
Comment:
**Configured queue name `"executor"` doesn't match the actual dispatch queue**
`get_worker_celery_app()` registers `task_queues=[Queue("executor")]`, but `ExecutionDispatcher._get_queue()` (in `sdk1/execution/dispatcher.py`) constructs the actual queue name as `celery_executor_{executor_name}` — for the legacy executor this becomes `"celery_executor_legacy"`.
The queue declared on the app (`"executor"`) never matches the queue used by `send_task`, so this `task_queues` setting has no practical effect. While `send_task` with an explicit `queue` parameter bypasses queue routing and the task is delivered correctly, the misconfigured `task_queues` setting means any queue-routing policies (e.g. prefetch limits, fair scheduling) configured on `"executor"` will not apply.
Either align the queue name to `"celery_executor_legacy"` (or the appropriate prefix), or remove the stale `task_queues` declaration from this app's config if it is intentionally unused.
How can I resolve this? If you propose a fix, please make it concise. |
||
|
|
||
| _worker_app = app | ||
| # Log broker host only (mask credentials) | ||
| safe_broker = broker_url.split("@")[-1] if "@" in broker_url else broker_url | ||
| safe_backend = ( | ||
| result_backend.split("@")[-1] if "@" in result_backend else result_backend | ||
| ) | ||
| logger.info( | ||
| "Created worker dispatch Celery app (broker=%s, result_backend=%s)", | ||
| safe_broker, | ||
| safe_backend, | ||
| ) | ||
| return _worker_app | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
settings.DB_USERis not URL-encoded in the result backend URLquote_plusis correctly applied toDB_PASSWORD, butDB_USERis interpolated raw. If the database username contains any URL-special characters (e.g.@,:,/), the resulting connection string would be malformed and the Celery result backend would fail to connect. Apply the samequote_plusencoding tosettings.DB_USERfor consistency and correctness, just as is done forsettings.DB_PASSWORD.Prompt To Fix With AI