【问题标题】:How to upgrade Django Celery App from Elastic Beanstalk Amazon Linux 1 to Amazon Linux 2如何将 Django Celery App 从 Elastic Beanstalk Amazon Linux 1 升级到 Amazon Linux 2
【发布时间】:2021-04-06 06:49:52
【问题描述】:

我正在尝试将运行 Celery 的 Django 应用程序从运行 Python 3.6 的 Amazon Elastic Beanstalk Linux 1 升级到使用 Python 3.8 的 Amazon Linux 2。

我在使用 Celery 应用程序时遇到问题。

在 Linux 1 我下面的文件

#!/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-worker]
; Set full path to celery program if using virtualenv
command=/opt/python/run/venv/bin/celery -A core worker -P solo --loglevel=INFO -n worker.%%h

directory=/opt/python/current/app/src
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
/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf reread

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

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

我不确定程序在 Linux 2 中的位置,所以我将文件修改如下:

#!/usr/bin/env bash

# Get django environment variables
celeryenv=`cat /var/app/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-worker]
; Set full path to celery program if using virtualenv
command=celery -A core worker -P solo --loglevel=INFO -n worker.%%h

directory=/var/app/current/src
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
/usr/local/bin/supervisorctl -c /opt/python/etc/supervisord.conf reread

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

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

我现在收到以下错误

2021/04/06 06:40:50.802882 [ERROR] An error occurred during execution of command [app-deploy] - [RunAppDeployPostDeployHooks]. Stop running the command. Error: Command .platform/hooks/postdeploy/03_celery-worker.sh failed with error exit status 127. Stderr:cat: /var/app/current/env: No such file or directory
tee: /opt/python/etc/celery.conf: No such file or directory
grep: /opt/python/etc/supervisord.conf: No such file or directory
tee: /opt/python/etc/supervisord.conf: No such file or directory
tee: /opt/python/etc/supervisord.conf: No such file or directory
.platform/hooks/postdeploy/03_celery-worker.sh: line 48: /usr/local/bin/supervisorctl: No such file or directory
.platform/hooks/postdeploy/03_celery-worker.sh: line 51: /usr/local/bin/supervisorctl: No such file or directory
.platform/hooks/postdeploy/03_celery-worker.sh: line 54: /usr/local/bin/supervisorctl: No such file or directory

我环顾四周,找不到文件现在的结构。非常感谢您的帮助。

【问题讨论】:

    标签: python django linux amazon-web-services django-celery


    【解决方案1】:

    默认情况下,管理员在 Amazon Linux2 中不存在。您只需重写此脚本即可在此类系统的监督下运行 celery

    `#!/usr/bin/env bash
    
    # Create the celery systemd service file
    echo "[Unit]
    Name=Celery
    Description=Celery service for My App
    After=network.target
    StartLimitInterval=0
    
    [Service]
    Type=simple
    Restart=always
    RestartSec=1
    User=root
    WorkingDirectory=/var/app/current
    ExecStart=$PYTHONPATH/celery worker -A documerge --loglevel=INFO -n worker.%%h
    EnvironmentFile=/opt/elasticbeanstalk/deployment/env
    
    [Install]
    WantedBy=multi-user.target
    " | tee /etc/systemd/system/celery.service
    
    # Start celery service
    systemctl start celery.service
    
    # Enable celery service to load on system start
    systemctl enable celery.service`
    

    【讨论】:

    • 我加入了这是.platform/hooks/postdeploy
    • 不,理想情况下它应该放在存储库根目录的 Procfile 文件中。只需写 celery_worker: celery worker -A my_django_app.settings.celery.app --concurrency=1 --loglevel=INFO -n worker.%%h。超级简单。
    猜你喜欢
    • 2021-07-02
    • 2021-12-11
    • 2020-07-23
    • 2022-11-23
    • 2020-11-08
    • 2022-07-23
    • 2021-01-12
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多