如何在Elastic Beanstalk启动时守护Rails Rake任务

时间:2014-10-08 15:28:31

标签: ruby-on-rails linux bash amazon-web-services elastic-beanstalk

我有一个名为" my-env"的Elastic Beanstalk Web服务器环境。我希望my-env在启动时执行以下bash命令:bundle exec rake jobs:work。我是EB的新手,而我的Bash是平庸的(充其量)。我昨天整天都在喋喋不休,想出了下面的内容,.ebextensions/start_worker.config

编辑: 实际上,以下内容无法正常运行。它似乎最初工作正常然后开始产生错误。环境可能会改变吗?

commands:
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
files:
  "/usr/sbin/jobs_worker.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/support/envvars
      export PATH=/home/ec2-user/.gem/ruby/2.1.2/bin:/opt/rubies/ruby-2.1.2/lib/ruby/gems/2.1.0/bin:/opt/rubies/ruby-2.1.2/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin:/home/ec2-user/bin
      cd $EB_CONFIG_APP_CURRENT
      su -c "RAILS_ENV=production bundle exec rake jobs:work" $EB_CONFIG_APP_USER
      echo "worker starting" >> /var/log/directory-hooks-executor.log    
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      nohup /usr/sbin/jobs_worker.sh 0<&- &> /var/log/directory-hooks-executor.log &

因此,这会在/opt/elasticbeanstalk/hooks/appdeploy/post中创建一个脚本,因此EB在部署时会自动执行它。我不得不守护,以便脚本不会阻止EB的启动序列。

所以我怀疑从长远来看这会有效。我将不胜感激任何有关如何做到更清洁的建议;也许是通过Linux服务,或在Rails初始化程序中。此外,我想知道是否存在这个脚本不止一次运行或意外停止的危险。所以我想我要求就如何最好地从这里进行任何一般性建议。

1 个答案:

答案 0 :(得分:2)

我相信我找到了合适的解决方案。我安装了ruby gem:&#34;守护进程&#34;并将脚本切换到:

commands:
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
  webapp_pids:
    command: "mkdir /home/webapp/pids"
    ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/support/envvars
      chown webapp:webapp /home/webapp/pids
      su -l -c "$EB_CONFIG_APP_CURRENT/bin/delayed_job --pid-dir=/home/webapp/pids restart" $EB_CONFIG_APP_USER
      echo "worker starting" >> /var/log/directory-hooks-executor.log    

到目前为止似乎正在发挥作用。