【问题标题】:Execute ruby on rails scheduled job with access to database from Windows通过从 Windows 访问数据库执行 ruby​​ on rails 计划作业
【发布时间】:2014-01-13 16:59:32
【问题描述】:

我有一个 RoR 应用程序。我想离线执行一些繁重的任务,以使我的应用程序更快。我需要实现某种 EventManager 来访问我的事件数据库表并执行它们。这个 EventManager 应该检查是否有新的事件要定期执行,所以我发现只要 gem 可以集成到你的应用程序中,以某种方式与 cron 集成,看起来足以满足我的要求并且非常易于使用。唯一的问题是因为它使用的是 cron,所以只能在 Unix 系统上运行。

为了在 windows 中模拟 cron,我找到了 Windows Task Scheduler What is the Windows version of cron?,所以我可以计划任何脚本。在另一篇文章中,我发现了如何调用 ruby​​ 类方法:

ruby -r "./secretary.rb" -e "Secretary.executeEvents"

问题是我认为这种方法并不像每当(集成在我的应用程序中)那么简单。我的意思是,当我手动执行脚本时,我得到了这个错误:

$ ruby -r "./secretary.rb" -e "Secretary.executeEvents"
c:/dev/yanpyapi/app/models/secretary.rb:1:in `<top (required)>': uninitialized constant ActiveRecord (NameError)
        from c:/Ruby200-x64/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
        from c:/Ruby200-x64/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'

这是我的 EventManager 类:

class Secretary < ActiveRecord::Base

  def self.executeEvents
    events = Event.find_by_sql("SELECT e.*
                                 FROM events e, event_states es
                                 WHERE E.execution_date <= Time.now
                                 AND es.name = 'Pending'
                                 AND es.id = e.event_state_id");

    for event in events do
      event.execute
    end    
  end
end

另一方面,我找到了 ruby​​ DBI http://www.tutorialspoint.com/ruby/ruby_database_access.htm

所以,我错过了这一点。我可以像我想做的那样简单地做吗?或者最好的方法是什么?我需要使用 DBI 吗?

请注意,这只是为了在开发环境中测试我的应用程序。我的生产环境是unix,所以我的想法是使用Whenever。所以,我希望解决方案尽可能简单。

【问题讨论】:

    标签: ruby-on-rails ruby windows cron


    【解决方案1】:

    我终于使用了在 windows 上运行的 rufus-scheduler。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 2014-05-07
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2013-04-08
      相关资源
      最近更新 更多