none
Connect Flask Celery App to Azure cache for redis RRS feed

  • Question

  • I got a flask app that's using celery to run async tasks and would like to use Azure cache for redis as the broker. The problem is that the web app is not able to connect to Azure redis via webjob. I would get the following error: 

    redis.exceptions.ConnectionError: Error 10060 connecting to myAzureRedis.redis.cache.windows.net:6380. A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

    This is how I'm connecting to redis in the flask app:

    def make_celery(app):
        celery = Celery(
            app.import_name,
            backend=app.config['CELERY_RESULT_BACKEND'],
            broker=app.config['CELERY_BROKER_URL']
        )
        celery.conf.update(app.config)

        class ContextTask(celery.Task):
            def __call__(self, *args, **kwargs):
                with app.app_context():
                    return self.run(*args, **kwargs)

        celery.Task = ContextTask
        return celery

    app.config.update(
        CELERY_BROKER_URL='redis://:{password}@{redisname}.redis.cache.windows.net:6380',
        CELERY_RESULT_BACKEND='redis://:{password}@{redisname}.redis.cache.windows.net:6380','
    )
    celery = make_celery(app)

    I also saw people setting this up in virtual network, is that necessary for this to work?

    Saturday, August 10, 2019 12:11 AM

All replies

  • You can set up Azure cache for Redis in a VNET if you do not want it to be exposed to the public internet.

    Coming to your issue.

    Is your flask app deployed to a Web App on Azure? If so, is it on a Virtual Network? 
    Could you please enable the non ssl port 6389 and try connecting via that port just to rule out any firewall issues.

    Please follow the Troubleshooting steps on the "Diagnose and solve problems" blade

    Please let us know if you still face issues. We will work with you to troubleshoot further.

    Monday, August 12, 2019 3:28 AM
    Moderator
  • Thanks for the answer but it turned out that the company firewall was blocking the connection so it is now connecting now. However, the app is still not functioning properly. It seems like that web.config might not be properly set up. Below is the error and my web.config. I had to install python 3.6.4 extension because tensorflow was not available in python 3.6.6. 

    HTTP Error 500.0 - INTERNAL SERVER ERROR
    The page cannot be displayed because an internal server error has occurred.

    Most likely causes:
    IIS received the request; however, an internal error occurred during the processing of the request. The root cause of this error depends on which module handles the request and what was happening in the worker process when this error occurred.
    IIS was not able to access the web.config file for the Web site or application. This can occur if the NTFS permissions are set incorrectly.
    IIS was not able to process configuration for the Web site or application.
    The authenticated user does not have permission to use this DLL.
    The request is mapped to a managed handler but the .NET Extensibility Feature is not installed.

    Things you can try:
    Ensure that the NTFS permissions for the web.config file are correct and allow access to the Web server's machine account.
    Check the event logs to see if any additional information was logged.
    Verify the permissions for the DLL.
    Install the .NET Extensibility feature if the request is mapped to a managed handler.
    Create a tracing rule to track failed requests for this HTTP status code. For more information about creating a tracing rule for failed requests, click here.

    Detailed Error Information:
    Module    FastCgiModule
    Notification    ExecuteRequestHandler
    Handler    PythonHandler
    Error Code    0x00000000

    Web.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <appSettings>
        <add key="WSGI_HANDLER" value="app.app"/>
        <add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
        <add key="WSGI_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
      </appSettings>
      <system.webServer>
        <handlers>
          <add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:\home\Python364x64\python.exe|D:\home\Python364x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
        </handlers>
      </system.webServer>
    </configuration>


    Monday, August 12, 2019 11:47 PM
  • Thank you for following up with the resolution and the additional steps you needed to follow to get this working. If I read this correctly, you have resolved the issues you were facing?

    Regards,

    Mike

    Friday, August 16, 2019 12:54 AM
    Moderator