【问题标题】:Whenever gem not recognizing instance of class每当 gem 不识别类的实例时
【发布时间】:2016-04-27 15:40:22
【问题描述】:

我有以下通过 What gem 调用的类方法。如果我从 Rails 控制台调用该方法,它会完美运行。如果它是从 What cron 作业调用的,前两个记录器调用会显示在日志文件中,所以我知道它被调用了。但是,第三个没有。

似乎将该方法作为 cron 作业执行可以识别该类,但未连接到数据库以检索“消息”的各个实例

感谢任何帮助

def self.check_saved_messages

    #Code to test the method with the 'whenever' gem.   
    my_logger.warn("Scheduler fired at #{Time.now} Logger 1") 

    # 1. Grab all messages that have not been sent.
    @messages = Array.new #Creates new array to store messages in.

    #Stores all messages that have not been sent in @messages array.
    my_logger.warn("Scheduler fired at #{Time.now} #{Message.all} Logger 2") #Works as Cron job
    my_logger.warn("Scheduler fired at #{Time.now} #{Message.last} Logger 3") #Doesn't work as cron job

    Message.where(sent: false).find_each do |message|
              @messages.push(message)
    end

    # 2. Iterate through them and find the ones where the time has past to send.
    current_time = Time.now.utc

    #Send messages that have a send_time earlier than current time.
    @messages.each do |message|
        if  current_time.to_i > message.send_time.to_i
            message.send_message 
            message.send_time # Testing (working)
        end
    end

end

【问题讨论】:

    标签: ruby-on-rails whenever


    【解决方案1】:

    当我在开发环境中启动'whenever'命令时,它实际上可以连接到开发数据库,​​它完美地工作:-)

    每当 --update-crontab --set environment='development'

    【讨论】:

      【解决方案2】:

      默认情况下,whatever gem 将在生产环境中运行。由于您可能首先在开发环境中对其进行测试,这将导致一个问题,因为它会尝试连接到您不存在的生产数据库。

      你可以通过这两种方式解决:

      在方法调用时显式设置环境

      runner "Model.some_method", :environment => 'development'
      

      或在更新 crontab 时设置

      whenever --update-crontab --set environment='development'
      

      【讨论】:

        猜你喜欢
        • 2011-12-20
        • 1970-01-01
        • 2014-09-26
        • 2011-06-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-12
        • 1970-01-01
        相关资源
        最近更新 更多