lexeme
lexeme

Reputation: 2973

How to wire up Huey to a Flask application

I've read the official docs yet I'm not quite sure I understand how to apply what they tell. Also I've seen this QA, I also use a factory pattern. Just can't see the whole picture.

  1. The connection pool as long as other redis/huey settings may differ depending on the given environment (development, production). How do we wire huey up so we can configure it similar to the Flask application?

  2. As long as I understand to fire a task from a view we need to import tasks moudule and call the specific task (call a function passing the sensitive params). Where shoud we instantiate, keep the huey instance?

  3. Should tasks know about the application dependencies? Should we consider another stripped-down Flask app for this matter?

Can you help a little bit?

Upvotes: 2

Views: 3215

Answers (1)

lexeme
lexeme

Reputation: 2973

Here's how I wired it all up.

First off, here's the contents of my project folder:

enter image description here

  1. Get a stripped-down Flask application to be used by your tasks. As it was suggested in the post I created a secondary application factory:

    # global dependencies
    db = SQLAlchemy()
    
    def create_app_huey(config_name):
    
        app = Flask(__name__)
    
        # apply configuration
        app.config.from_object(config[config_name])
    
        # init extensions
        db.init_app(app)
    
        return app
    
  2. Create tasking package. Two important files here are config.py and tasks.py. This post helped a lot. Let's start with configuration. Note, this is very simple approach.

    # config.py (app.tasking.config)
    
    import os
    from huey import RedisHuey
    
    
    settings__development = {
        'host': 'localhost'
    }
    
    settings__testing = {
        'host': 'localhost'
    }
    
    settings__production = {
        'host': 'production_server'
    }
    
    settings = {
        'development': settings__development,
        'testing': settings__testing,
        'production': settings__production,
        'default': settings__development
    }
    
    huey = RedisHuey(**settings[os.getenv('FLASK_ENV') or 'default'])
    

    Then the tasks.py module will look like this:

    import os
    from app.tasking.config import huey
    from app import create_app_huey
    
    
    app = create_app_huey(config_name=os.getenv('FLASK_ENV') or 'default')
    
    
    @huey.task()
    def create_thumbnails(document):
        pass
    
  3. Run the consumer. Activate your virtual environment. Then run from cmd (I'm on Windows):

    huey_consumer.py app.tasking.config.huey

    Where app.tasking.config is a package.package.module path (in my case!) and huey is the name of available (in the config module) huey instance. Check your huey instance name.

    Reading this helped.

Upvotes: 5

Related Questions