当数据库在我的rails应用程序中关闭时,工作不起作用

时间:2010-11-26 12:05:17

标签: ruby-on-rails workling

我想安排我的工作人员使用cron作业定期检查数据库连接(比如5分钟)并相应地更新内存缓存密钥。所以在我的应用程序中,如果我找到要设置的memcache变量。当数据库启动时,我以不同的方式呈现我的页面。

但问题是,当数据库关闭时,工作人员会启动。当数据库启动时。它正确地发现数据库连接存在并更新memcache变量,一切正常。

我不知道,为什么工作人员在数据库关闭时无法启动。 我在截止日期前跑了。任何帮助非常感谢!

更新

这是我在工作没有开始时得到的错误

/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/mysql_adapter.rb: 527:在real_connect': Can't connect to MySQL server on '10.223.2.50' (111) (Mysql::Error) from /apps/Symantec/shasta/website/vendor/plugins/workling/script/../lib/workling/starling/poller.rb:35:in加入'         来自/apps/Symantec/shasta/website/vendor/plugins/workling/script/../lib/workling/starling/poller.rb:35:in listen' from /apps/Symantec/shasta/website/vendor/plugins/workling/script/../lib/workling/starling/poller.rb:35:in each'         来自/apps/Symantec/shasta/website/vendor/plugins/workling/script/../lib/workling/starling/poller.rb:35:in listen' from /apps/Symantec/shasta/website/vendor/plugins/workling/script/listen.rb:19 from /apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/application.rb:203:in load'         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/application.rb:203:in { {1}}开始”         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/monitor.rb:51:in { {1}}叉”         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/monitor.rb:51:in { {1}}每个”         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/monitor.rb:45:in { {1}}环”         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/monitor.rb:44:in { {1}} start_with_pidfile”         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/monitor.rb:64:in { {1}} start_with_pidfile”         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/monitor.rb:111:in { {1}} create_monitor”         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/application.rb:283:in { {1}}运行”         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons.rb:143:in {{1 }}呼叫'         来自/apps/Symantec/shasta/website/install/local/ruby-1.8.7-p299/lib/ruby/gems/1.8/gems/daemons-1.1.0/lib/daemons/cmdline.rb:112:in { {1}}运行”         来自script / workling_starling_client:17

1 个答案:

答案 0 :(得分:1)

也许工作者在启动时(总是)尝试连接数据库并抛出异常?你有没有记录工人的错误?

你是否用Rails编写了你的​​工作人员?也许写一个shell脚本,当工人无法启动时,它会假设数据库已关闭?

更新:在堆栈跟踪中有一个起点:script/workling_starling_client:17。有什么,在第17行?

由于第一行(异常消息本身)表示“ real_connect”:无法连接到'10 .223.2.50上的MySQL服务器'(111)(Mysql ::错误)“然后如果你将第17行(可能还有一些)包装在“rescue”块中就足够了,并检查错误信息是否有你想要的答案:

(当然,不要停在这里。继续你的检查,因为缺少例外意味着建立了连接)

begin
  line_17_is_here
rescue => e
  if e.message =~ /Can't connect to MySQL/
    handle_your_no_connection_state
  else
    raise e
  end
end

问题是:如果没有ActiveRecord,你能处理无连接状态吗?