簡體   English   中英

您如何使用 AWS Elastic Beanstalk 運行工作線程?

[英]How do you run a worker with AWS Elastic Beanstalk?

我正在 aws elastic beanstalk 上啟動一個 django 應用程序。 我想運行后台任務或工作人員以運行芹菜。

我找不到是否可能。 如果是,如何實現?

這是我現在正在做的事情,但這每次都會產生事件類型錯誤。

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

正如@chris-wheadon 在他的評論中所建議的那樣,您應該嘗試在后台運行 celery 作為守護進程。 AWS Elastic Beanstalk 已經使用supervisord來運行一些守護進程。 因此,您可以利用它來運行 celeryd 並避免為此創建自定義 AMI。 它對我來說很好用。

我所做的是在 EB 將應用程序部署到實例后,以編程方式將 celeryd 配置文件添加到實例中。 棘手的部分是該文件需要為守護進程設置所需的環境變量(例如 AWS 訪問密鑰,如果您在應用程序中使用 S3 或其他服務)。

下面是我使用的腳本的副本,將此腳本添加到配置 EB 環境的.ebextensions文件夾中。

安裝腳本在/opt/elasticbeanstalk/hooks/appdeploy/post/文件夾( 文檔)中創建一個文件,該文件位於所有 EB 實例上。 部署后將執行其中的任何 shell 腳本。 放置在那里的 shell 腳本的工作方式如下:

  1. celeryenv變量中,virutalenv 環境以遵循 supervisord 符號的格式存儲。 這是一個逗號分隔的環境變量列表。
  2. 然后腳本創建一個變量celeryconf ,其中包含作為字符串的配置文件,其中包括先前解析的 env 變量。
  3. 然后這個變量celeryd.conf送到一個名為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

我試圖在 PHP 中做類似的事情,但是無論出於何種原因我都無法讓工作人員繼續運行。 我在 EC2 服務器上切換到 AMI,從那時起就取得了成功。

對於那些將 Elasticbeanstalk 與 Rails 和 Sidekiq 一起使用的人。 這是一組 ebextensions,它們最終為我解決了問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM