2

I would like to set a route for my Celery tasks and monitor them.


This is the code I have in my flask app running at localhost:5000

background.py

Task:

@celery.task(queue='cache')
def cache_user_tracks_with_features():
    return {'status': 'Task completed!'}

Route:

@task_bp.route('/filter', methods=['GET', 'POST'])
def cache_user_with_features():
    # task
    task = cache_user_tracks_with_features.apply_async()
    while not task.ready():
      sleep(2)

    response_object = {
        'status': 'fail',
        'message': 'User does not exist'
    }
    try:
        user = User.query.filter_by(id=1)).first()
        if not user:
            return jsonify(response_object), 404
        else:
            response_object = {
                'status': 'success',
                'data': {
                    'task_id': task.id,
                    'username': user.username,
                    'email': user.email,
                    'active': user.active
                }
            }
            return jsonify(response_object), 200
    except ValueError:
        return jsonify(response_object), 404

Trigger attempt

I am trying to test it using CURL at terminal, like so:

$ curl -X POST http://localhost:5001/filter -H "Content-Type: application/json" 

But either I get curl: (52) Empty reply from server or else it just hangs. If I remove task from function and curl POST, I get:

{
  "data": {
    "active": true, 
    "email": "me@mac.com", 
    "username": "me"
  }, 
  "status": "success"
}

Docker logs give me:

nginx_1    | 172.21.0.1 - - [03/Apr/2019:22:26:41 +0000] "GET /manifest.json HTTP/1.1" 304 0 "http://localhost/filter" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" "-"

web-db_1   | 2019-04-01 19:52:52.415 UTC [1] LOG:  background worker "logical replication launcher" (PID 25) exited with exit code 1

celery_1   | worker: Warm shutdown (MainProcess)
celery_1   |  
celery_1   |  -------------- celery@fb24d4bd2089 v4.2.1 (windowlicker)
celery_1   | ---- **** ----- 
celery_1   | --- * ***  * -- Linux-4.9.125-linuxkit-x86_64-with 2019-04-06 21:34:38
celery_1   | -- * - **** --- 
celery_1   | - ** ---------- [config]
celery_1   | - ** ---------- .> app:         project:0x7f9923d8a9e8
celery_1   | - ** ---------- .> transport:   redis://redis:6379/0
celery_1   | - ** ---------- .> results:     redis://redis:6379/0
celery_1   | - *** --- * --- .> concurrency: 2 (prefork)
celery_1   | -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
celery_1   | --- ***** ----- 
celery_1   |  -------------- [queues]
celery_1   |                 .> cache            exchange=cache(direct) key=cache
celery_1   |                 
celery_1   | 
celery_1   | [tasks]
celery_1   |   . project.api.routes.background.cache_user_tracks_with_analysis
celery_1   |   . project.api.routes.background.cache_user_tracks_with_features

This is how I configured Celery and Flower (Celery monitoring) in my docker-compose file:

docker-compose-dev.yml

version: '3.6'

services:

  celery:
    image: dev3_web
    restart: always
    volumes:
      - ./services/web:/usr/src/app
      - ./services/web/logs:/usr/src/app/logs

    command: celery worker -A celery_worker.celery --loglevel=INFO --logfile=logs/celery.log -Q cache
    environment:
      - CELERY_BROKER=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      - web
      - redis
    links:
      - redis:redis
      - web-db

  redis:
    image: redis:5.0.3-alpine
    restart: always
    expose:
      - '6379'
    ports:
      - '6379:6379'

  monitor:
    image: dev3_web
    ports:
      - 5555:5555
    command:  flower -A celery_worker.celery --port=5555 --broker=redis://redis:6379/0
    depends_on:
      - web
      - redis

web/logs/celery_log

[2019-04-02 02:51:07,338: INFO/MainProcess] Connected to redis://redis:6379/0
[2019-04-02 02:51:07,375: INFO/MainProcess] mingle: searching for neighbors
[2019-04-02 02:51:08,491: INFO/MainProcess] mingle: all alone
[2019-04-02 02:51:08,582: INFO/MainProcess] celery@59ed7459ac14 ready.
[2019-04-02 02:51:08,661: INFO/MainProcess] Events of group {task} enabled by remote.

Flower shows worker with an active status at dashboard:

enter image description here

Celery Instantiation

# services/web/project/__init__.py

import os
from flask import Flask  
from flask_sqlalchemy import SQLAlchemy
from celery import Celery

# instantiate the db
db = SQLAlchemy()
# background processes instance
celery = Celery(__name__, broker='redis://redis:6379/0') // <------- instant.

