Skip to content
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

Flask in Celery context #56

Open
POD666 opened this issue May 7, 2022 · 0 comments
Open

Flask in Celery context #56

POD666 opened this issue May 7, 2022 · 0 comments

Comments

@POD666
Copy link

POD666 commented May 7, 2022

Hi,
I have tried to use enable_request_id_propagation with celery but during debugging found this:
image

I have a lot of things bound to my flask app (e.g. flask-sqlalchemy) so in celery, I just create an instance of my flask app to interact with DB and much more.

Since both flask and celery contexts are available, MultiContextRequestIdFetcher seems to handle it in the wrong way.
I guess this code should be changed to iterate until a first non-empty value is returned instead of returning on a first non-exception result:

class MultiContextRequestIdFetcher(object):
    ...

    def __call__(self):
        for ctx_fetcher in self.ctx_fetchers:
            try:
                return ctx_fetcher()
            except ExecutedOutsideContext:
                continue
        return None

For now, I fixed it by reversing handlers in current_request_id during celery worker startup:

enable_request_id_propagation(celery)
current_request_id.ctx_fetchers = current_request_id.ctx_fetchers[::-1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant