如何使用 AWS Elastic Beanstalk 可扩展的 Django 应用程序运行 celery worker? [英] How to run a celery worker with Django app scalable by AWS Elastic Beanstalk?

查看:25
本文介绍了如何使用 AWS Elastic Beanstalk 可扩展的 Django 应用程序运行 celery worker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 Django 与 AWS Elastic Beanstalk 一起使用,同时也只能在主节点上通过 celery 运行任务?

How to use Django with AWS Elastic Beanstalk that would also run tasks by celery on main node only?

推荐答案

这是我在弹性 beantalk 上使用 django 设置 celery 的方式,可扩展性工作正常.

This is how I set up celery with django on elastic beanstalk with scalability working fine.

请记住,container_commands'leader_only'选项仅适用于环境重建部署强>的应用程序.如果服务运行时间足够长,Elastic Beanstalk 可能会删除领导节点. 为了解决这个问题,您可能需要为领导节点应用实例保护.检查:http://docs.aws.amazon.com/autoscaling/latest/userguide/as-instance-termination.html#instance-protection-instance

Please keep in mind that 'leader_only' option for container_commands works only on environment rebuild or deployment of the App. If service works long enough, leader node may be removed by Elastic Beanstalk. To deal with that, you may have to apply instance protection for your leader node. Check: http://docs.aws.amazon.com/autoscaling/latest/userguide/as-instance-termination.html#instance-protection-instance

为celery worker添加bash脚本和beat配置.

Add bash script for celery worker and beat configuration.

添加文件root_folder/.ebextensions/files/celery_configuration.txt:

#!/usr/bin/env bash

# Get django environment variables
celeryenv=`cat /opt/python/current/env | tr '
' ',' | sed 's/export //g' | sed 's/$PATH/%(ENV_PATH)s/g' | sed 's/$PYTHONPATH//g' | sed 's/$LD_LIBRARY_PATH//g' | sed 's/%/%%/g'`
celeryenv=${celeryenv%?}

# Create celery configuraiton script
celeryconf="[program:celeryd-worker]
; Set full path to celery program if using virtualenv
command=/opt/python/run/venv/bin/celery worker -A django_app --loglevel=INFO

directory=/opt/python/current/app
user=nobody
numprocs=1
stdout_logfile=/var/log/celery-worker.log
stderr_logfile=/var/log/celery-worker.log
autostart=true
autorestart=true
startsecs=10

; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 600

; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true

; if rabbitmq is supervised, set its priority higher
; so it starts first
priority=998

environment=$celeryenv

[program:celeryd-beat]
; Set full path to celery program if using virtualenv
command=/opt/python/run/venv/bin/celery beat -A django_app --loglevel=INFO --workdir=/tmp -S django --pidfile /tmp/celerybeat.pid

directory=/opt/python/current/app
user=nobody
numprocs=1
stdout_logfile=/var/log/celery-beat.log
stderr_logfile=/var/log/celery-beat.log
autostart=true
autorestart=true
startsecs=10

; Need to wait for currently executing tasks to finish at shutdown.
; Increase this if you have very long running tasks.
stopwaitsecs = 600

; When resorting to send SIGKILL to the program to terminate it
; send SIGKILL to its whole process group instead,
; taking care of its children as well.
killasgroup=true

; if rabbitmq is supervised, set its priority higher
; so it starts first
priority=998

environment=$celeryenv"

# Create the celery supervisord conf script
echo "$celeryconf" | tee /opt/python/etc/celery.conf

# Add configuration script to supervisord conf (if not there already)
if ! grep -Fxq "[include]" /opt/python/etc/supervisord.conf
  then
  echo "[include]" | tee -a /opt/python/etc/supervisord.conf
  echo "files: celery.conf" | tee -a /opt/python/etc/supervisord.conf
fi

# Reread the supervisord config
supervisorctl -c /opt/python/etc/supervisord.conf reread

# Update supervisord in cache without restarting all services
supervisorctl -c /opt/python/etc/supervisord.conf update

# Start/Restart celeryd through supervisord
supervisorctl -c /opt/python/etc/supervisord.conf restart celeryd-beat
supervisorctl -c /opt/python/etc/supervisord.conf restart celeryd-worker

在部署期间注意脚本执行,但仅限于主节点(leader_only: true).添加文件root_folder/.ebextensions/02-python.config:

Take care about script execution during deployment, but only on main node (leader_only: true). Add file root_folder/.ebextensions/02-python.config:

container_commands:
  04_celery_tasks:
    command: "cat .ebextensions/files/celery_configuration.txt > /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh && chmod 744 /opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh"
    leader_only: true
  05_celery_tasks_run:
    command: "/opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh"
    leader_only: true

  • Beat 无需重新部署即可配置,具有单独的 django 应用程序:https://pypi.python.org/pypi/django_celery_beat.
  • 存储任务结果是个好主意:https://pypi.python.org/pypi/django_celery_beat
  • 文件requirements.txt

    celery==4.0.0
    django_celery_beat==1.0.1
    django_celery_results==1.0.1
    pycurl==7.43.0 --global-option="--with-nss"
    

    为 Amazon SQS 代理配置 celery(从列表中获取所需的端点:http://docs.aws.amazon.com/general/latest/gr/rande.html)root_folder/django_app/settings.py:

    Configure celery for Amazon SQS broker (Get your desired endpoint from list: http://docs.aws.amazon.com/general/latest/gr/rande.html) root_folder/django_app/settings.py:

    ...
    CELERY_RESULT_BACKEND = 'django-db'
    CELERY_BROKER_URL = 'sqs://%s:%s@' % (aws_access_key_id, aws_secret_access_key)
    # Due to error on lib region N Virginia is used temporarily. please set it on Ireland "eu-west-1" after fix.
    CELERY_BROKER_TRANSPORT_OPTIONS = {
        "region": "eu-west-1",
        'queue_name_prefix': 'django_app-%s-' % os.environ.get('APP_ENV', 'dev'),
        'visibility_timeout': 360,
        'polling_interval': 1
    }
    ...
    

    django django_app 应用程序的 Celery 配置

    Celery configuration for django django_app app

    添加文件root_folder/django_app/celery.py:

    from __future__ import absolute_import, unicode_literals
    import os
    from celery import Celery
    
    # set the default Django settings module for the 'celery' program.
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'django_app.settings')
    
    app = Celery('django_app')
    
    # Using a string here means the worker don't have to serialize
    # the configuration object to child processes.
    # - namespace='CELERY' means all celery-related configuration keys
    #   should have a `CELERY_` prefix.
    app.config_from_object('django.conf:settings', namespace='CELERY')
    
    # Load task modules from all registered Django app configs.
    app.autodiscover_tasks()
    

    修改文件root_folder/django_app/__init__.py:

    from __future__ import absolute_import, unicode_literals
    
    # This will make sure the app is always imported when
    # Django starts so that shared_task will use this app.
    from django_app.celery import app as celery_app
    
    __all__ = ['celery_app']
    

    还要检查:

    • How do you run a worker with AWS Elastic Beanstalk? (solution without scalability)
    • Pip Requirements.txt --global-option causing installation errors with other packages. "option not recognized" (solution for problems coming from obsolate pip on elastic beanstalk that cannto deal with global options for properly solving pycurl dependency)

    这篇关于如何使用 AWS Elastic Beanstalk 可扩展的 Django 应用程序运行 celery worker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