我有通过Whatever gem调用的以下类方法。如果我从Rails控制台调用该方法,它可以完美地工作。如果从Whatever cron作业调用它,前两个记录器调用会显示在日志文件中,所以我知道它被称为okay。但是,第三个没有。
似乎将该方法作为cron作业执行可识别该类,但未连接到数据库以检索" Message"
的单个实例感谢任何帮助
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
答案 0 :(得分:1)
当我使用开发环境启动'never'命令时,它实际上可以连接到开发数据库,它完美地工作: - )
每当--update-crontab --set environment ='development'
答案 1 :(得分:0)
默认情况下,whatever
gem 将在生产环境中运行。由于您可能首先在开发环境中对其进行测试,这会导致问题,因为它会尝试连接到不存在的生产数据库。
您可以通过以下两种方式解决:
显式设置方法调用的环境
runner "Model.some_method", :environment => 'development'
或者在更新crontab时设置
whenever --update-crontab --set environment='development'