我正在使用 gem 来创建一个 cron 作业。这个 cron 作业需要定期在我的 rails 应用程序上运行一个辅助方法。这个辅助方法检查我的模型的每个实例并决定是否更新它。
/app/helpers/auctions_helper.rb:
module AuctionsHelper
def self.checkForExpiredAuction
# method to update each auction that has expired
puts "There are currently #{Auction.count} auctions."
@auctions = Auction.all
@auctions.each do |auction|
if auction.end_time > Time.now.utc
auction.price = 1000000
auction.save
puts "just updated #{auction.product} auction"
end
end
puts "just updated any auctions that had expired"
end
end
时间表.rb:
set :output, "log/cron_log.log"
every 1.minute do
runner "AuctionsHelper.checkForExpiredAuction"
end
这将创建以下 cronjob:
# Begin Whenever generated tasks for: bestBay
* * * * * /bin/bash -l -c 'cd /home/bestbay && script/rails runner -e production '\''AuctionsHelper.checkForExpiredAuction'\'' >> log/cron_log.log 2>&1'
# End Whenever generated tasks for: bestBay
我遇到的问题是辅助方法无法访问表“拍卖”。从我的 cron_log.log 中:
Could not find table 'auctions' (ActiveRecord::StatementInvalid)
这似乎不是辅助方法本身的问题。如果我从终端运行:
rails runner AuctionsHelper.checkForExpiredAuction
我从 puts 消息中得到了一组不错的输出。那么为什么我的 cronjob 不能访问我的模型/表呢?