def create_app(script_info=None):
    from .api import routes

    # instantiate the app
    app = Flask(__name__)

    # set config
    app_settings = os.getenv('APP_SETTINGS')
    app.config.from_object(app_settings)

    # set up extensions
    db.init_app(app) 
    # register blueprints
    routes.init_app(app)
    #models.init_app(app)
    celery.conf.update(app.config)

    # shell context for flask cli
    @app.shell_context_processor
    def ctx():
        return {'app': app, 'db': db}

    return app

config.py

class DevelopmentConfig(BaseConfig):
    """Development configuration"""
    DEBUG_TB_ENABLED = True 
    DEBUG = True
    BCRYPT_LOG_ROUNDS = 4 
    #set key
    SECRET_KEY = os.environ.get('SECRET_KEY')
    #sqlalchemy
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
    SEVER_NAME = 'http://127.0.0.1:8080'
    # celery broker
    REDIS_HOST = "0.0.0.0"
    REDIS_PORT = 6379
    BROKER_URL = os.environ.get('REDIS_URL', "redis://{host}:{port}/0".format(
                                                                    host=REDIS_HOST, 
                                                                    port=str(REDIS_PORT)))
    INSTALLED_APPS = ['routes']
    # celery config
    CELERYD_CONCURRENCY = 10
    CELERY_BROKER_URL = BROKER_URL
    CELERY_RESULT_BACKEND = 'redis://redis:6379/0'
    CELERY_IMPORTS = ('project.api.routes.background',)

QUESTION

What am I missing? How do I trigger this Celery task and monitor it?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
  • how do you instantiate the celery app object? – 2ps Apr 07 '19 at 01:13
  • please refer to edit, with `celery` instantiation and `config.py`. – 8-Bit Borges Apr 07 '19 at 06:27
  • Your comment pointed me in the right direction and I solved the issue. But I cannot get my rep back for answering my own question. So if you wish the give a similar answer, be my guest, and you'll get the bounty. I'll delete my answer afterwards. – 8-Bit Borges Apr 07 '19 at 07:03

2 Answers2

1

The problem lies at config.py:

REDIS_HOST = "0.0.0.0"
REDIS_PORT = 6379
BROKER_URL = os.environ.get('REDIS_URL', "redis://{host}:{port}/0".format(
                                                                host=REDIS_HOST, 
                                                                port=str(REDIS_PORT)))
INSTALLED_APPS = ['routes']
# celery config
CELERYD_CONCURRENCY = 10
CELERY_BROKER_URL = BROKER_URL #<-------- THIS WAS OVERRIDING

overriding docker-compose environment:

environment:
      - CELERY_BROKER=redis://redis:6379/0  #<------- THIS WAS BEING OVERRIDEN
      - CELERY_RESULT_BACKEND=redis://redis:6379/0

Simply setting CELERY_BROKER_URL to redis://redis:6379/0 in config.py, as well as in docker environment, solved the issue: tasks are now being picked up by worker and the process is being monitored by flower.

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198
0

I do not know exactly what is wrong (it seems OK)... There are several ways (like anything with Celery - there are many ways to achieve something) to achieve what you want to do:

1) To use apply_async() and poll for finished execution. Something like:

res = cache_user_tracks_with_features.apply_async("""parameters here""")
while not res.ready():
  sleep(2)
# business logic

2) Use apply_async() with link to a task to be executed once the job is done.

res = cache_user_tracks_with_features.apply_async(
        """parameters here""", 
        link=task_to_run_when_finished)

Celery also has link_error parameter so you can give it a function to execute if some error happened.

3) Use Celery workflow. Make a chain with cache_user_tracks_with_features and task that does the rest.

Or it could be something completely different that is causing you trouble...

DejanLekic
  • 18,787
  • 4
  • 46
  • 77
  • I tried solution 1), with a simplified dummy task (please refer to edit), and it does not work with `curl` `POST`. It just hangs. If I remove `task = cache_user_tracks_with_features.apply_async()`a nd `'task_id': task.id` from `response_object`, it returns object. so something still wrong with task. – 8-Bit Borges Apr 06 '19 at 19:23
  • 1
    @dejanlekic `.delay` does not poll behind the scenes it is merely a more convenient way to invoke `apply_async` (http://docs.celeryproject.org/en/latest/userguide/calling.html#example). You might be confusing `delay` with `Task.get` – 2ps Apr 07 '19 at 01:10
  • You are absolutely right. I somehow confused it with .get() indeed! Thanks for pointing that out. – DejanLekic Apr 08 '19 at 16:29