如何守护进程Rails的弹性魔豆Rake任务启动 [英] how to daemonize Rails Rake task on Elastic Beanstalk start up

查看:246
本文介绍了如何守护进程Rails的弹性魔豆Rake任务启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为我的-ENV的弹性魔豆的web服务器环境。我希望我的-ENV执行下面的bash命令在启动:捆绑EXEC耙作业:工作。我是新来的EB和我的Bash是平庸的(最好)。我昨天关于挥动整日与工程下面就起来, .ebextensions / start_worker.config

I have an Elastic Beanstalk web server environment called "my-env". I want my-env to execute the following bash command on start up: bundle exec rake jobs:work. I am new to EB and my Bash is mediocre (at best). I flailed about all day yesterday and came up with the following which works, .ebextensions/start_worker.config:

编辑:其实,下面的工作不正常。它似乎正常工作最初然后开始产生错误。也许是环境的变化?

Actually, the following isn't working correctly. It seems to work properly initially then starts generating errors. Perhaps the environment changes?

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 /钩子脚本/ appdeploy /后所以EB自动执行它,当我部署。我不得不守护进程,这样脚本就撑不起来EB的启动顺序。

So this creates a script in /opt/elasticbeanstalk/hooks/appdeploy/post so EB executes it automatically when I deploy. I had to daemonize so that the script wouldn't hold up EB's start up sequence.

所以我怀疑,这将在长期内很好地工作。我想AP preciate如何可以这样做更清洁的任何意见;也许是一个Linux服务的方式,或在一个Rails初始化。此外,我想知道是否有一种危险,即该脚本将运行超过一次或意外停止了。所以我想我要求任何一般就如何我可以最好从这里着手。

So I doubt that this is going to work well in the long run. I would appreciate any advice on how this could be done cleaner; perhaps by means of a linux service, or within a Rails initializer. Also I would like to know if there is a danger that this script will be run more than once or stop unexpectedly. So I guess I am asking for any general advise on how I could best proceed from here.

推荐答案

我相信我已经找到了合适的解决方案。我安装红宝石宝石守护,并切换脚本:

I believe I have found a suitable solution. I installed the ruby gem: "daemons" and switched the script to:

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    

到目前为止,似乎是工作。

So far seems to be working.

这篇关于如何守护进程Rails的弹性魔豆Rake任务启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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