运行rufus-scheduler时断开数据库连接

时间:2014-05-08 09:56:07

标签: ruby-on-rails ruby ruby-on-rails-3 rufus-scheduler

我有一个场景,

我每天都在12点收集服务器上的在线设备/单元。为此,我在我的控制器中写了一份工作。目前我使用 .at 方法进行测试。这是我的控制器代码

require 'rubygems'
require 'rufus/scheduler'
require 'net/ssh'
require 'fileutils'
require "time"
require "date"

scheduler = Rufus::Scheduler.new

scheduler.at '2014/05/09 9:35:00' do
  autoupdates = AutoupdatesController.new
  autoupdates.get_all_online_units
end

class AutoupdatesController < ApplicationController    
  def get_all_online_units
    # logic for getting online units
  end    
end

调度程序运行正常,但它断开了我的数据库连接。此外,我检查了我是否只是初始化调度程序对象,那时它也在制动。  它提供无法连接到MySQL服务器错误。重新启动服务器后,它工作正常。

为此我使用rails 3.2.11, ruby 1.8.7 and rufus-scheduler 3.0.7 我该如何解决这个问题。

修改 现在我在 config/initializer/task_scheduler.rb 中创建了一个单独的文件。 这是我的文件内容

require 'rubygems'
require 'rufus/scheduler'
require 'net/ssh'
require 'fileutils'
require "time"
require "date"

scheduler = Rufus::Scheduler.new

scheduler.at '2014/05/08 1:25:00' do
    Rails.logger.info("#-----------In the config/initializer/task_scheduler.rb-----------#")
end

然后在启动服务器后,它给了我相同类型的错误

=> Booting Thin
=> Rails 3.2.11 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/var/lib/gems/1.8/gems/mysql2-0.3.14/lib/mysql2/client.rb:67:in `connect': Can't connect to MySQL server on 'XXXXXXXXXXXXXXXXXXX.com' (4) (Mysql2::Error)
    from /var/lib/gems/1.8/gems/mysql2-0.3.14/lib/mysql2/client.rb:67:in `initialize'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `new'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/mysql2_adapter.rb:16:in `mysql2_connection'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `send'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:315:in `new_connection'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:325:in `checkout_new_connection'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:247:in `checkout'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `loop'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:242:in `checkout'
    from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:239:in `checkout'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:102:in `connection'
    from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:101:in `connection'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/model_schema.rb:308:in `clear_cache!'
    from /var/lib/gems/1.8/gems/activerecord-3.2.11/lib/active_record/railtie.rb:97:in `_callback_before_3'
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:418:in `_run__321316982__prepare__4__callbacks'
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `send'
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:405:in `__run_callback'
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:385:in `_run_prepare_callbacks'
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `send'
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /var/lib/gems/1.8/gems/actionpack-3.2.11/lib/action_dispatch/middleware/reloader.rb:74:in `prepare!'
    from /var/lib/gems/1.8/gems/actionpack-3.2.11/lib/action_dispatch/middleware/reloader.rb:48:in `prepare!'
    from /var/lib/gems/1.8/gems/railties-3.2.11/lib/rails/application/finisher.rb:47
    from /var/lib/gems/1.8/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
    from /var/lib/gems/1.8/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
    from /var/lib/gems/1.8/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `run_initializers'
    from /var/lib/gems/1.8/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
    from /var/lib/gems/1.8/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
    from /var/lib/gems/1.8/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
    from /var/lib/gems/1.8/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `send'
    from /var/lib/gems/1.8/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
    from /home/pdcadmin/deploy/pdc-dashboard/config/environment.rb:5
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /var/lib/gems/1.8/gems/activesupport-3.2.11/lib/active_support/dependencies.rb:251:in `require'
    from /home/pdcadmin/deploy/pdc-dashboard/config.ru:3
    from /var/lib/gems/1.8/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval'
    from /var/lib/gems/1.8/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize'
    from /home/pdcadmin/deploy/pdc-dashboard/config.ru:0:in `new'
    from /home/pdcadmin/deploy/pdc-dashboard/config.ru:0

然后我评论了task_scheduler.rb文件中的一些行。这是该文件

require 'rubygems'
require 'rufus/scheduler'
require 'net/ssh'
require 'fileutils'
require "time"
require "date"
=begin
scheduler = Rufus::Scheduler.new

scheduler.at '2014/05/08 1:25:00' do
          Rails.logger.info("#-----------In the config/initializer/task_scheduler.rb-----------#")
end
=end

再次启动服务器它运行正常。

编辑2 这是我的config.ru文件

# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment',  __FILE__)
run MyApp::Application

1 个答案:

答案 0 :(得分:0)

也许这可以提供帮助:http://new-bamboo.co.uk/blog/2010/04/11/automatic-reconnection-of-mysql-connections-in-active-record

您的文字不清楚,何时“无法连接到MySQL”错误发生?当rufus-scheduler调用你的get_all_online_units?