我正在尝试用Websockets的独立模式、Resque和Redis部署我的Rails应用程序。Ubuntu14.04服务器正在Puma上运行Ruby2.2。
在美洲狮的开发模式中,一切都很好。我在AWS弹性豆柄上生产的错误似乎与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没有实际部署,但我不知道如何检查它是否是。
在弹性豆柄上部署Redis和其他rake任务的正确方法是什么?
另一个可能的问题是在Redis中使用websockets。我在某个地方读到需要修改nginx.conf及其升级头标记以允许Websockets,但我不确定这是否是问题的直接原因。
编辑:
Redis现在正在Elasticache上运行。我不再收到任何Redis连接错误,但是Resque和Websockets似乎不起作用。我不认为是Redis引起了这个问题,但可能是因为Resque和Websockets的孤立问题。
我尝试使用monit脚本来确保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上持久化Websocket的独立服务器(我使用的是gem‘Websockets’)。
发布于 2016-01-07 21:19:32
我不认为在Elastic环境中运行队列进程是个好主意。我认为使用普通的EC2实例来承载队列进程更有意义。但是,您也可以使用Amazon简单队列服务(SQS)而不是Resque。然后,您就不必监视和维护队列实例,并且有一个非常可伸缩的解决方案。
如果您使用Resque来协调Rails >= 4.2应用程序中的后台作业,那么请查看主动弹性作业 gem。它可能以一种优雅的方式解决你的问题。
免责声明:我是主动弹性作业的作者。
https://stackoverflow.com/questions/34639454
复制相似问题