簡體   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