【问题标题】:How do you run a worker with AWS Elastic Beanstalk?您如何使用 AWS Elastic Beanstalk 运行工作程序?
【发布时间】:2013-01-23 13:31:35
【问题描述】:

我正在 AWS Elastic Beanstalk 上启动一个 Django 应用程序。我想运行后台任务或工作人员以运行 celery。

我找不到它是否可能。如果可以,如何实现?

这是我现在正在做的事情,但这每次都会产生事件类型错误。

container_commands:
  01_syncdb:
    command: "django-admin.py syncdb --noinput"
    leader_only: true
  50_sqs_email:
    command: "./manage.py celery worker --loglevel=info"
    leader_only: true

【问题讨论】:

标签: django amazon-web-services celery amazon-elastic-beanstalk


【解决方案1】:

正如@chris-wheadon 在他的评论中建议的那样,您应该尝试在后台运行 celery 作为守护进程。 AWS Elastic Beanstalk 已经使用supervisord 来运行一些守护进程。因此,您可以利用它来运行 celeryd 并避免为此创建自定义 AMI。它对我很有效。

我所做的是在应用程序由 EB 部署到实例后以编程方式将 celeryd 配置文件添加到实例。棘手的部分是该文件需要为守护进程设置所需的环境变量(例如,如果您在应用程序中使用 S3 或其他服务,则为 AWS 访问密钥)。

下面是我使用的脚本的副本,将此脚本添加到配置您的 EB 环境的 .ebextensions 文件夹中。

安装脚本会在所有 EB 实例上的 /opt/elasticbeanstalk/hooks/appdeploy/post/ 文件夹 (documentation) 中创建一个文件。那里的任何 shell 脚本都将在部署后执行。放置在那里的 shell 脚本的工作方式如下:

  1. celeryenv变量中,virutalenv环境存放在 遵循 supervisord 符号的格式。这是一个逗号 分隔的环境变量列表。
  2. 然后脚本创建一个变量celeryconf,其中包含 配置文件作为一个字符串,其中包括之前解析的 环境变量。
  3. 这个变量然后通过管道传送到一个名为celeryd.conf 的文件中,一个 celery 守护进程的 supervisord 配置文件。
  4. 最后,将新创建的配置文件的路径添加到 supervisord.conf 主文件,如果它不存在的话。

这是脚本的副本:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/run_supervised_celeryd.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash

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

      # Create celery configuraiton script
      celeryconf="[program:celeryd]
      ; Set full path to celery program if using virtualenv
      command=/opt/python/run/venv/bin/celery worker -A myappname --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"

      # 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

【讨论】:

  • 感谢您发布此消息! Celery 和 EB 一直是个挑战,但您的解决方案似乎奏效了!但是我发现了一个问题:如果环境变量中存在% 符号,supervisord 会引发格式错误。我相信% 可以通过添加额外的% 来转义,例如%%。有没有办法格式化环境变量以将额外的% 添加到所有%github.com/Supervisor/supervisor/issues/291
  • 在这种情况下,您可以在解析环境变量的部分添加一个额外的查找/替换部分。例如,sed 's/%/%%/g' 将用%% 替换任何%。脚本开头的命令链执行了一系列字符串替换,以使环境变量列表 supervisord 兼容。所以尝试在第一个命令之后添加它:cat /opt/python/current/env | tr '\n' ',' | sed 's/%/%%/g' | ...
  • @yellowcap 感谢您的详细解答!
  • 这确实有效,但存在一些问题。如果你这样做,你的 web 和 worker 实例就会相互关联。因此,如果您的工作人员的负载增加,您将同时扩展您的 Web 和工作人员实例。另一个问题是,如果你有一个 celery beat 任务,如果你扩大规模,你最终会得到重复的任务。您必须只有 1 个实例运行您的 celery beat。我知道第二个问题与这个问题的内容无关,但是一个有 celery 工人的项目也可以有 celery beat。
  • 在我决定将 settings.py 中的一些变量迁移到我的 Elastic Beanstalk 环境属性之前,您的代码运行良好。确实,调用脚本时出现以下错误: for \'environment\' is badly formatted'>: file: /usr/lib64/python2.7/xmlrpclib.py line: 800 celeryd: ERROR (no such process)感谢您的帮助。
【解决方案2】:

我试图在 PHP 中做类似的事情,但无论出于何种原因,我都无法让工作人员继续运行。我切换到 EC2 服务器上的 AMI 并从那以后取得了成功。

【讨论】:

  • 我也这样做了
【解决方案3】:

适用于将 Elasticbeanstalk 与 Rails 和 Sidekiq 结合使用的用户。以下是最终为我解决问题的 ebextensions 集合:

https://gist.github.com/ctrlaltdylan/f75b2e38bbbf725acb6d48283fc2f174

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 2017-04-30
    • 1970-01-01
    • 2012-12-31
    • 2013-01-02
    • 2019-06-11
    • 2017-08-01
    • 2021-08-22
    • 2014-11-07
    相关资源
    最近更新 更多