【发布时间】: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
【问题讨论】: