繁体   English   中英

在AWS Elastic Beanstalk中使用Resque和/或Redis部署Rails

[英]Deploying Rails with Resque and/or Redis in AWS Elastic Beanstalk

我正在尝试在独立模式,Resque和Redis上以Websockets-Rails部署我的Rails应用程序到AWS Elastic Beanstalk上。 Ubuntu 14.04服务器在Puma上运行Ruby 2.2。

在Puma的开发模式下,一切正常。 我在AWS Elastic Beanstalk上生产时遇到的错误似乎与Redis有关。

Redis::CannotConnectError (Error connecting to Redis on my.domain:6379 (ECONNREFUSED)):
  redis (3.2.0) lib/redis/client.rb:320:in `rescue in establish_connection'
  redis (3.2.0) lib/redis/client.rb:311:in `establish_connection'
  redis (3.2.0) lib/redis/client.rb:91:in `block in connect'
  redis (3.2.0) lib/redis/client.rb:273:in `with_reconnect'
  redis (3.2.0) lib/redis/client.rb:90:in `connect'
  redis (3.2.0) lib/redis/client.rb:337:in `ensure_connected'
  redis (3.2.0) lib/redis/client.rb:204:in `block in process'
  redis (3.2.0) lib/redis/client.rb:286:in `logging'
  redis (3.2.0) lib/redis/client.rb:203:in `process'
  redis (3.2.0) lib/redis/client.rb:109:in `call'
  redis (3.2.0) lib/redis.rb:1874:in `block in hget'
  redis (3.2.0) lib/redis.rb:37:in `block in synchronize'
  /opt/rubies/ruby-2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
  redis (3.2.0) lib/redis.rb:37:in `synchronize'
  redis (3.2.0) lib/redis.rb:1873:in `hget'
  redis-objects (1.2.1) lib/redis/hash_key.rb:29:in `hget'
  /opt/rubies/ruby-2.2.3/lib/ruby/gems/2.2.0/bundler/gems/websocket-rails-cf5d59b671c5/lib/websocket_rails/synchronization.rb:184:in `block in find_user'

有时我会收到Redis::TimeoutError (我似乎再也无法重现)。

我为Redis和Resque添加了预部署脚本:

# .ebextensions/redis_server.config
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/14_redis_server.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/support/envvars
      cd $EB_CONFIG_APP_ONDECK
      su -c "leader_only redis-server" $EB_CONFIG_APP_USER ||
      echo "Redis server startup failed, skipping."
      true

# .ebextensions/resque_workers.config
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/16_resque_workers.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      . /opt/elasticbeanstalk/support/envvars
      cd $EB_CONFIG_APP_ONDECK
      su -c "leader_only TERM_CHILD=1 QUEUES=* rake environment resque:work & rake environment resque:scheduler" $EB_CONFIG_APP_USER ||
      echo "Resque initialization failed, skipping."
      true

我怀疑这可能是由于Redis并未实际部署,但是我不确定如何检查它是否是由于。

在Elastic Beanstalk上部署时,启动Redis和其他rake任务(如Resque)的正确方法是什么?

另一个可能的问题是将Websockets与Redis一起使用。 我读过某个地方,我需要修改nginx.conf及其Upgrade标头标签以允许Websocket,但是我不确定这是否是导致此问题的直接原因。

编辑:

Redis现在在Elasticache上运行。 我再也没有收到任何Redis连接错误,但是Resque和Websockets似乎不起作用。 我认为不是Redis引起了问题,但可能与Resque和Websockets无关。

我尝试使用监控脚本来确保Resque调度程序和工作程序持久存在:

packages:
  yum:
    monit: []

files:
  "/etc/monit.d/resque_worker":
    mode: "000644"
    owner: root
    group: root
    content: |
      check process resque_worker_QUEUE
        with pidfile /var/app/resque_worker_QUEUE.pid
        start program = "/bin/sh -l -c 'cd /var/app/current; nohup rake environment resque:scheduler PIDFILE=/var/app/resque_scheduler.pid >> log/resque_scheduler.log 2>&1' && nohup rake nohup rake environment resque:work TERM_CHILD=1 QUEUE=* VERBOSE=1 PIDFILE=/var/app/resque_worker_QUEUE.pid >> log/resque_worker_QUEUE.log 2>&1'" as uid webapp and gid webapp
        stop program = "/bin/sh -c 'cd /var/app/current && kill -9 $(cat tmp/pids/resque_scheduler.pid) && rm -f /var/app/resque_scheduler.pid && kill -9 $(cat tmp/pids/resque_worker_QUEUE.pid) && rm -f /var/app/resque_worker_QUEUE.pid; exit 0;'"
        if totalmem is greater than 300 MB for 10 cycles then restart  # eating up memory?
        group resque_workers

commands:
  remove_bak:
    command: "rm /etc/monit.d/resque_worker.bak"
    ignoreErrors: true

service:
  sysvinit:
    monit:
      ensureRunning: true
      enabled: true

哪个似乎不起作用。

由于我手动运行命令且实例未销毁,因此它在开发中有效。

我还需要在端口3001上保留用于Websockets的独立服务器(我正在使用gem'Websocket-Rails')。

我认为在Elastic Beanstalk Web环境中运行队列进程不是一个好主意。 我认为使用香草EC2实例托管队列过程更有意义。 但是,您也可以使用Amazon Simple Queue Service(SQS)代替Resque。 这样,您就不必监视和维护队列实例,并且拥有一个非常可扩展的解决方案。

如果您使用Resque协调Rails> = 4.2应用程序中的后台作业,请查看Active Elastic Job gem。 它可以用一种优雅的方式解决您的问题。

免责声明:我是Active Elastic Job的作者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM