add request context to celery

This commit is contained in:
Byron Wang 2025-12-25 17:18:37 +08:00
parent 26d12b34ce
commit ed583f7e4b
No known key found for this signature in database
GPG Key ID: 335E934E215AD579
1 changed files with 4 additions and 0 deletions

View File

@ -47,7 +47,11 @@ def _get_celery_ssl_options() -> dict[str, Any] | None:
def init_app(app: DifyApp) -> Celery:
class FlaskTask(Task):
def __call__(self, *args: object, **kwargs: object) -> object:
from core.logging.context import init_request_context
with app.app_context():
# Initialize logging context for this task (similar to before_request in Flask)
init_request_context()
return self.run(*args, **kwargs)
broker_transport_options = {}