cronジョブを作成するためにgemを使用しています。この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
schedule.rb:
set :output, "log/cron_log.log"
every 1.minute do
runner "AuctionsHelper.checkForExpiredAuction"
end
これにより、次のcronジョブが作成されます。
# 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がモデル/テーブルにアクセスできないのですか?